以编程方式设置活动启动模式 [英] Set activity launch mode programmatically
问题描述
我需要根据我的应用状态用不同的launchMode
调用活动.在一种情况下,应为singleInstance
,在其他情况下应为singleTask
.我知道如何在 AndroidManifest 中设置launchMode
,但是由于它应该是动态属性,因此必须从代码中进行设置.我以为可以在开始活动之前向意图添加一些特定的标志,但是我只找到了singleTop
标志.那么有什么办法可以解决这个问题?
谢谢
I need to call activity with different launchMode
according to my app state. In one case it should be singleInstance
, in other - singleTask
. I know how to set launchMode
in AndroidManifest, but since it should be dynamic property I have to do it from code. I thought, that I can add some specific flag to intent, before starting activity, but I found only singleTop
flag. So is any way to solve this issue?
Thanks
推荐答案
经过一些调查,我注意到不可能以这种方式执行此操作.但是好消息是我有一些解决方法:
After some investigations I've noticed that it is impossible to do that in such way. But good news is that i've got some workaround:
您必须创建两个Activities
,每个都有相应的 launchModes .
一个Activity
是真实的Activity
,其中包含您的代码,而另一个Activity
只是在onCreate()
方法中调用主Activity
,但是由于它需要 launchMode ,因此主Activity
将以该模式启动.不是很好,但是完全可以解决问题.
You have to create two Activities
, each with corresponding launchModes.
One Activity
is real Activity
with your code inside, and another one will just call main Activity
in onCreate()
method, but since it will have needed launchMode, main Activity
will be launched with that mode. Not very nice, but completely working solution.
然后,不要尝试使用意图标志打开Activity
,而是根据需要使用 launchMode 放置Activity
的意图class
.
After that, instead of trying open your Activity
with intent flags, put in intent class
of the Activity
according to launchMode you need.
这篇关于以编程方式设置活动启动模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!