kingston
kingston

Reputation: 11419

Android FLAG_ACTIVITY_NEW_TASK is ignored

is there a case when FLAG_ACTIVITY_NEW_TASK is ignored? From a service I start an activity with an intent that sets that flag and when the main activity is in the background I was expecting to see the new activity in a new task and so I was expecting to see the application in the background again when I close the new activity. It does not happen and the main activity is shown.

Just for testing I tried to set the main activity as singleInstance and in that case it works. I do not want to set the main activity as singleInstance though.

Do you know when this can happen?

I should add that I see this behaviour on all the devices so it looks like it is the way it should work and not a device specific issue.

Upvotes: 3

Views: 2119

Answers (1)

kingston
kingston

Reputation: 11419

I fixed it setting a different affinity for the second activity. The problem is that the documentation about the FLAG_ACTIVITY_NEW_TASK flag states:

If set, this activity will become the start of a new task on this history stack

but this is misleading because it sounds like the framework will always create a new task, and this is not true when the activity is started from a service.

This is the documentation about the taskaffinity attribute of an activity:

android:taskAffinity The task that the activity has an affinity for. Activities with the same affinity conceptually belong to the same task (to the same "application" from the user's perspective). The affinity of a task is determined by the affinity of its root activity. The affinity determines two things — the task that the activity is re-parented to (see the allowTaskReparenting attribute) and the task that will house the activity when it is launched with the FLAG_ACTIVITY_NEW_TASK flag.

So what happens is that a new task is started if you call the startActivity from an activity but if you start it from a service, the framework will attach the activity to a task with the same affinity if any exists with the top activity that is not a singleInstance

Upvotes: 6

Related Questions