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

viewmodels-persistence-onsaveinstancestate-rest...

 viewmodels-persistence-onsaveinstancestate-restoring-ui-state

Here, Thompson Nsikak Provides a primary understanding of the MVVM Pattern with most insights on ViewModels and how they work with persistence, onSavedInstanceState() and RestoringUI state.

Android Nigeria Community

February 19, 2018
Tweet

More Decks by Android Nigeria Community

Other Decks in Programming

Transcript

  1. Profile I am Nsikak Thompson I build for Android A

    community Advocate Technical Writer @medium/droidplate @thompson_nsikak
  2. TOC Overview Problems to solve Project objective Market trends Trend

    analysis Target audience Proposed solution Process Deliverables Vision Team Next Billion Users
  3. How do I use ViewModels to save and restore UI

    state efficiently? You use a combination of ViewModels, onSaveInstanceState() and local persistence.
  4. Do ViewModels persist my data? ViewModels hold transient data used

    in the UI but they don’t persist data. Once the associated UI Controller (fragment/activity) is destroyed or the process is stopped,
  5. The lifecycle of a ViewModel The ViewModel remains in memory

    until the Lifecycle it's scoped to goes away permanently:
  6. App killed - Low memory - Shut down due to

    resource constraints - device runs out of battery
  7. ViewModel vs SaveInstanceState onSaveInstanceState(): This callback is meant to retain

    a small amount of UI related data in two situations: 1. The app’s process is stopped when it’s in the background 2. Configuration Changes
  8. Recap • Local persistence is used for storing all data

    you don’t want to lose if you open and close the activity. • ViewModels are used for storing all the data needed to display the associated UI Controller. • onSaveInstanceState is used for storing a small amount of data needed to easily reload activity state if the UI Controller is stopped and recreated by the system. E.g search query