MapActivity实例管理
我有一个 MapActivity
子类,我想保留堆栈,但我无法在同一进程中保留 MapActivity
的多个实例。因此,我想出了两种方案来实现此目的:
- 传递 MapActivity 的状态及其触发的任何意图,然后让切换的活动重建 MapActivity通过发送重新创建活动的意图。此外,
MapActivity
将被设置为一次只创建该活动的一个实例。这种方法是有缺陷的,因为此活动有多个退出路径,因此需要更改所有路径来支持此操作。 - 将 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:
- Pass the state of the
MapActivity
along with any intents it fires and then let the activities that get switched to reconstruct theMapActivity
by sending an intent that recreates the activity. Additionally, theMapActivity
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. - Replace the
MapActivity
with a mock activity that does the recreation of the activity in it'sonResume()
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该在清单文件中使用 SingleInstance 属性,这会将早期启动的实例带到后台堆栈的顶部
You should use SingleInstance attribute in the manifest file, this will bring the earlier launched instance to the top of the backstack