Having One Instance Of Activity

China☆狼群 提交于 2019-12-03 09:45:35

You say

Gmail >> Message >> Open attachment in my application >> ImportActivity >> RootActivity

but that may not be right. In this circumstance, gmail will issue an Intent targeted at the ImportActivity in your app. ImportActivity will execute. However, my reading of https://developer.android.com/guide/components/tasks-and-back-stack.html suggests that ImportActivity will execute as part of the same task as gmail and will be put on top of the back stack for the gmail task, unless you take special steps in the manifest to prevent that or gmail specifically invokes it as a separate task. When ImportActivity finishes, it shouldn't call startActivity(intentForRootActivity) but should just call finish() so that it will be destroyed and the activity from gmail which lies underneath it in the back stack will appear.

If ImportActivity did call startActivity(intentForRootActivity) then RootActivity would just go onto the top of the gmail task and appear on the gmail back stack. Touching home and then the launcher icon for gmail would see RootActivity reappear, hiding gmail underneath.

I think you need android:launchMode="standard" in the manifest declaration of ImportActivity.

The task which represents the older, stand-alone instance of your app will not be modified. If someone touches the launcher icon for your app, the old state of your app will be restored, unaffected by whatever happened in the gmail task.

The document http://developer.android.com/guide/components/processes-and-threads.html is rather vague about how Activities map onto processes here (causing David Wasser to comment on my earlier version of this answer) but it seems that the Activities of gmail will execute in one linux process with one linux user id and the ImportActivity will execute in another linux process with another user id. However, they can all form part of one task with one back stack.

ImportActivity will execute, as part of the gmail task, with the same effective Linux User ID as it would had it executed as part of your standalone app - and different from the Linux user ID that gmail executes with. This sounds unlikely and complicated but seems to be implied by https://developer.android.com/guide/components/fundamentals.html. That makes sense; if ImportActivity needs to get at, say, the user preferences stored for your app, it needs to read the preference file as if it were the user defined for your app, not the user defined for gmail.

I haven't tried executing any of this. If I have hold of entirely the wrong end of the stick, I'm sure someone will soon tell us!

You shouldn't need any special launchMode to do this. If your import activity launches the root activity with Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP this should do what you want. I'm guessing you have a problem with taskAffinity. Try this:

In your manifest, in the declaration for the importActivity add this:

android:taskAffinity=""

If this doesn't work, please post your manifest so we can look at it.

Pozzo Apps

I am really not sure if I got it, but if you want to go to your "root" activity with out loosing extra application stack, it would be Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP direct on flags at your intent, and with no extra configs on Manifest... and if you need to separate the task to create a new complete new stack than you can use Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP as you described.

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