22

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

4
  • 1
    Have you tried using the Single Top flag? Dec 12, 2012 at 19:34
  • 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. Dec 15, 2012 at 2:00
  • Use Intent.FLAG_ACTIVITY_CLEAR_TASK when you launch your TrackingActivity Dec 17, 2012 at 13:42
  • Have you tried my suggestion regarding taskAffinity? Dec 19, 2012 at 13:52

3 Answers 3

5
+25

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!

5
  • 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. Dec 17, 2012 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, 2012 at 16:46
  • 1
    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 Dec 17, 2012 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, 2012 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. Dec 17, 2012 at 17:20
1

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.

0

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.

5
  • 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 Dec 12, 2012 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, 2012 at 19:54
  • 1
    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" Dec 12, 2012 at 19:58
  • Did you try with android:launchMode="singleInstance" on your AndroidManifest.xml ?
    – vsm
    Dec 12, 2012 at 21:16
  • Yea the problem with that is then this activity will be in a task all by itself... Dec 14, 2012 at 9:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.