The Android Lifecycle cheat sheet — part IV : ViewModels, Translucent Activities...
source link: https://www.tuicool.com/articles/hit/qq2euuM
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
ViewModels
The lifecycle of ViewModel
s is quite simple: they have only one callback: onCleared
. However, there’s a difference between scoping to an activity or to a fragment:
Note that the initialization happens whenever you obtain the ViewModel
, which is normally done in onCreate
.
:arrow_down: 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:
Also, when coming back to a task, both activities are restored and started, and only the translucent is resumed:
:arrow_down: 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:
For the sake of comparison, here’s what singleTask
would look like (but you probably shouldn’t use it):
Note: If you use Jetpack’s Navigation Architecture Component , you will benefit from Single Top support and automatic synthetic back stack.
:arrow_down: 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.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK