If you ever created an app that has more than 2 activities, it’s likely that you needed to change the launch mode for some of them in order to achieve that specific behavior you wanted to have. I must admit that the whole thing was a bit confusing to me, and I needed to visualize everything in order to fully understand what happens with the back stack when you change the screens you’re looking at. Thankfully, there was a lot of help online (also a lot of useless stuff..), so I finally decided to create a simple app to demonstrate how it actually works and what happens inside.

The app binary is located here, and the source is located here. I wrote it in Kotlin as it was especially quick and compact, but you’ll get the idea of how it works even if you don’t know how to read Kotlin - everything is extremely simple.

So.. the app shows the activity title on top, describing what kind of activity you’re currently in, and then displays that activity’s Intent origin (where it was started from). It also shows the current activity’s task ID, and the corresponding task ID for the origin activity.

Below that, inside of each activity there’s a list of currently active tasks from the OS (well, the ones we could access with the new API), so there you can see what exactly happens when you change to another activity. You should only be interested in the launcher task and your app’s tasks.

To change (go) to another type of activity, just use one of the buttons at the bottom - options are: default, singleTop, singleTask, singleInstance. You can read more about the tasks and back stack from the official docs, or take a look at this explanation video presented by Google’s Ian Lake.

Anyway, to illustrate a simple task/stack demo, I created this video using the app mentioned above by going through various scenarios, recording all of that and then wasting a few hours on preparing the video. Sorry about the irritating background music though, I really didn’t like the silence :)