如何从调用堆栈中结束特定活动的先前实例

huangapple go评论77阅读模式
英文:

How to kill previous instances of a particular activity from call stack

问题

当我在活动C中按下重置按钮时,我想要销毁活动B的先前实例,以便我的堆栈变为 [A | C | B]。我不能在转到活动C时完成活动B,因为我可能需要返回到B。

英文:

My activity flow is like A->B->C->B,

When I press reset button in activity C I go to Activity B

Button reset=(Button) findViewById(R.id.button3);
        reset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view){
                Intent intent = new Intent(C.this, B.class);
                startActivity(intent);
                finish();
            }
        });

But, I want to destroy the previous instance of activity B when I press reset button in activity C, so that my stack becomes
[A | C| B].
I can't finish Activity B while going to Activity C as I might have to come back to B.

答案1

得分: 0

你可以使用广播完成命令,并在活动中接收到一个完成信号时,结束该操作。

英文:

you can use broadcast finish order and when receive in activity a finish that

答案2

得分: 0

添加FLAG_ACTIVITY_CLEAR_TOP,当你从c启动B时,它会删除不需要的B。

Intent i = new Intent(this, B.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
英文:

add FLAG_ACTIVITY_CLEAR_TOP when you start B form c it will deleted the unneeded B

Intent i = new Intent(this, B.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);

huangapple
  • 本文由 发表于 2020年9月16日 15:10:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/63914880.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定