Android “launchMode” (Visualized)

Tram Ho

Android launchMode is important and all Android developers should know how launchMode works. Most developers (including me) think we know why it is used. But we were wrong. It is not just about creating a SingleTask operation to prevent duplication. OK you're right. It solves our problem but do we really know what happens to other operations in the stack when we perform the SingleTask operation?

First of all I want to give you two images to make it easier to understand. Green means new activity. Yellow means an instance of the same operation, but the onNewInten () method is called.

1 – launchMode = "singleTop" in Activity D

  1. Example If you define Activity D as singleTop in AndroidManifest.xml. D uses the same instance and the onNewIntent () method called in Activity D.

2.example If you define Activity D as singleTop in AndroidManifest.xml. New activity is created.

3. Example If you identify Activity D as singleTop in AndroidManifest.xml. D uses the same instance and the onNewIntent () method if and only if it was at the top of the task. If it is not on the task, a new Activity D instance will be created.

2 – launchMode = "singleTask" in Activity C

Do you find Activity D destroyed? and the onNewIntent () method used in Activity C.

New activity C is launched.

3 – launchMode = "singleInstance" in Activity E

Gosh!? It's correct. A, B, C, D are in a mission. E will be created in another mission.

It's correct. Activity E is created in the new task. What if we want to launch Activity F from Activity D? This is the result;

E is still on another mission. a, b, c, d and f are on another mission.

Activity E is still in a different mission. And if we start over, the onNewIntent () method is called instead of creating a new activity.

4 – launchMode = "standard" in Activity B

Create new activity B. And yes I know we already have a B in the stack. But it doesn't use that.

Happy coding.

Link reference
Share the news now

Source : Viblo