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.
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:
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:
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