I am finding it hard to understand how the Android behaves with tasks and activities and need a solution on this one particular problem of mine.
I have a TabActivity A with singleTask as launchMode in manifest, and is the launcher as well. This internally starts two other Activities B, C upon visiblity(or clicks) of respective tabs. Now considering that activity B was started along with A, the task back stack becomes
Task 1 [ A B ]
I also happen to run a Service (X) in background. After sometime, on some event, Service X opens a new Activity D ( which is not part of the tab activity’s specs ). Activity D is specified launchMode as singleTask and affinity different than the package. Since services can directly start the activities, I had to provide FLAG_ACTIVITY_NEW_TASK. All goes well and the activity D is created and displayed.
With the explainations from developer.android.com about the tasks creation and all, the new Activity D should be created in a new task of its own. Thus the whole viewpoint should look like
[Background Task 1 [ A B ]][Foreground Task 2 [ D ]]
Now there is a way provided in activity D to go to Activity A and the intent flag is set to FLAG_ACTIVITY_CLEAR_TOP.
With my understanding, this flag should clear all the activities above A that are in the task of which A is the root activity. Meaning it should destroy activity B only. i.e. the viewpoint should look like
[Background Task 2 [ D ]] [Foreground Task 1 [ A ]]
But in my case, it is destroying activity D as well, which is definetly not the behaviour i want since i want the Acitivity D to be present all the time unless explicitly finished by user. After navigating, the stack looks like
[Foreground Task 1 [ A ]].
Am I doing something wrong here? If the above behavior is the expected one in Android, then is there a way where I can start A from D without clearing D but finishing all other activities between A & D?
The problem turned out to be two declarations of Activity D in AndroidManifest.xml.
Removed the extra declarations and its now working perfectly.