简体   繁体   中英

Creating an alertdialog in Android app inside a navigation menu to logout

I'll try and explain this the best I can and I'm sure there will be a simple explanation to this.

I have a navigation menu with a logout option. The menu items are all in a switch case except for the logout which I have separated and used an IF statement. I got it to logout and go straight back to the login screen but I didn't think that was great for UX so I tried to add an alertdialog box to give the user the option to confirm whether or not they definitely want to logout or not.

I tried this (this is just the switch):

navigationView = findViewById(R.id.navigation_view);
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId())
            {
                case R.id.nav_home:
                    fragmentTransaction = getSupportFragmentManager().beginTransaction();
                    fragmentTransaction.replace(R.id.main_container, new HomeFragment());
                    fragmentTransaction.commit();
                    getSupportActionBar().setTitle("");
                    item.setCheckable(true);
                    mDrawerLayout.closeDrawers();
                    break;

                case R.id.nav_create_case:
                    fragmentTransaction = getSupportFragmentManager().beginTransaction();
                    fragmentTransaction.replace(R.id.main_container, new CreateCaseFragment());
                    fragmentTransaction.commit();
                    getSupportActionBar().setTitle("Create a Case");
                    item.setCheckable(true);
                    mDrawerLayout.closeDrawers();
                    break;

                case R.id.nav_barcode_scanner:
                    fragmentTransaction = getSupportFragmentManager().beginTransaction();
                    fragmentTransaction.replace(R.id.main_container, new BarcodeScannerFragment());
                    fragmentTransaction.commit();
                    getSupportActionBar().setTitle("Barcode Scanner");
                    item.setCheckable(true);
                    mDrawerLayout.closeDrawers();
                    break;

                case R.id.nav_checklists:
                    fragmentTransaction = getSupportFragmentManager().beginTransaction();
                    fragmentTransaction.replace(R.id.main_container, new ChecklistsFragment());
                    fragmentTransaction.commit();
                    getSupportActionBar().setTitle("Checklists");
                    item.setCheckable(true);
                    mDrawerLayout.closeDrawers();
                    break;

                case R.id.nav_create_report:
                    fragmentTransaction = getSupportFragmentManager().beginTransaction();
                    fragmentTransaction.replace(R.id.main_container, new CreateReportFragment());
                    fragmentTransaction.commit();
                    getSupportActionBar().setTitle("Create Report");
                    item.setCheckable(true);
                    mDrawerLayout.closeDrawers();
                    break;


            }
            if (item.getItemId() == R.id.nav_logout) {

                final AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
                alert.setTitle("Logout");
                alert.setMessage("Are you sure you wish to logout?")
                        .setCancelable(false)
                        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                FirebaseAuth.getInstance().signOut();
                            }
                        })
                        .setNegativeButton("No", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                Intent i2 = new Intent(MainActivity.this, MainActivity.class);
                                i2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                startActivity(i2);
                            }
                        });

So with this, the logout button does nothing. Doesn't crash or anything. Just does nowt.

Any help greatly appreciated. Especially if I've done something stupid.

Thanks in advance

You are missing a show statement:

alert.show();

It's similar to a toast, you have to explicitly call show() to show the dialog

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM