简体   繁体   English

在导航菜单内的Android应用中创建AlertDialog以注销

[英]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. 菜单项全部处于切换状态,但注销已分开,我已分开并使用了IF语句。 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. 我可以注销并直接返回到登录屏幕,但我认为这对UX不太有用,因此我尝试添加一个alertdialog框,以使用户可以选择是否确认他们是否确实要注销。 。

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 它类似于吐司,您必须显式调用show()来显示对话框

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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