Set activity launch mode programmatically

痴心易碎 提交于 2019-12-23 10:19:38

问题


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


回答1:


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:

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.

After that, instead of trying open your Activity with intent flags, put in intent class of the Activity according to launchMode you need.




回答2:


In my case I need two different launchMode related to different android API level: in AndroidManifest

android:launchMode="@integer/launchModeAPIlevel"

and different integers value inside folders values-18, values-21

<integer name="launchModeAPIlevel">1</integer>

launchmode 1 == singleTop singleTask == 2




回答3:


Just create two Activities A and B, B extends A. In manifest declare launchMode="singleTask" for A, and launchMode="singleInstance" for B. And start the Activity according to launchMode you need.



来源:https://stackoverflow.com/questions/19155526/set-activity-launch-mode-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!