Take the 2-minute tour ×

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);

Is it possible to do what I want?

Thanks in advance

share|improve this question
Have you tried using the Single Top flag? –  Vinay S Shenoy Dec 12 '12 at 19:34
Yea tried that one too –  Disco S2 Dec 12 '12 at 19:41
What about RootActivity with singleTask mode, where you save its state in onPause(). When user returns to RootActivity, a new instance is created that reads the saved state. –  Gunnar Karlsson Dec 15 '12 at 2:00
Use Intent.FLAG_ACTIVITY_CLEAR_TASK when you launch your TrackingActivity –  Sherif elKhatib Dec 17 '12 at 13:42
Have you tried my suggestion regarding taskAffinity? –  David Wasser Dec 19 '12 at 13:52

3 Answers 3

You say


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!

share|improve this answer
Your statements at the beginning of this answer are wrong. You are confusing 2 completely different concepts: Android task is NOT an operating system process. The security model for Android uses unique user-IDs for each application and relies on the operating system's security mechanism. This means that your code will never run in the same process (memory) space as Gmail code and it will never have the same user-ID as Gmail (or any other application). The import activity may be in the same task stack as he Gmail application, this depends on how Gmail starts the activity. –  David Wasser Dec 17 '12 at 16:39
Yes, I agree with you that a task is not a process, and that user ids are application-specific, and that import's task stack can depend on how gmail sends the intent. Can you direct me to the doc that explains how Activities map onto processes? I couldn't find it. –  emrys57 Dec 17 '12 at 16:46
developer.android.com/guide/components/… The first paragraph states that components (Activity, Service, BroadcastReceiver, ContentProvider) of an application all run together in the same process (by default). You can specify in the manifest which components run in which process if you want to. A detailed description of the security model (which also explains a lot about components, processes, etc.) can be found here: source.android.com/tech/security/index.html –  David Wasser Dec 17 '12 at 16:58
Yes, thanks, I hunted that down and rewrote the answer while you were typing. Components of an application all run in the same process by default - but what happens to an Activity that is part of another application which isn't running at the time? It's not explicit. However, I agree your interpretation seems much more likely than my original one. Thanks for your correction. –  emrys57 Dec 17 '12 at 17:07
The first sentence is "When an application component starts and the application does not have any other components running, the Android system starts a new Linux process for the application with a single thread of execution." So this is clear. If I start one of your activities and your app isn't running, a new process gets created to host your activity. –  David Wasser Dec 17 '12 at 17:20

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:


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

share|improve this answer

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.

share|improve this answer
Sorry if I wasn't clear and I don't think you've answered it here. I want to lose my previous stack if the ImportActivity is called. From there I navigate to the RootActivity of the previous task. I don't want two separate tasks –  Disco S2 Dec 12 '12 at 19:49
android:launchMode="singleTask" at mainfest in ImportActivity. "The system creates the activity at the root of a new task and routes the intent to it. However, if an instance of the activity already exists, the system routes the intent to existing instance through a call to its onNewIntent() method, rather than creating a new one." –  Pozzo Apps Dec 12 '12 at 19:54
The activity I want to be at the root is RootActivity. I have tried singleTask on it, however it always seemed to create a new task instead of (like you quoted) "the system routing the intent to the existing instance" –  Disco S2 Dec 12 '12 at 19:58
Hmmm, ok, I'm sorry.... lets wait for someone with answear then :S –  Pozzo Apps Dec 12 '12 at 20:01
Thanks though Pozzo –  Disco S2 Dec 12 '12 at 20:04

Your Answer


