MapActivity实例管理

发布于 2024-12-16 10:59:41 字数 673 浏览 2 评论 0原文

我有一个 MapActivity 子类,我想保留堆栈,但我无法在同一进程中保留 MapActivity 的多个实例。因此,我想出了两种方案来实现此目的:

  1. 传递 MapActivity 的状态及其触发的任何意图,然后让切换的活动重建 MapActivity通过发送重新创建活动的意图。此外,MapActivity 将被设置为一次只创建该活动的一个实例。这种方法是有缺陷的,因为此活动有多个退出路径,因此需要更改所有路径来支持此操作。
  2. 将 MapActivity 替换为模拟活动,该活动在其 onResume() 方法或其他方法中重新创建活动,然后您切换到的活动可以幸福地不知道此问题。这种方法的问题是我不知道应该如何创建这个模拟活动,并激发一个意图来启动我想要切换到的活动。

所以我的问题是,是否有更好的方法来做到这一点,如果没有,如果可能的话,我将如何进行选项 2?

编辑:执行选项 2 的一种可能方法是使模拟活动成为一个路点,在其 onCreate() 中为您启动目标活动。但是,您必须小心,如果由于活动正在重建而再次调用 onCreate(),则该活动不会再次启动目标活动。这可以通过检查 savedInstanceState 是否为 null 来完成。

I have a MapActivity subclass and I want to preserve the stack, but I can't keep multiple instances of a MapActivity in the same process. So I have come up with 2 schemes to achieve this:

  1. Pass the state of the MapActivity along with any intents it fires and then let the activities that get switched to reconstruct the MapActivity by sending an intent that recreates the activity. Additionally, the MapActivity would be set so that intents only ever create a single instance of this activity at a time. This approach is flawed as there are multiple exit paths from this activity so all of them would need to be changed to support this.
  2. Replace the MapActivity with a mock activity that does the recreation of the activity in it's onResume() method or something and then the activities you switch to can remain blissfully unaware of this issue. The problem with this approach is I am unaware about how I should go about creating this mock activity and also fire an intent to start the activity I want to switch to.

So my question is this is there a better way to do this and, if not, how would I go about doing option 2, if it is possible?

EDIT: One possible way to do option 2 is to make the mock activity a waypoint that starts the target activity for you in it's onCreate(). But then one just has to be careful that if the onCreate() gets called again because the activity is being reconstructed, that one doesn't start the target activity again. This can be done by checking that savedInstanceState is null.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

━╋う一瞬間旳綻放 2024-12-23 10:59:41

您应该在清单文件中使用 SingleInstance 属性,这会将早期启动的实例带到后台堆栈的顶部

<activity android:launchMode="singleInstance"/>

You should use SingleInstance attribute in the manifest file, this will bring the earlier launched instance to the top of the backstack

<activity android:launchMode="singleInstance"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文