管理android的backstack

我有一个活动层次结构,我有一个按钮,允许我从活动D到活动B.

《管理android的backstack》

问题是从D转到B将C放在靠背堆上,所以如果我做A-> C-> D-> B然后按回来它会把我送到C,而不是A(这个是我想要的).

当我点击B中的按钮时,有没有办法删除C,还是有某种解决方法?

最佳答案 考虑使用A作为调度程序.如果要从D启动B并在此过程中完成C,请在D中执行此操作:

// Launch A (our dispatcher)
Intent intent = new Intent(this, A.class);
// Setting CLEAR_TOP ensures that all other activities on top of A will be finished
//  and setting SINGLE_TOP ensures that a new instance of A will not
//  be created (the existing instance will be reused and onNewIntent() will be called)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
// Add an extra telling A that it should launch B
intent.putExtra("startB", true);
startActivity(intent);

在A.onNewIntent()中执行此操作:

@Override
protected void onNewIntent(Intent intent) {
    if (intent.hasExtra("startB")) {
        // Need to start B from here
        startActivity(new Intent(this, B.class));
    }
}
点赞