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

ViewModels: Persistence, onSaveInstanceState()...

ViewModels: Persistence, onSaveInstanceState(), Restoring UI State

ViewModels are designed to hold and manage UI-related data in a life-cycle conscious way.

Nsikak Thompson

February 18, 2018
Tweet

More Decks by Nsikak Thompson

Other Decks in Technology

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