Savedstate

Write pluggable components that save the UI state when a process dies, and restore it when the process restarts.
Latest Update Stable Release Release Candidate Beta Release Alpha Release
November 13, 2024 1.2.1 - - 1.3.0-alpha05

Declaring dependencies

To add a dependency on SavedState, you must add the Google Maven repository to your project. Read Google's Maven repository for more information.

Add the dependencies for the artifacts you need in the build.gradle file for your app or module:

Groovy

dependencies {
    // Java language implementation
    implementation "androidx.savedstate:savedstate:1.2.1"

    // Kotlin
    implementation "androidx.savedstate:savedstate-ktx:1.2.1"
}

Kotlin

dependencies {
    // Java language implementation
    implementation("androidx.savedstate:savedstate:1.2.1")

    // Kotlin
    implementation("androidx.savedstate:savedstate-ktx:1.2.1")
}

For more information about dependencies, see Add build dependencies.

Feedback

Your feedback helps make Jetpack better. Let us know if you discover new issues or have ideas for improving this library. Please take a look at the existing issues in this library before you create a new one. You can add your vote to an existing issue by clicking the star button.

Create a new issue

See the Issue Tracker documentation for more information.

Version 1.3

Version 1.3.0-alpha05

November 13, 2024

androidx.savedstate:savedstate-*:1.3.0-alpha05 is released. Version 1.3.0-alpha05 contains these commits.

KotlinX Serialization Support

  • SavedState now includes KotlinX Serialization support. You can convert a class annotated with @Serializable to a SavedState using the methods encodeToSavedState and decodeFromSavedState. The returned SavedState is a regular Bundle on Android and can be used by any API that accepts a Bundle. (I6f59f, b/374102924)

    @Serializable
    data class Person(val firstName: String, val lastName: String)
    
    fun main() {
        val person = Person("John", "Doe")
        val encoded: SavedState = encodeToSavedState(person)
        val decoded: Person = decodeFromSavedState(encoded)
    }
    
  • We also have included saved, a lazy property delegate, to make it easy to store @Serializable classes in a SavedStateRegistryOwner (e.g., ComponentActivity, Fragment, etc.) and have those classes automatically be restored across process death and recreation. Please note the saved delegate is lazy and will not call the init lambda or save anything into the SavedStateRegistry until it is accessed. (I66739, b/376027806)

    @Serializable
    data class Person(val firstName: String, val lastName: String)
    
    class MyActivity : ComponentActivity() {
        var person by saved { Person("John", "Doe") }
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            this.person = Person("Jane", "Doe")
        }
    }
    
  • There is a similar saved property delegate for SavedStateHandle added in Lifecycle 2.9.0-alpha07.

API Changes

  • Add toMap to SavedState, allowing any SavedState to be converted to a regular Map (shallow copy). (I487b9, b/334076622)
  • SavedState KMP now supports arrays. (Ic0552, b/334076622)

Version 1.3.0-alpha04

October 30, 2024

androidx.savedstate:savedstate-*:1.3.0-alpha04 is released. Version 1.3.0-alpha04 contains these commits.

API Changes

Version 1.3.0-alpha03

October 16, 2024

androidx.savedstate:savedstate-*:1.3.0-alpha03 is released with no notable changes. Version 1.3.0-alpha03 contains these commits.

Version 1.3.0-alpha02

October 2, 2024

androidx.savedstate:savedstate-*:1.3.0-alpha02 is released. Version 1.3.0-alpha02 contains these commits.

Kotlin Multiplatform

  • The SavedState module is now KMP compatible. Supported platforms now include Android, iOS, Linux, Mac, and JVM desktop environments. (I26305, b/334076622)

New Features

  • Introduce SavedState opaque type as an abstraction to provide a consistent way to save and restore application state in KMP. It includes a SavedStateReader and SavedStateWriter for modifying the state to be saved. On Android, SavedState is a type alias for Bundle, ensuring binary compatibility and facilitating the migration of existing APIs to a common source set. On other platforms, SavedState is a Map<String, Any> instance. (I18575, b/334076622)
  // Create a new SavedState object using the savedState DSL:
  val savedState = savedState {
    putInt("currentPage", 1)
    putString("filter", "favorites")
  }

  // Read from a SavedState object
  val currentPage = savedState.read { getInt("currentPage") }

  // Edit an existing SavedState object
  savedState.write {
    remove("currentPage")
  }

API Changes

  • SavedStateRegistry and SavedStateRegistryController are now KMP compatible. (Id7bb8, b/334076622)
  • SavedState, SavedStateWriter and SavedStateReader are now KMP compatible. (I26305, b/334076622)

Version 1.3.0-alpha01

August 7, 2024

androidx.savedstate:savedstate:1.3.0-alpha01 and androidx.savedstate:savedstate-ktx:1.3.0-alpha01 are released. Version 1.3.0-alpha01 contains these commits.

API Changes

  • The savedstate-ktx kotlin extensions have now been moved to the base savedstate module. (I1cc18, b/274803094)

Note

  • Update compileSdk to 35 (5dc41be)

Version 1.2.1

Version 1.2.1

March 22, 2023

androidx.savedstate:savedstate:1.2.1 and androidx.savedstate:savedstate-ktx:1.2.1 are released. Version 1.2.1 contains these commits.

Dependency Updates

Version 1.2.0

Version 1.2.0

June 29, 2022

androidx.savedstate:savedstate:1.2.0 and androidx.savedstate:savedstate-ktx:1.2.0 are released. Version 1.2.0 contains these commits.

Important changes since 1.1.0

  • SavedStateRegistryController now allows early attachment of the SavedStateRegistry via performAttach().
  • You can now retrieve a previously registered SavedStateProvider from a SavedStateRegistry via getSavedStateProvider().
  • The SavedState library has been rewritten in Kotlin.
    • For SavedStateRegistryOwner, this is a source incompatible change for those classes written in Kotlin - you must now override the savedStateRegistry property rather than implement the previous getSavedStateRegistry() function.
    • For ViewTreeSavedStateRegistryOwner, this is a source incompatible change for those classes written in Kotlin - you must now directly import and use the Kotlin extension methods on View of androidx.savedstate.setViewTreeSavedStateRegistryOwner and androidx.savedstate.findViewTreeSavedStateRegistryOwner to set and find a previously set owner. This replaces the savedstate-ktx API of findViewTreeSavedStateRegistryOwner.

Behavior Changes

  • SavedStateRegistry no longer saves an empty Bundle if there is no state to save.

Version 1.2.0-rc01

May 11, 2022

androidx.savedstate:savedstate:1.2.0-rc01 and androidx.savedstate:savedstate-ktx:1.2.0-rc01 are released. Version 1.2.0-rc01 contains these commits.

Documentation Changes

  • The SavedStateRegistryOwner Kdocs have been updated to clarify the responsibilities and contract that the owner has on how it should implement the interface or when they should call the methods on SavedStateRegistryController. (Iefc95, b/228887344)

Version 1.2.0-beta01

April 20, 2022

androidx.savedstate:savedstate:1.2.0-beta01 and androidx.savedstate:savedstate-ktx:1.2.0-beta01 are released. Version 1.2.0-beta01 contains these commits.

API Changes

  • The SavedStateRegistry and ViewTreeSavedStateRegistryOwner classes have been rewritten in Kotlin. For ViewTreeSavedStateRegistryOwner, this is a source incompatible change for those classes written in Kotlin - you must now directly import and use the Kotlin extension methods on View of androidx.savedstate.setViewTreeSavedStateRegistryOwner and androidx.savedstate.findViewTreeSavedStateRegistryOwner to set and find a previously set owner. This replaces the savedstate-ktx API of findViewTreeSavedStateRegistryOwner. This is binary compatible and remains source compatible for implementations written in the Java programming language. (b/220191285)

Version 1.2.0-alpha02

April 6, 2022

androidx.savedstate:savedstate:1.2.0-alpha02 and androidx.savedstate:savedstate-ktx:1.2.0-alpha02 are released. Version 1.2.0-alpha02 contains these commits.

New Features

  • You can now retrieve a previously registered SavedStateProvider from a SavedStateRegistry via getSavedStateProvider(). (I7ea47, b/215406268)

API Changes

  • The SavedStateRegistryOwner, SavedStateRegistryController, and Recreator classes have been rewritten in Kotlin. For SavedStateRegistryOwner, this is a source incompatible change for those classes written in Kotlin - you must now override the savedStateRegistry property rather than implement the previous getSavedStateRegistry() function. This is binary compatible and source compatible for implementations written in the Java programming language. (b/220191285)

Version 1.2.0-alpha01

January 26, 2022

androidx.savedstate:savedstate:1.2.0-alpha01 and androidx.savedstate:savedstate-ktx:1.2.0-alpha01 are released. Version 1.2.0-alpha01 contains these commits.

New Features

  • SavedStateRegistryController now allows early attachment of the SavedStateRegistry via performAttach(). (Ice4bf)

Behavior Changes

Version 1.1.0

Version 1.1.0

February 10, 2021

androidx.savedstate:savedstate:1.1.0 and androidx.savedstate:savedstate-ktx:1.1.0 are released. Version 1.1.0 contains these commits.

Major changes since 1.0.0

  • ViewTreeSavedStateRegistryOwner API: A new ViewTreeSavedStateRegistryOwner.get(View) API allows you to retrieve the containing SavedStateRegistry given a View instance. You must upgrade to Activity 1.2.0, Fragment 1.3.0, and AppCompat 1.3.0-alpha01 or higher to populate this correctly.
  • savedstate-ktx artifact: The new savedstate-ktx artifact has been added with a findViewTreeSavedStateRegistryOwner() Kotlin extension for working with ViewTreeSavedStateRegistryOwner.

Version 1.1.0-rc01

December 16, 2020

androidx.savedstate:savedstate:1.1.0-rc01 and androidx.savedstate:savedstate-ktx:1.1.0-rc01 are released with no changes since 1.1.0-beta01. Version 1.1.0-rc01 contains these commits.

Version 1.1.0-beta01

October 1, 2020

androidx.savedstate:savedstate:1.1.0-beta01 and androidx.savedstate:savedstate-ktx:1.1.0-beta01 are released with no changes since 1.1.0-alpha01. Version 1.1.0-beta01 contains these commits.

Version 1.1.0-alpha01

May 20, 2020

androidx.savedstate:savedstate:1.1.0-alpha01 and androidx.savedstate:savedstate-ktx:1.1.0-alpha01 are released. Version 1.1.0-alpha01 contains these commits.

New Features

Version 1.0.0

Version 1.0.0

September 5, 2019

androidx.savedstate:savedstate:1.0.0 is released. The commits included in this version can be found here.

Major features of SavedState 1.0.0

androidx.savedstate graduated to a stable release. This is a set of APIs that allow developers to plugin components into the restore / saveInstanceState process. The main entry point of the API is SavedStateRegistry, which provides a way to retrieve previously saved states using consumeRestoredStateForKey and register a callback to registerSavedStateProvider to provide a saved state once system requests it.

Version 1.0.0-rc01

July 2, 2019

androidx.savedstate:savedstate:1.0.0-rc01 is released. The commits included in this version can be found here.

Bug fixes

Version 1.0.0-beta01

May 7, 2019

androidx.savedstate:savedstate:1.0.0-beta01 is released. The commits included in this version can be found here.

Version 1.0.0-alpha02

March 13, 2019

androidx.savedstate:savedstate:1.0.0-alpha02 is released. androidx.savedstate:savedstate combines artifacts androidx.savedstate:savedstate-bundle and androidx.savedstate:savedstate-common into one artifact, because it was decided to simplify savedstate infrastructure and remove generics from SavedStateRegistry. Thus, there is no need for separate modules.

The full list of commits included in this version can be found here.

New features

  • SavedStateRegistry.runOnNextRecreaction(Class<? extends AutoRecreated> clazz ) was added. The given class will be instantiated and the method AutoRecreated.onRecreated will be run when the owning component restarted.

API changes

  • Generics removed from SavedStateRegistry<T>
  • AbstractSavedStateRegistry & BundlableSavedStateRegistry are removed, use simple SavedStateRegistry instead
  • BundleSavedStateRegistryOwner is renamed to SavedStateRegistryOwner

Version 1.0.0-alpha01

December 17, 2018

This is the first release of SavedState.

New features

androidx.savedstate is a new set of alpha APIs that allow developers to plugin components to the restore / saveInstanceState process. The main entry point of the API is SavedStateRegistry<T>, which provides a way to retrieve previously savedstate via consumeRestoredStateForKey and register a callback to registerSavedStateProvider to provide a savedstate once system requests it.