Sarthak Sharma
Sarthak Sharma

Reputation: 137

android - Two activities with launch modes as 'singleInstance'

Suppose there are two activities in a single android application with launch modes as 'singleInstance'. Assume an example below.

I am navigating from activity A -> B(launchMode="singleInstance"). Now from activity B -> C. Finally, I navigate from activity C -> D(launchMode="singleInstance").

Now we know instance of activity B will be created in a different task, and A & C will be in the different task.

Now, my question is, in which task instance of activity D would be placed. Will it be with activity B, or some other task would be created for activity D.

Thanks.

Upvotes: 4

Views: 1058

Answers (1)

Jackey
Jackey

Reputation: 3234

I'll bold the answer to your question if you don't want to read the clarification.

When using launchMode="singleInstance", there's two things to keep in mind:

  • The Activity will always be created in a new task
  • All Activities launched from this Activity will be created in a separate task

As such, an Activity with launchMode of singleInstance will always be isolated in it's own task. There won't be another Activity inside of that task.

So with your example from your question of Activities A, B, C, and D:

  • Activity A launches Activity B
  • Activity B is launchMode="singleInstance" so it's on a new task
  • Activity B launches Activity C
  • Activity C is launched in the same task as Activity A
  • Activity C launches Activity D
  • Activity D is launchMode="singleInstance" so it's on a new task

From what happened here, you have one a task that stores the launchMode="standard" Activity A and Activity C. Activity B is in it's own task. Activity D is in it's own task.

Therefore, if you choose to Back out of these Activities, you'll notice that:

  • Activity D is backed and Activity C appears
  • Activity C is backed and Activity A appears

This happens because Activity C is on the same task as Activity A.

Also, Activity D definitely won't be in the same task as Activity B because Activity B's task is meant only for Activity B due to launchMode="singleInstance".

Keep in mind that there can be any number of tasks being held in the background at once. Just that if there's too many being held or if the system requires memory, it'll start destroying these background Activities across your multiple tasks.

Upvotes: 5

Related Questions