我有以下内容:
Activity A, B, C, D
. A
和 D
可以随时随地通过应用程序联系到。
B
和 C
像这样达到:
A -> B -> C
我有以下用例:
用户输入了C
( A -> B -> C
) 然后她去了 D
.
当她想去 A
,我想把她从A
转移到已经开始的队列中- 换句话说,我想让她开始C
.
像这样的东西 A -> B -> C -> D -> (same) C
.但我不想失去D
从 Activity 堆栈。此后,当她按下“后退”按钮时,她会转到D
。再次。
这样做是否可能且正确?最佳做法是什么?
最佳答案
很遗憾,您不能这样做。 The documentation says the following :
Note that when a new instance of an Activity is created to handle a new intent, the user can always press the BACK key to return to the previous state (to the previous activity). But when an existing instance of an Activity handles a new intent, the user cannot press the BACK key to return to what that instance was doing before the new intent arrived.
基本上,您不能在堆栈中的两个位置同时拥有相同的 Activity C 实例。
但是,您可以通过让 Activity C 将其瞬时状态保存到 Application
object 来使情况看起来确实如此。为您的应用程序。这将允许 Activity C 的所有实例共享状态,因此看起来就像它们是同一个实例。
您需要创建自己的 Application
子类,然后在 onResume()
和 onPause()< 中从 Activity C 读取和写入它
.
关于Android:处理 Activity 堆栈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2100401/