Persisting view-state when using a custom backstack: Creating a Flow-like custom backstack (Part 4)

Persisting the viewstate

Saving the view hierarchy state

The State class

  • the Key of the given view (to identify this state later when we restore it)
  • the SparseArray<Parcelable> that is the saved view hierarchy state
  • a Bundle in case we want to add additional data to our stored state.

Keeping track of the State

Removing States that are no longer needed

Restoring State after process death/config change


  • we don’t have convenient lifecycle callbacks to our views for when they get created, and removed (or destroyed) ~ although maybe we could use square/coordinators for that
  • you need to add a bunch of code in your Activity to integrate the backstack, and the application state/view state persistence. Maybe we could move this code out, into the library?



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store