avatarJose Alcérreca

Summary

This webpage provides a detailed explanation of Android Lifecycle topics, including ViewModels, Translucent Activities, and Launch Modes.

Abstract

This webpage is part IV of "The Android Lifecycle cheat sheet" series, focusing on ViewModels, Translucent Activities, and Launch Modes. The content explains the lifecycle of ViewModels and their scoping to either an activity or a fragment. Additionally, it covers Translucent Activities, activities with translucent or transparent backgrounds, and their impact on the Android lifecycle. Lastly, the post discusses Launch Modes and the recommended approach to managing tasks and back stacks, emphasizing the default behavior. It also includes diagrams in PDF format for quick reference.

Bullet points

  • The series covers topics like single activity lifecycle, multiple activities navigation, and fragment lifecycle.
  • This post focuses on ViewModels, Translucent Activities, and Launch Modes.
  • ViewModels have a simple lifecycle with one callback: onCleared.
  • ViewModel initialization happens during the ViewModel's acquisition, typically in onCreate.
  • Translucent Activities have translucent or transparent backgrounds, allowing the user to see the background activity.
  • When using android:windowIsTranslucent, the background activity is paused but not stopped, allowing it to continue receiving UI updates.
  • The recommended way to manage tasks and back stacks is to adopt default behavior.
  • The post includes diagrams for ViewModels, Translucent Activities, and Launch Modes.
  • Downloadable diagrams are available in PDF format.

The Android Lifecycle cheat sheet — part IV : ViewModels, Translucent Activities and Launch Modes

In this series: * Part I: Activities — single activity lifecycle * Part II: Multiple activities — navigation and back stack * Part III: Fragments — activity and fragment lifecycle * Part IV: ViewModels, Translucent Activities and Launch Modes (this post)

The diagrams are also in PDF format for quick reference.

ViewModels

The lifecycle of ViewModels is quite simple: they have only one callback: onCleared. However, there’s a difference between scoping to an activity or to a fragment:

ViewModel scoping

Note that the initialization happens whenever you obtain the ViewModel, which is normally done in onCreate.

⬇️ Download ViewModels diagram

Translucent Activities

Translucent activities have translucent (usually transparent) backgrounds so the user can still see what’s underneath.

When the property android:windowIsTranslucent is applied to an activity’s theme, the diagram changes slightly: the background activity is never stopped, only paused, so it can continue receiving UI updates:

Comparison between regular and translucent activities

Also, when coming back to a task, both activities are restored and started, and only the translucent is resumed:

Pressing home and coming back to an app with a translucent activity

⬇️ Download Translucent activities diagram

Launch Modes

The recommended way to deal with tasks and the back stack is, basically: don’t you should adopt the default behavior. For more details, read Ian Lake’s post about this topic: Tasks and Back Stack.

If you really need to use SINGLE_TOP, here’s its diagram:

Single Top behavior

For the sake of comparison, here’s what singleTask would look like (but you probably shouldn’t use it):

Single Task

Note: If you use Jetpack’s Navigation Architecture Component, you will benefit from Single Top support and automatic synthetic back stack.

⬇️ Download launch modes diagram

If you find errors or you think something important is missing, please report them in the comments. Also, let us know what other scenarios you would like us to write about.

Android App Development
Lifecycle
Android
Diagrams
Featured
Recommended from ReadMedium