• The Android Lifecycle Cheat Sheet — Part IV: ViewModels, always Activities and Launch Modes
  • Jose Alcerreca
  • The Nuggets translation Project
  • Permanent link to this article: github.com/xitu/gold-m…
  • Translator: xiaxiayang
  • Proofread by: Phxnirvana

Articles in this series:

  • Part one: Activity– Life cycle of a single activity
  • Part 2: Multiple Activities– Jump and return to the back stack
  • Part 3: Fragment– Life cycles of activities and fragments
  • Part 4: ViewModel, Translucent Activity, and Launch Mode (this article)

To make this easier, check out the PDF version of the chart memo.

ViewModel

The ViewModel lifecycle is very simple: it has only the onCleared callback. However, the scope of this function is different between activities and fragments:

The ViewModel scope

Note that initialization is done when the ViewModel is retrieved, usually in the onCreate method.

Download the ViewModel diagram

Translucent Activity

Translucent activities have a translucent (usually transparent) background, so the user can still see what is underneath the activity.

When an activity set up the theme of the android: windowIsTranslucent properties, a slight change of the life cycle: the background behind the activity will not be stopped, will only be suspended, so you can continue to receive the UI update:

A comparison between a regular activity and a translucent activity

In addition, when returning to a task, both activities are resumed, reclaiming the onRestart and onStart methods, but only the translucent activity reclaiming the onResume method:

Press the home button to return to the application with the translucent Activity

Download the translucent Activity diagram

Boot mode

The recommended methods for handling tasks and rolling back the stack are: don’t handle — you should use the default behavior. For more details, read Ian Lake’s article on this topic: Tasks and Rollback.

If you really need to use SINGLE_TOP, here’s how it behaves:

Single Top behavior mode

For comparison purposes, here’s what the singleTask pattern looks like (but you probably shouldn’t use it) :

Single Task

Note: If you use the Navigation Architecture Component in Jetpack, you will benefit from its support for Single Top and auto-compositing fallback stacks.

Download the startup mode chart

If you find any mistakes in your translation or other areas that need to be improved, you are welcome to the Nuggets Translation Program to revise and PR your translation, and you can also get the corresponding reward points. The permanent link to this article at the beginning of this article is the MarkDown link to this article on GitHub.


The Nuggets Translation Project is a community that translates quality Internet technical articles from English sharing articles on nuggets. The content covers Android, iOS, front-end, back-end, blockchain, products, design, artificial intelligence and other fields. If you want to see more high-quality translation, please continue to pay attention to the Translation plan of Digging Gold, the official Weibo, Zhihu column.