首页 > 解决方案 > 为什么此警报对话框未在 backPressed 上显示?

问题描述

这是我的 onBackPressed() :

@Override
public void onBackPressed() {
        final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(MainScreen.this);
        String checkInVal = preferences.getString("checkIn", "");
        if (!PRE_USER_ACTIVE.equalsIgnoreCase("1")) {
            super.onBackPressed();
            finish();

        } else if (MainScreen.this.getSupportFragmentManager().findFragmentByTag("VOLUNTEERDETAILS") != null && checkInVal.equals("yes")) {
            popFragment();
        } else {
            if (Functions.iNumberOfLoadedFragments == 0) {
                String exitLog = "Exit Application", ok = "OK", cancel = "CANCEL";
                String sureLog = "Are you sure you want to exit  " + MainScreen.this.getResources().getString(R.string.app_name);
                if (EasyPreference.with(MainScreen.this, PRE_KEY)
                        .getString(PRE_USER_LANGUAGE, PRE_LANGUAGE_ENGLISH).equalsIgnoreCase(PRE_LANGUAGE_ASSEMESS)) {

                    exitLog = "প্ৰস্থান কৰক অ্যাপ্লিকেশন";
                    sureLog = "আপুনি Our SchoolZoneৰ পৰা প্ৰস্থান কৰিবলৈ নিশ্চিতনে";
                    ok = "স্বীকাৰ কৰক";
                    cancel = "বাতিল কৰক";
                } else if (EasyPreference.with(MainScreen.this, PRE_KEY)
                        .getString(PRE_USER_LANGUAGE, PRE_LANGUAGE_ENGLISH).equalsIgnoreCase(PRE_LANGUAGE_THI)) {

                    exitLog = "ออกจากแอพพลิเคชัน";
                    sureLog = "คุณแน่ใจหรือว่าต้องการออกจาก Our SchoolZone";
                    ok = "ตกลง";
                    cancel = "ยกเลิก";
                }
                new LovelyStandardDialog(MainScreen.this)
                        .setTopColor(Functions.getBackColor())
                        .setIcon(R.drawable.ic_info_outline_white_36dp)
                        .setTitle(exitLog)
                        .setMessage(sureLog)
                        .setPositiveButton(ok, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                MainScreen.super.onBackPressed();
                            }
                        })
                        .setNegativeButton(cancel, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {

                            }
                        })
                        .show();

            } else if (Functions.iNumberOfLoadedFragments > 0) {
                Functions.iNumberOfLoadedFragments--;
                super.onBackPressed();
            }
        }
}

ALertDialog 需要显示在从此 Activity 按下的后退按钮中,而不是应用程序只是退出到启动器。

有什么可能的问题,请回复您的宝贵建议。谢谢

标签: androidandroid-alertdialog

解决方案


使用以下代码: AlertDialog for exit from app

@Override
public void onBackPressed() {
    new AlertDialog.Builder(this)
            .setMessage("Are you sure you want to EXIT ?")
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    moveTaskToBack(true);
                    finish();

                }

            })
            .setNegativeButton("No", null)
            .show();
}

如果觉得有用,请点赞,以便其他人可以得到答案


推荐阅读