Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Android architecture component

Android architecture component

Android architecture component - LifeCycleEvent , LiveData and ViewModel session Presented at Simform Solution

Avatar for Sanket Kachhela

Sanket Kachhela

February 21, 2018

Other Decks in Technology

Transcript

  1. • Handling lifecycle event • Crashes due to stopped activities

    • Memory leak • Handling configuration changes • boilerplate code Common problems
  2. LiveData<T> • observable data holder
 (LiveData is an observable data

    holder class.) • lifecycle-aware observable
 (Unlike a regular observable, LiveData is lifecycle-aware, meaning it respects the lifecycle of other app components, such as activities, fragments, or services) • Manage Subscription itself
 (LiveData only notifies to active observer only if it’s activity state in STARTED or RESUMED)
  3. LiveData Methods • onActive()
 The onActive() method is called when

    the LiveData object has an active observer. This means you need to start observing the CRYPTO PRICE updates from this method. • onInactive()
 The onInactive() method is called when the LiveData object doesn't have any active observers. Since no observers are listening, there is no reason to stay connected to the CRYPTO MANAGER service. • setValue(T)
 The setValue(T) method updates the value of the LiveData instance and notifies any active observers about the change. • postValue(T)
 The postValue(T) method works similarly as setValue(T) but this method useful while in worker/ background thread.
  4. What is issue with this ? • Manually handling lifecycle

    event • memory leak • Unnecessary use of data pack
  5. LifeCycle Monitor Activity Created OnCreate API Call OnStart OnResume Activity

    Recreated OnCreate API Call OnStart OnResume Rotate Device OnPause OnStop OnDestroy
  6. LifeCycle Monitor Activity Created OnCreate API Call OnStart OnResume Activity

    Recreated OnCreate API Call OnStart OnResume Rotate Device OnPause OnStop OnDestroy
  7. Yes Is this good solution ? Will there be memory

    leak ? No Can we make better ?
  8. Yes Is this good solution ? Will there be memory

    leak ? No Can we make better ? Yes
  9. Problem Activity Created OnCreate API Call OnStart OnResume Activity Recreated

    OnCreate API Call OnStart OnResume Rotate Device OnPause OnStop OnDestroy 2 API CALL
  10. Solution Activity Created OnCreate API Call OnStart OnResume Activity Recreated

    OnCreate API Call OnStart OnResume Rotate Device OnPause OnStop OnDestroy
  11. ViewModel • Viewmodel class is designed to store and manage

    UI-related data in a lifecycle conscious way. • Viewmodel can save large data as compare to onSavedInstance(). • it can survive configuration changes. • clear automatically when activity destroyed • don’t reference your view inside view model • Now it can be GOAL KEEPER to call an APIs.