单击设备中的后退按钮时退出应用程序 [英] Exit the app when back button in device is clicked
问题描述
我的应用程序中有3个活动.
There are 3 activities in my app.
Activity A(Main Page) -> Activity B -> Activity C.
点击活动C 中的提交按钮
时,它将再次回到活动A .当我单击设备中的后退按钮以退出应用程序时,它将再次返回到活动C ,然后分别返回到 B 和 A ,出口.
When submit button
in Activity C is clicked, it will back to Activity A again. When I click the back button in device to exit the app, it will back to Activity C again, then B and A , then only can exit.
是否有一种方法可以让应用程序在按后退"按钮时立即退出活动A中按下了设备?
Is there a way to let the app straight away exit when back button in device is pressed in Activity A?
我在活动A 中添加了以下代码,但问题是它仍然转到活动C ,而不是退出.
I add below code in Activity A, but the problem is it still goes to Activity C instead of exit.
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);
}
推荐答案
感谢大家的回答和评论.我已经通过使用以下代码解决了该问题
Thanks guys for your answer and comment..I have solved it by using below code
public void onBackPressed(){
Intent a = new Intent(Intent.ACTION_MAIN);
a.addCategory(Intent.CATEGORY_HOME);
a.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(a);
}
这篇关于单击设备中的后退按钮时退出应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!