FLAG_ACTIVITY_NEW_TASK clarification needed

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.

  • Android AlertDialog box WindowManager$BadTokenException problem
  • How to add a button control to an android xml view at runtime?
  • Using Android L animations for switching between activities
  • ListView with CHOICE_MODE_MULTIPLE using CheckedText in a custom view
  • How to open Settings of Android Phone on a button click in our Android App
  • How do NFC payments work?
  • Could someone explain what’s happening??

    Related posts:

    Location of sqlite database on the device
    RSA Encryption Decryption in Android
    cordova platform add android gives the error: “Error: The provided path is not an Android ...
    Android Volley + JSONObjectRequest Caching
    how do you pass images (bitmaps) between android activities using bundles?
    ListViewDraggingAnimation broken on Android 5 Lollipop
  • Android read text raw resource file
  • How can an Android Service know that its not bound to any Activities
  • Using Hardware Layer in Custom View onDraw
  • Recommended text size ratio between mobile and tablet devices?
  • Using Android to submit to a Google Spreadsheet Form
  • Android Studio: Duplicate files copied in APK META-INF/DEPENDENCIES when compile
  • 6 Solutions collect form web for “FLAG_ACTIVITY_NEW_TASK clarification needed”

    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.

    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

    Try this it works for me.
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |Intent.FLAG_ACTIVITY_CLEAR_TASK);
    startActivity(intent);

    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.

    FLAG_ACTIVITY_NEW_TASK

    Start the activity in a new task. If a task is already running for the activity you are now starting, that task is brought to the foreground with its last state restored and the activity receives the new intent in onNewIntent().

    This produces the same behavior as the “singleTask” launchMode value, discussed in the previous section.

    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.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.