Want to move to another activity from navigation drawer

Viewed 720

i have a map activity. and it has a navigation drawer. but when i click in any of the navigation items i cant go to another activity. i am attaching my code here.there is no errors. When i click any item of the navigation drawer its not moving to another activity.

     nv = (NavigationView) findViewById(R.id.nv);
            nv.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                    int id = item.getItemId();
                    switch (id) {
                        case R.id.profile:

                            Intent i = new Intent(HomeActivity.this, ProfileActivity.class);
                            startActivity(i);
                            break;
                        case R.id.dona:
                            Intent j = new Intent(HomeActivity.this, DonateActivity.class);
                            startActivity(j);
                            break;

                        case R.id.pla:
                            Intent k = new Intent(HomeActivity.this, PlantActivity.class);
                            startActivity(k);
                            break;

                        case R.id.not:
                            Intent l = new Intent(HomeActivity.this, NotificationActivity.class);
                            startActivity(l);
                            break;

                        case R.id.hist:
                            Intent m = new Intent(HomeActivity.this, HistoryActivity.class);
                            startActivity(m);
                            break;

                        case R.id.hel:
                            Intent n = new Intent(HomeActivity.this, HelpActivity.class);
                            startActivity(n);
                            break;



                    }

                    return false;

                }
            });





 SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);


    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (ContextCompat.checkSelfPermission(this,
                    Manifest.permission.ACCESS_FINE_LOCATION)
                    == PackageManager.PERMISSION_GRANTED) {
                buildGoogleApiClient();
                mMap.setMyLocationEnabled(true);
            }
        } else {
            buildGoogleApiClient();
            mMap.setMyLocationEnabled(true);
        }

    }






 protected synchronized void buildGoogleApiClient() {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API).build();
        mGoogleApiClient.connect();
    }

    @Override
    public void onConnected(Bundle bundle) {

        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(1000);
        mLocationRequest.setFastestInterval(1000);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_FINE_LOCATION)
                == PackageManager.PERMISSION_GRANTED) {
            LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
        }

    }

    @Override
    public void onConnectionSuspended(int i) {

    }

    @Override
    public void onLocationChanged(Location location) {

        mLastLocation = location;
        if (mCurrLocationMarker != null) {
            mCurrLocationMarker.remove();
        }
        //Place current location marker
        LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.position(latLng);
        markerOptions.title("Current Position");
        markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
        mCurrLocationMarker = mMap.addMarker(markerOptions);

        //move map camera
        mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
        mMap.animateCamera(CameraUpdateFactory.zoomTo(11));

        //stop location updates
        if (mGoogleApiClient != null) {
            LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
        }

    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (t.onOptionsItemSelected(item))
            return true;
        int id = item.getItemId();



        if (id == R.string.action_settings) {
            return true;
        }


            return super.onOptionsItemSelected(item);


    }
}
4 Answers

you need to return truein end of onNavigationItemSelected

  nv.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                    int id = item.getItemId();
                    switch (id) {
                        case R.id.profile:

                            Intent i = new Intent(HomeActivity.this, ProfileActivity.class);
                            startActivity(i);
                            break;

                        /*--your code--*/


                        case R.id.hist:
                            Intent m = new Intent(HomeActivity.this, HistoryActivity.class);
                            startActivity(m);
                            break;

                        case R.id.hel:
                            Intent n = new Intent(HomeActivity.this, HelpActivity.class);
                            startActivity(n);
                            break;



                    }

                yourDrawer.closeDrawer(GravityCompat.START);

                    return true;       //you need to return true here, not false

                }
            });

Try this

  • I am using direct on item selected click method like this

    public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();
    
    if (id == R.id.nav_buildings) {
        startActivity(new Intent(SuperAdminHomeActivity.this,BuildingsActivity.class));
    
    } else if (id == R.id.nav_companies) {
        startActivity(new Intent(SuperAdminHomeActivity.this,CompaniesActivity.class));
    
    } else if (id == R.id.nav_reports) {
        startActivity(new Intent(SuperAdminHomeActivity.this,ReportsActivity.class));
    
    } else if (id == R.id.nav_invoice) {
        startActivity(new Intent(SuperAdminHomeActivity.this,InvoiceActivity.class));
    
    } else if (id == R.id.nav_notifications) {
        startActivity(new Intent(SuperAdminHomeActivity.this,NotificationsActivity.class));
    
    } else if (id == R.id.nav_ticket) {
        startActivity(new Intent(SuperAdminHomeActivity.this,TicketActivity.class));
    
    }
    else if (id == R.id.nav_my_ticket) {
        startActivity(new Intent(SuperAdminHomeActivity.this,TicketsAddActivity.class));
    }
    
    else if(id==R.id.nav_logout){
        alertDialogue("Do you want to logout?");
    }
    
    drawer.closeDrawer(GravityCompat.START);
    return true;
     }
    
  • i am not using drawer click listener.

  • this is my drawer layout

enter image description here

its working fine please try

After carefully examaning your full code I realized that the .setNavigationItemSelectedListener was not being called. Even if the the drawer was displaying it was at the back of your activity. So you were not actually clicking on it.

So the simple solutuion is :

nv.bringToFront();

Put this after you initialize nv

Make your Navigation View a global variable

Related