Android:处理 Activity 堆栈

标签 android

我有以下内容:

Activity A, B, C, D . AD可以随时随地通过应用程序联系到。

BC像这样达到:

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/

相关文章:

android - WebView 拉动刷新

android - 如何使用 HTTP-response-handler 接收 php 响应数组

android - Flutter 在主题中改变页面过渡速度/持续时间

android - 在 "Always"对话框中选择 "Complete action using"选项时,应用程序强制关闭

android - 将资源文件迁移到Androidx

Android:在启动的 Activity 上设置窗口背景

javascript - 如何在所有主要移动浏览器中使用 javascript 使视频播放器在按钮单击时全屏显示

Android:背景图片大小

java - Android WebView单图,去除右侧空白

java - 在 Unity Android 插件中访问和使用图像