RookieGuy
RookieGuy

Reputation: 517

Launching an Activity from the background without attaching it to the task stack of existing activities of the same app

I am launching an activity A from a service in the background, this works well and the activity is created and usable with the code below:

        Intent intent = new Intent(context, getActivityClass());
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);

This however has an unwanted behaviour if there is already a running activity B of the app containing activity A in the task list. The user would expect to get back to the activity that was in the front when my activity A was started from the background if he uses the back button, however it navigates back to activity B instead which really is confusing.

After reading docs I tried to allow task reparenting which did not work and still shows the problem described above.

    <activity
        android:name="net.x.y.z.PickContactActivity"
        android:allowTaskReparenting="true"
        android:theme="@android:style/Theme.Translucent.NoTitleBar">
    </activity>     

According to this question Need single activity in new task backstack the Intent.FLAG_ACTIVITY_MULTIPLE_TASK should be used, however it is mentioned in the comments on this to not use it unless implementing an app-launcher

Do not use this flag unless you are implementing your own top-level application launcher. Because the default system does not include graphical task management, you should not use this flag unless you provide some way for a user to return back to the tasks you have launched.

What do I then have to do to fix this? In short: the pick contact activity should simply be handled as a real new task without being associated with any other running instance of the app, just as i would open notepad two times on a windows pc

I'd really be glad to have good solution on this, thanks in advance

Upvotes: 0

Views: 834

Answers (1)

said
said

Reputation: 106

BY DEFAULT;two activities from the same app will appear in the same task;to change this behaviour add this in the Manifest xml for the activities A and B: android:taskAffinity="";and change android:allowTaskReparenting to "false"

Upvotes: 2

Related Questions