30

So my problem is:

  • I start App1, open Screen1 and then Screen2.
  • I press Home, leaving App1 in the background.
  • I open App2 and start App1.Screen1 with FLAG_ACTIVITY_NEW_TASK expecting to be on App1.Screen2 in the previously left task. Instead, I'm on App1.Screen1 and the system called onNewIntent().

When I press back, it brings Screen2 and Screen1 again. I don't use any other intent flags or launch modes.

Could someone explain what's happening??

Adil Hussain
  • 30,049
  • 21
  • 112
  • 147
jakk
  • 1,193
  • 2
  • 12
  • 28

6 Answers6

52

Android has TONS of bugs related to activities and tasks.

Nevertheless, Google changed the behavior of tasks between OS versions and didn't notify the developers, which is the most annoying thing about it.

If you didn't set any flags on the activities (A or B), then the behaviour you are describing is WRONG.

And for all those who say that there is no problem with the documentation, try this:

  1. Create an application with Activity A (launching activity) & B (with the default launch mode for both).
  2. Start the application - a task is created with activity A only.
  3. From a button in activity A, launch activity B with FLAG_ACTIVITY_NEW_TASK.
  4. Click the button several times and you'll see that activity B is created multiple times inside the task, which is NOT as the documentation says.

There are more scenarios to prove that the documentation is BAD / WRONG.

Adil Hussain
  • 30,049
  • 21
  • 112
  • 147
  • 20
    This happens because `taskAffinity` takes precedence over `FLAG_ACTIVITY_NEW_TASK`. If you want ActivityB to be in a different task than ActivityA you need to give it a different `taskAffinity`. But yes, I agree, there are tons of bugs related to activities and tasks :-) – David Wasser Aug 24 '12 at 15:49
  • As per docs, it seems like if we start an activity with intent ( FLAG_NEW_TASK) , and if the activity exist in other task, the complete task will be brought to the foreground above the existing task.as there is nothing specified about the position of the target activities , it seems position of the activity in back stack doesn't matter . Is my conclusion right? – smasher Jun 23 '16 at 16:27
  • A little late to the party but its completely abhorrent that the issue remains. You are absolutely right . NEW_TASK doesn't work as documented and I am not sure why are people even arguing with you about it . It is simple , launch activity A and B . Then from B launch A with NEW TASK flag. According to the docs , if A exists in the task , it wont be launched again . Yet it does..WTAF – Muhammad Ahmed AbuTalib Feb 28 '19 at 23:40
  • This has been changed now: https://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_NEW_TASK I suggest you to update your answer accordingly :) – Jimit Patel Apr 12 '19 at 06:06
17

The following code snippet works:

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
lwei
  • 55
  • 2
  • 6
balakumar
  • 171
  • 1
  • 2
10

In your 3rd step when you open App2 and start App1.Screen1 with Intent.FLAG_ACTIVITY_NEW_TASK you need to also set Intent.FLAG_ACTIVITY_SINGLE_TOP to get this to do what you want. It's an Android bug :-(

Be also aware that the behaviour is also a bit broken if you launch your app for the first time from your IDE (IntelliJ, Eclipse), or after installing it via the market (Google Play) or from a browser download. See How to prevent multiple instances of an activity when it is launched with different intents and http://code.google.com/p/android/issues/detail?id=26658

Community
  • 1
  • 1
David Wasser
  • 93,459
  • 16
  • 209
  • 274
3

We have to use two flags namely:

  1. Intent.FLAG_ACTIVITY_NEW_TASK
  2. Intent.FLAG_ACTIVITY_CLEAR_TASK

The first flag will simply add an activity to lifecycle just as an element is added to stack.

The second flag will simply remove the first activity from the stack so that when the user navigates back to home screen, he will not be able to see the activity and will only see the second activity.

Elletlar
  • 3,136
  • 7
  • 32
  • 38
2

This seems to be in line with the documentation on http://developer.android.com/guide/topics/fundamentals/tasks-and-back-stack.html.

FLAG_ACTIVITY_NEW_TASK is equivalent to launchMode=singleTask and in there I read

However, if an instance of the activity already exists in a separate task, the system routes the intent to the existing instance through a call to its onNewIntent() method, rather than creating a new instance

Starting the activity without FLAG_ACTIVITY_NEW_TASK should give you the expected behavior.

Marc Van Daele
  • 2,856
  • 1
  • 26
  • 52
  • So the problem is within the system? :) – jakk Mar 26 '12 at 10:31
  • as far as I can tell, there is no problem :-) the system behaves as documented. – Marc Van Daele Mar 26 '12 at 11:18
  • This isn't correct. The doc. indicates that a new instance of the activity is **not** created and that `onNewIntent()` is called on the existing instance of the activity. This isn't what is happening (actually a new instance of the activity **is being created**). Not only that, if you think about this it makes no sense: If I have a task that contains ActivityA with ActivityB **on top of ActivityA**, how can I possibly call `onNewIntent()` on the existing instance of ActivityA? It isn't the top activity soit cannot react and to make it the top activity would require reordering the stack. – David Wasser Aug 24 '12 at 16:10
  • You need to use FLAG_ACTIVITY_MULTIPLE_TASK in conjunction with FLAG_ACTIVITY_NEW_TASK if you want a new instance of teh activity to be runnable in another task. This can cause problems though as I can't figure out how to go back to the old task from the new task, if the app goes to the background and is then restored. – speedynomads Feb 12 '15 at 12:34
-2

The FLAG_ACTIVITY_NEW_TASK places your new activity on a new task stack. I'm going to refer to the activities as A and B.

When you launch the first app, you have a single task with A in it. Task 1 = A

Clicking on the second activity puts B in the task. Task 1 = AB

When you click home you preserve the task. Task 1 = AB (still)

Opening the second app and send the A intent with the new task flag a NEW task will be created with only A on it. Now you have two tasks. Task 1 = AB, Task 2 = A

Unwrapping this arrangement with the back key will result in 2A, 1B, 1A which is what you are seeing. This is the expected result.

drasticp
  • 287
  • 3
  • 10
  • 2
    I don't think a new task will be created in this case (since the activity already exists in a separate Task) – Marc Van Daele Mar 19 '12 at 16:00
  • This isn't true. When you are running the second app and send the A intent with FLAG_ACTIVITY_NEW_TASK, it brings the existing task to the foreground (as it should) and then creates another instance of Activity A in that task (this is the incorrect behaviour). It doesn't work like it is supposed to work (or like it is documented). – David Wasser Aug 24 '12 at 16:05