跳转回去直接父活动时,pressed回跳转、直接、pressed

2023-09-04 09:59:45 作者:凉了夏末ζ蓝了海

我有在一个Android应用程序我工作的一个小问题:

I have got a small problem in an Android app I am working on :

有3项活动,即A,B,C和调用是按以下顺序: A - > B - > C。而在C,当我preSS BACK按钮,它应该回到A [对B,而不是默认。而pressing回来会退出程序。

There are 3 activities namely A , B , C and the invocation is in the following order : A -> B -> C. While in C, when I press BACK button, it should go back to A[Instead of the B by default]. And pressing BACK in A will exit the program.

我试图调用一个意图从C到A.但是,在这种情况下,呼叫调用进入一个循环: A - > B - >ç - >答:由于新的活动被压入堆栈的顶部。这样一来,当背部pssed,而不是退出[A是一开始]在A,$ P $,它会为C,然后B,然后回到A中不必要的圈子。

I tried to call an intent from C to A. But in this case the call invocation gets into a loop : A -> B -> C -> A since the new activity is pushed on top of the stack. As a result, when BACK is pressed at A, instead of exiting [A is the start], it goes to C and then B and then back to A in a needless circle.

这将是巨大的,如果有人能够给出一个更好的办法来解决这个糊涂的情况下!

It would be great if someone could give a better way to address this loopy scenario!

推荐答案

很简单! 当你开始活动C,从B,使用B.finish()。事情是这样的。

Very simple!! When you are starting the activity C, from B, use B.finish(). Something like this.

Intent i = new Intent(B.this, C.class);
B.this.finish();
startActivity(i);

这会从栈中删除乙!