这个问题已经有了答案:
Android: Clear the back stack
36个答案
我从活动a->b->c->d->e开始。当我从d->e开始时,堆栈中应该没有活动,但是,用户可以使用d中的back按钮并转到c(不刷新活动c,就像正常的back函数一样)
最佳答案
您可以在所有要关闭的活动(a、b、c、d)中添加aBroadcastReceiver
:
public class MyActivity extends Activity {
private FinishReceiver finishReceiver;
private static final String ACTION_FINISH =
"com.mypackage.MyActivity.ACTION_FINISH";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
finishReceiver= new FinishReceiver();
registerReceiver(finishReceiver, new IntentFilter(ACTION_FINISH));
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(finishReceiver);
}
private final class FinishReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ACTION_FINISH))
finish();
}
}
}
…然后打电话给他们…
sendBroadcast(new Intent(ACTION_FINISH));
…在活动e中,也检查this nice example。