简体   繁体   English

按下返回按钮时确认关闭应用程序

[英]Confirm application closure when pressing back button

How can I make it so that pressing the back button does not close my application? 我该如何做才能使按下后退按钮不会关闭我的应用程序? I want to display a confirmation message. 我想显示一条确认消息。

Thank you. 谢谢。

Source: Override back button to act like home button 来源: 覆盖后退按钮,使其像主页按钮一样

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        //Display confirmation here, finish() activity.
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

That was a very quick search, try to look a little next time. 这是一个非常快速的搜索,下次再试一下。

Application close confirmation is here 申请关闭确认在这里

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("Do you want to close?")
               .setCancelable(false)
               .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                        //do finish
                    ImageViewActivity.this.finish();
                   }
               })
               .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       //do nothing
                       return;
                   }
               });
        AlertDialog alert = builder.create();
        alert.show();


    }
    return super.onKeyDown(keyCode, event);
}

try this on back button pressed it shows confirmation message 尝试此后退按钮按下它显示确认消息

@Override
public void onBackPressed() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Thank You!!!!!")
           .setCancelable(false)
           .setPositiveButton("OK", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   //do things
               }
           });
    AlertDialog alert = builder.create();
    alert.show();
}   

It is not recommended to exit your Android Application.Android's design does not favor exiting an application by choice, but rather manages it by the OS. 不建议退出您的Android应用程序。Android的设计不支持选择退出应用程序,而是由OS对其进行管理。 You can bring up the Home application by its corresponding Intent: 您可以通过相应的Intent调出Home应用程序:

You can fire this Intent on onKeyDown() in Android 1.x and higher or onBackPressed() in Android 2.x and higher 您可以在Android 1.x及更高版本中的onKeyDown()或Android 2.x及更高版本中的onBackPressed()上触发此Intent

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

A simpler approach is to capture the Back button press and call moveTaskToBack(true) as follows: 一种更简单的方法是捕获“后退”按钮的按下并调用moveTaskToBack(true),如下所示:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        moveTaskToBack(true);
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

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

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