# Android Architecture Component [TOC] ## ViewModel https://www.youtube.com/watch?v=orH4K6qBzvE&ab_channel=Stevdza-San ViewModel Scope ![](https://i.imgur.com/mHLr3zW.png) Separates UI from data, which helps to avoid memory leak. It is not used for data persistence by itself, it is used in combination with unsaved instance state and local database. onCleared() > called before your app process is killed by the Android OS. ViewModels should **NOT HOLD** a reference to activities, fragments, contexts, nor UI controllers(Views). But application context is fine for system services, as it is tied to application lifecycle. If Application context is needed then extends to AndroidViewModel Class instead. ### ViewModelFactory: [Pass Variables to ViewModel](/IiJGuPu5TviWtBqL_K3UQA) ### SharedViewModel 1. [SharedViewModel Between Fragments](/pyEU0BpvRNiRw6NaNR4o6A) 2. ## LiveData Note: https://www.youtube.com/watch?v=suC0OM5gGAA&ab_channel=Stevdza-San ### Introduction LiveData is an observable holder class, unlike a regular observable this one can be aware of the lifecycle(Activity/Fragment/Services). So LiveData only updates observers, when they are in active state. If Activity is Paused or destroyed, it will not update the observer. It will wait for it to come to live to update it. In simple words, **LiveData is data that is aware of the lifecycle of it's observer.** It will just send updates when data changes and only to live observers. **onPause && onDestroy** ![](https://i.imgur.com/2ian9jq.png) **Observer come to live** ![](https://i.imgur.com/tupJUL9.png) ### Advantages 1. No Memory Leak 2. No crashes due to stopped activities 3. No more manual lifecycle handling 4. Always up to date data