Memahami ViewModel & LiveData
Lebih Dalam
Alfian Yusuf Abdullah
Google Associate Android Developer, Code Reviewer Dicoding
ViewModel
Part of Android Jetpack
The ViewModel class is designed to store and manage UI-related data in a
lifecycle conscious way. The ViewModel class allows data to survive
configuration changes such as screen rotations.
https://developer.android.com/topic/libraries/architecture/viewmodel
Android Jetpack
ViewModel Overview
Lifecycle of ViewModel
Android Jetpack
ViewModel Implementation
Android Jetpack
Android Jetpack
Android Jetpack
Android Jetpack
Don’t save all about Context
References!
Android Jetpack
And how to access Context?
Android Jetpack
Use AndroidViewModel!
Android Jetpack
Android Jetpack
LiveData
Part of Android Jetpack
● Ensures your UI matches your data state
● No memory leaks
● No crashes due to stopped activities
● No more manual lifecycle handling
● Always up to date data
● Proper configuration changes
Android Jetpack
The advantages of using LiveData
LiveData Implementation
Android Jetpack
Android Jetpack
Some tips when use
LiveData
Android Jetpack
Don’t use Fragment as LifecycleOwner of LiveData
Android Jetpack
Don’t use Fragment as LifecycleOwner of LiveData
Android Jetpack
Don’t exposing LiveData as mutable to views
Android Jetpack
Don’t exposing LiveData as mutable to views
Android Jetpack
Don’t exposing LiveData as mutable to views
Android Jetpack
Don’t exposing LiveData as mutable to views
Android Jetpack
Don't put observe function at the event handling
Android Jetpack
Don't put observe function at the event handling
Android Jetpack
Contact:
@alfianyabdullah
THANKS!
Follow us: @dicoding

Dicoding Developer Coaching #24: Android | Memahami ViewModel & LiveData Lebih Dalam