FLAG_ACTIVITY_NEW_TASK clarification needed

flag_activity_new_task example
flag_activity_new_task manifest
flag_activity_new_task and flag_activity_clear_task
calling startactivity() from outside of an activity context requires the flag_activity_new_task flag
flag_activity_new_task android 9
flag_activity_new_task xamarin
android back stack
android:launchmode

So my problem is:

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

When I press back it brings Sceen2 and Screen1 again. I dont use any other intent flags or launch modes.

Could someone explain what's happening??

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.

jakk - If you didn't set any flags on the activities (A or B), than the behavior you are describing is WRONG.

And for all the ones which 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 a 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.

Intent, When using implicit intents, given such an arbitrary intent we need to know what to do with it. These are the current standard categories that can be used to further clarify an Intent via addCategory(String) . int, FLAG_ACTIVITY_NEW_TASK. From a button in activity A, launch activity B with a FLAG_ACTIVITY_NEW_TASK. 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.

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

Clarification required FLAG_ACTIVITY_NEW_TASK, Android has tons of errors related to activities and tasks. However, Google changed the behavior of tasks between OS versions and did not inform developers  Yes, you should use FLAG_ACTIVITY_NEW_TASK.Otherwise you may get unexpected behavior on some devices. As of today (March 25, 2017) the official guide linked in the question has this updated code snippet:

Try this. It works for me.

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);

FLAG_ACTIVITY_NEW_TASK Android M Error · Issue #25 , https://stackoverflow.com/questions/9772927/flag-activity-new-task-clarification-​needed (a bit old but valid). Also, could you post the logcat so  The requirement for the first in a series of PendingIntents to have Intent.FLAG_ACTIVITY_NEW_TASK is the issue. Note that a series of pending intents can have the same context as the initial pending intent using the "getActivities" method. You are correct that it is not possible to do what you want only using flags.

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.

Intent.FLAG_ACTIVITY_NEW_TASK is doing What we think it should , But it seems that using FLAG_ACTIVITY_NEW_TASK doesn't Creates a new task always, it is only creating a new task if there is no task available for the activity we need to run like when we start AnyOne Who Can Clarify? FLAG_ACTIVITY_CLEAR_TASK. If set in an Intent passed to Context.startActivity (), this flag will cause any existing task that would be associated with the activity to be cleared before the activity is started. That is, the activity becomes the new root of an otherwise empty task, and any old activities are finished.

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.

android application launching another application and waiting for , reference you can see FLAG_ACTIVITY_NEW_TASK clarification needed Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); intent.setFlag(​Intent. One obvious difference between this and FLAG_ACTIVITY_CLEAR_TOP | FLAG_ACTIVITY_SINGLE_TOP is that FLAG_ACTIVITY_CLEAR_TASK needs FLAG_ACTIVITY_NEW_TASK. But, other than that, it would seem like the net effects are the same, and also match FLAG_ACTIVITY_RESET_TASK_IF_NEEDED .

make a call answer, reference you can see FLAG_ACTIVITY_NEW_TASK clarification needed Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); intent.setFlag(​Intent. Here is a simple clarification regarding the usage of Flag_Activity_Clear_Top. One Liner Formula for its right use : It always clears the intermediate Activities ( if any ) between the calling activity and the called activity. Here is an Activity Flow : Activity A -> Activity B -> Activity C ( use flag activity clear top to go to D) ->Activity D

Intent.flag_ACTIVITY_NEW_TASK is doing what we , But it seems that using FLAG_ACTIVITY_NEW_TASK doesn't Creates a New Task Always, it is only Creating a new task if there is no task available for the activity we need to run like when we start AnyOne Who Can Clarify? A task is a collection of activities that users interact with when performing a certain job. The activities are arranged in a stack—the back stack )—in the order in which each activity is opened. For example, an email app might have one activity to show a list of new messages. When the user selects a message, a new activity opens to view

Intent.FLAG_ACTIVITY_NEW_TASK is doing What we think , But it seems that using FLAG_ACTIVITY_NEW_TASK doesn't creates a new task Always, it is only creating a new task if there is no task available for the activity we need to run like when we start an activity from a Anyone who can clarify? Looks like Every tutorial that we have got out there on the Web mentions that FLAG_ACTIVITY_NEW_TASK Starts a new task if the Activity we are starting is not currently running in the task.But it seems that using FLAG_ACTIVITY_NEW_TASK doesn't Creates a New Task Always, it is only Creating a new task if there is no task available for the activity we need to run like when we start an activity

Comments
  • 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 :-)
  • 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?
  • 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
  • This has been changed now: developer.android.com/reference/android/content/… I suggest you to update your answer accordingly :)
  • So the problem is within the system? :)
  • as far as I can tell, there is no problem :-) the system behaves as documented.
  • 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.
  • 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.
  • I don't think a new task will be created in this case (since the activity already exists in a separate Task)
  • 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).