如何在 backpress 上退出 android 应用程序?
How to exit the android application on backpress?
情况可以说,我有 5 个活动。说 Main activity, Activity 1, Activity 2, Activity 3 和 Activity 4.
可以直接使用 Activity 1、2、3 和 4,并在主 Activity 布局上插入按钮的帮助下。
情况:用户进入主界面Activity,然后按下按钮进入Activity 2. 然后他返回按下并返回主界面Activity。从那里他现在在另一个按钮的帮助下进入 Activity 3 并且类似地当他按下后退按钮时他进入 Main Activity.
主Activity --> Activity 2 --> 主Activity --> Activity 3 --> 主Activity.
问题:现在当用户从 Activity 3 进入 Main Activity 时。用户在 Main Activity 布局上。现在,如果用户按下 phone 上的后退按钮,则将执行以下过程退出应用程序:
主Activity --> Activity 3 --> 主Activity --> Activity 2 --> 主Activity --> 退出
需要完成:我想从 Main Activity 本身退出应用程序,如果用户按下其 phone 上的后退按钮,则不是按照上述路径退出应用程序,我希望用户能够通过显示一个简单的 AlertDialog 框 .
从 Main Activity 本身退出它
我找不到解决这个问题的办法,怎么办?
添加您的 MainActivity
@Override
public void onBackPressed() {
finish();
super.onBackPressed();
}
您需要在 MainActivity()
中覆盖 onBackPressed()
并使用 finishAffinity
完成所有活动
Called when the activity has detected the user's press of the back key.
finishAffinity()
:
added in API level 16
Finish this activity as well as all activities immediately below it in the current task that have the same affinity.
示例代码
@Override
public void onBackPressed() {
super.onBackPressed();
ActivityCompat.finishAffinity(MainActivity.this);
}
编辑
I want that the user should be able to exit it from the Main Activity itself by displaying a simple AlertDialog box.
@Override
public void onBackPressed() {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this)
.setTitle("Alert")
.setCancelable(false)
.setMessage("Are your sure want to exit?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.finishAffinity(MainActivity.this);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
builder.show();
}
编写双回按退出Main是一个很好的代码实践Activity。
boolean doubleBackToExitPressedOnce = false;
@Override
public void onBackPressed() {
if (doubleBackToExitPressedOnce) {
super.onBackPressed();
return;
}
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
doubleBackToExitPressedOnce=false;
}
}, 2000);
}
这是退出应用程序的常用方式,另外我建议在其他活动的清单中使用"nohistory"属性比 MainActivity.
boolean doubleBackToExitPressedOnce = false;
@Override
public void onBackPressed() {
if (doubleBackToExitPressedOnce) {
super.onBackPressed();
return;
}
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
doubleBackToExitPressedOnce=false;
}
}, 2000);
}
对于其他活动,请在清单中使用 nohistory 方法
<activity
android:name="com.omniapay.salesservice.Activities.LoginActivity"
android:label="LOGIN"
android:noHistory="true" />
情况可以说,我有 5 个活动。说 Main activity, Activity 1, Activity 2, Activity 3 和 Activity 4.
可以直接使用 Activity 1、2、3 和 4,并在主 Activity 布局上插入按钮的帮助下。
情况:用户进入主界面Activity,然后按下按钮进入Activity 2. 然后他返回按下并返回主界面Activity。从那里他现在在另一个按钮的帮助下进入 Activity 3 并且类似地当他按下后退按钮时他进入 Main Activity.
主Activity --> Activity 2 --> 主Activity --> Activity 3 --> 主Activity.
问题:现在当用户从 Activity 3 进入 Main Activity 时。用户在 Main Activity 布局上。现在,如果用户按下 phone 上的后退按钮,则将执行以下过程退出应用程序:
主Activity --> Activity 3 --> 主Activity --> Activity 2 --> 主Activity --> 退出
需要完成:我想从 Main Activity 本身退出应用程序,如果用户按下其 phone 上的后退按钮,则不是按照上述路径退出应用程序,我希望用户能够通过显示一个简单的 AlertDialog 框 .
从 Main Activity 本身退出它我找不到解决这个问题的办法,怎么办?
添加您的 MainActivity
@Override
public void onBackPressed() {
finish();
super.onBackPressed();
}
您需要在 MainActivity()
中覆盖 onBackPressed()
并使用 finishAffinity
完成所有活动
Called when the activity has detected the user's press of the back key.
finishAffinity()
:
added in API level 16
Finish this activity as well as all activities immediately below it in the current task that have the same affinity.
示例代码
@Override
public void onBackPressed() {
super.onBackPressed();
ActivityCompat.finishAffinity(MainActivity.this);
}
编辑
I want that the user should be able to exit it from the Main Activity itself by displaying a simple AlertDialog box.
@Override
public void onBackPressed() {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this)
.setTitle("Alert")
.setCancelable(false)
.setMessage("Are your sure want to exit?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.finishAffinity(MainActivity.this);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
builder.show();
}
编写双回按退出Main是一个很好的代码实践Activity。
boolean doubleBackToExitPressedOnce = false;
@Override
public void onBackPressed() {
if (doubleBackToExitPressedOnce) {
super.onBackPressed();
return;
}
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
doubleBackToExitPressedOnce=false;
}
}, 2000);
}
这是退出应用程序的常用方式,另外我建议在其他活动的清单中使用"nohistory"属性比 MainActivity.
boolean doubleBackToExitPressedOnce = false;
@Override
public void onBackPressed() {
if (doubleBackToExitPressedOnce) {
super.onBackPressed();
return;
}
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
doubleBackToExitPressedOnce=false;
}
}, 2000);
}
对于其他活动,请在清单中使用 nohistory 方法
<activity
android:name="com.omniapay.salesservice.Activities.LoginActivity"
android:label="LOGIN"
android:noHistory="true" />