在导航菜单中的Android应用程序中创建alertdialog以注销

时间:2017-12-22 22:44:18

标签: java android switch-statement alertdialog

我会尽力解释这个问题,我相信会有一个简单的解释。

我有一个带有注销选项的导航菜单。除了我已经分离并使用IF语句的注销外,菜单项都在一个switch case中。我让它退出并直接回到登录界面,但我认为这不适合用户体验,所以我尝试添加一个警告对话框,让用户可以选择确认他们是否肯定要注销

我试过了(这只是转换):

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);
                            }
                        });

因此,注销按钮不执行任何操作。不崩溃或任何事情。就是现在。

任何帮助非常感谢。特别是如果我做了些蠢事。

提前致谢

1 个答案:

答案 0 :(得分:2)

你错过了一个show语句:

alert.show();

它类似于吐司,你必须明确调用show()来显示对话框