例如,
我有活动 A、B、C、D
A 呼叫 B
Intent intent = new Intent(A,B.class);
startActivity(intent);
然后,B 调用 C
Intent intent = new Intent(B,C.class);
startActivity(intent);
之后,C 调用 D
Intent intent = new Intent(C,D.class);
startActivity(intent);
在活动 D 中,我调用finish()
。它将返回到活动 C。
我的问题是如何在调用finish()
之前清除活动 A、B、C,以便应用程序像正常一样退出。
不要建议每次startactivity
都调用finish()
,因为应用可以按回上一个活动以继续。
这应该绝对有效...
Intent intent = new Intent(D,A.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("close",true);
startActivity(intent);
and in oncreat of A activity u have to write
if (getIntent().getBooleanExtra("close", false)) {finish();
}
else {
{
//ur previous code here
}
如果您有任何问题,请玩得开心
FLAG_ACTIVITY_CLEAR_TOP
FLAG_ACTIVITY_SINGLE_TOP
FLAG_ACTIVITY_CLEAR_TASK
FLAG_ACTIVITY_NEW_TASK
这可确保如果一个实例已经在运行并且不是顶部,那么它上面的任何内容都将被清除并被使用,而不是启动一个新实例(一旦你离开了活动 A ->活动 B,然后你想从 B 返回到 A,但额外的标志不应该影响你上面的情况)。
尝试添加FLAG_ACTIVITY_NEW_TASK
。
所以你的代码将是:
Intent intent = new Intent(this, A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
我在应用程序中使用以下方法。希望它会有所帮助。
Intent intent = new Intent(this, A.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // this will clear the stacks
intent.putExtra("exitme", true); // tell Activity A to exit right away
startActivity(intent);
并在活动 A 中添加以下内容:
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
if( getIntent().getBooleanExtra("exitme", false)){
finish();
return;
}
}
尝试使用Intent.FLAG_ACTIVITY_CLEAR_TOP
Intent intent = new Intent(this, A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
看这里http://developer.android.com/reference/android/content/Intent.html