Marlon
Marlon

Reputation: 69

Navigation Drawer Activity setting clicked event in Drawers Item

Hi i'm trying to put an event Item click in my Navigation View from drawer it works fine, but the problem is the other items in my drawer stop working, it seems that putting an item click event on my navigation view affect my NavigationController

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bundy_clock);
    dBhelper = new DBhelper(this);

    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    NavigationView navigationView = findViewById(R.id.nav_view);
    // Passing each menu ID as a set of Ids because each
    // menu should be considered as top level destinations.
    mAppBarConfiguration = new AppBarConfiguration.Builder(
            R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow,
            R.id.nav_tools, R.id.nav_share, R.id.nav_send)
            .setDrawerLayout(drawer)
            .build();
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
    NavigationUI.setupWithNavController(navigationView, navController);

    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        int id = item.getItemId();

        if(id == R.id.nav_logout){
            AlertDialog.Builder conDialog = new AlertDialog.Builder(BundyClock.this);
            conDialog.setTitle("Confirm");
            conDialog.setMessage("Are you sure you want to log out?");
            conDialog.setCancelable(false);

            conDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    startActivity(new Intent(BundyClock.this, MainActivity.class));
                    Toast.makeText(getApplicationContext(),"Successfully Logout",Toast.LENGTH_SHORT).show();
                    finish();
                }
            });

            conDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                }
            });
            final AlertDialog sdialog = conDialog.create();
            sdialog.show();
        }
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
        }
    });

}

Upvotes: 1

Views: 188

Answers (1)

Md. Asaduzzaman
Md. Asaduzzaman

Reputation: 15423

You have to use NavigationUI.onNavDestinationSelected to handle this. Check below:

boolean handled = NavigationUI.onNavDestinationSelected(menuItem, navController);
if (!handled) {
    if(id == R.id.nav_logout){
        AlertDialog.Builder conDialog = new AlertDialog.Builder(BundyClock.this);
        conDialog.setTitle("Confirm");
        conDialog.setMessage("Are you sure you want to log out?");
        conDialog.setCancelable(false);

        conDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                startActivity(new Intent(BundyClock.this, MainActivity.class));
                Toast.makeText(getApplicationContext(),"Successfully Logout",Toast.LENGTH_SHORT).show();
                finish();
            }
        });

        conDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        final AlertDialog sdialog = conDialog.create();
        sdialog.show();
    }
}

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return handled;

Upvotes: 3

Related Questions