[英]Close the app when back button clicked twice
My Activity flow looked like this 我的活动流程如下所示
LogIn Activity-> Activity A(Main Page)->Activity B-> Activity C
When button in C is clicked, it will intent to A. 单击C中的按钮时,它将指向A。
When back button in A is pressed twice,it should close the app. 两次按A中的后退按钮时,应关闭应用程序。
boolean doubleBackToExitPressedOnce = false;
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;
Intent a = new Intent(Intent.ACTION_MAIN); // close app code
a.addCategory(Intent.CATEGORY_HOME);
a.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(a);
}
},2000);
Problem : 问题:
when I press the button one times, it will display
Please click BACK again to exit
, and then it will close the app automatically even I didn't clicked the button twice.当我按一次按钮时,它将显示
Please click BACK again to exit
,然后即使我没有两次单击按钮,它也会自动关闭该应用程序。If I click the button twice, it will back to LogIn Activity.
如果单击两次按钮,它将返回“登录活动”。 What is the correct way to write ?
正确的写法是什么? Thanks
谢谢
Edit 编辑
If I remove the intent
如果我删除
intent
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);
}
When double click the back button, it will back to Activity C again. 双击后退按钮时,它将再次返回到活动C。
maybe just use long lastTimePressed=0L;
也许只使用
long lastTimePressed=0L;
and store in it System.currentTimeMillis();
并存储在其中
System.currentTimeMillis();
and if(System.currentTimeMillis()-lastTimePressed>2000) finish();
和
if(System.currentTimeMillis()-lastTimePressed>2000) finish();
2000 for Toast.Length_SHORT
, 3500 for Toast.LENGTH_LONG
2000
Toast.Length_SHORT
,3500 Toast.LENGTH_LONG
long lastTimePressed=0L;
@Override
public void onBackPressed (){
if(System.currentTimeMillis()-lastTimePressed>2000) //short Toast duration, now should be faded out
finish();
else
Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show();
lastTimePressed=System.currentTimeMillis();
}
don't start new Activity
(or maybe? if you have implemented singleTask
or singleTop
or noHistory
, but I doubt). 不要启动新的
Activity
(或者可能是?如果您已经实现了singleTask
或singleTop
或noHistory
,但我对此表示怀疑)。 finish();
and super.onBackPressed();
和
super.onBackPressed();
do the same practially. 实际做同样的事情。 possibility for override
onBackPressed
is added later in API5, because its simply usefull :) 稍后在API5中添加重写
onBackPressed
可能性,因为它非常有用:)
ohh, now I see your edit and everything is clear. 哦,现在我可以看到您所做的编辑,一切都清楚了。 Don't start your Activities with
startActivity
, but with startActivityForResult
. 不要使用
startActivity
开始您的活动,而要使用startActivityForResult
。 Implement also onActivityResult
( check out here ). 还实现
onActivityResult
( 在此处签出 )。 When you use 使用时
private static final int MY_RESULT_IS_KILL_MY_APP=4573; //random
setResult(MY_RESULT_IS_KILL_MY_APP);
finish();
and Activity
below receives that in own onActivityResult
it should also set this result and call finish();
下面的
Activity
在自己的onActivityResult
中接收到该结果,它还应该设置该结果并调用finish();
. 。 This way stack of Activities clean away and entire Application will exit.
这样,活动堆栈便会清除,整个应用程序将退出。 Without setting this static result
finish();
不设置此静态结果,则
finish();
will finish only current Activity
将仅完成当前
Activity
Finish LogIn Activity Before opening Activity A. that's it. 完成登录活动,然后再打开活动A。
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;
MainActivity.this.finish();
}
},2000);
}
@Override
public void onBackPressed() {
if (doubleBackToExitPressedOnce) {
super.onBackPressed();
return;
}
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Press again to exit", Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
doubleBackToExitPressedOnce=false;
}
}, 2000);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.