FLAG_ACTIVITY_NEW_TASK clarification needed
Asked Answered
H

6

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??

Histogram answered 19/3, 2012 at 15:25 Comment(0)
I
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.

Irretrievable answered 15/6, 2012 at 16:9 Comment(4)
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 :-)Helainehelali
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?Excitation
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..WTAFDehlia
This has been changed now: developer.android.com/reference/android/content/… I suggest you to update your answer accordingly :)Gyroscope
T
19

The following code snippet works:

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
Trondheim answered 31/10, 2015 at 6:13 Comment(0)
H
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

Helainehelali answered 24/8, 2012 at 15:36 Comment(0)
B
3

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.

Breeding answered 19/3, 2012 at 15:58 Comment(4)
So the problem is within the system? :)Histogram
as far as I can tell, there is no problem :-) the system behaves as documented.Breeding
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.Helainehelali
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.Hanaper
S
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.

Sheriesherif answered 4/2, 2019 at 14:33 Comment(0)
D
-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.

Dejadeject answered 19/3, 2012 at 15:55 Comment(2)
I don't think a new task will be created in this case (since the activity already exists in a separate Task)Breeding
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).Helainehelali

© 2022 - 2024 — McMap. All rights reserved.