Having One Instance Of Activity

2019-03-23 15:51发布

问题:

I have a problem with Activity navigation and the back stack and I was hoping you could clear it up for me.

The Problem

Root Activity >>> SecondActivity >> HomeButton

This then takes me to the home page and from there I select...

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

The outcome is a new task being started and another instance of my application being used. This is unacceptable as I don't want two separate tasks running I only want one.

The Desired Outcome

What I want to happen is when the user opens the attachment and ImportActivity finishes what its doing (it has android:noHistory set to true) and calls startActivity(intent), RootActivity is started, but keeping only one instance and the rest of the activities on top of it in the original task (in this case SecondActivity)are removed.

The reason I want this is if the user navigates out of my application after importing the file and then touches the app icon it loads the first task with its back stack and I have two tasks running in which the user can be in two different parts of my application at once.

What I have tried

I have played around with the launch mode but none of them really give me the functionality that I need.

Launch Modes I have tried..

android:launchMode="singleTask" - this just started the root activity again each time it was launched. Even if the user pressed the home button inside my application and touched the App icon the back stack was destroyed.

android:launchMode="singleInstance = doesn't allow any other activites to run in the task.

Also when calling the intent to start the RootActivity I have used the following to no avail.

Intent i = new Intent(ImportActivity.this,TrackingActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);

Is it possible to do what I want?

Thanks in advance

回答1:

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!



回答2:

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.



回答3:

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.