Savedstate
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.
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 aSavedState
using the methodsencodeToSavedState
anddecodeFromSavedState
. The returnedSavedState
is a regularBundle
on Android and can be used by any API that accepts aBundle
. (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 aSavedStateRegistryOwner
(e.g.,ComponentActivity
,Fragment
, etc.) and have those classes automatically be restored across process death and recreation. Please note thesaved
delegate is lazy and will not call theinit
lambda or save anything into theSavedStateRegistry
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 forSavedStateHandle
added in Lifecycle2.9.0-alpha07
.
API Changes
- Add
toMap
toSavedState
, allowing anySavedState
to be converted to a regularMap
(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
- SavedState KMP now supports Char. (I9ac2f, b/334076622)
- Add
putNull
andisNull
to SavedState KMP. (Iea71d, b/334076622) - Add additional
savedState
factory parameters supporting an initialMap<String, Any>
(I9b37d, b/334076622) - SavedState KMP now supports
contentDeepEquals
comparison. (Ia515c, b/334076622) - SavedState KMP now supports Long. (I4c180, b/334076622)
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 aSavedStateReader
andSavedStateWriter
for modifying the state to be saved. On Android,SavedState
is a type alias forBundle
, ensuring binary compatibility and facilitating the migration of existing APIs to a common source set. On other platforms,SavedState
is aMap<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
andSavedStateRegistryController
are now KMP compatible. (Id7bb8, b/334076622)SavedState
,SavedStateWriter
andSavedStateReader
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
SavedState
now depends on Lifecycle2.6.1
. (c1f621)
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 theSavedStateRegistry
viaperformAttach()
.- You can now retrieve a previously registered
SavedStateProvider
from aSavedStateRegistry
viagetSavedStateProvider()
. - 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 thesavedStateRegistry
property rather than implement the previousgetSavedStateRegistry()
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 onView
ofandroidx.savedstate.setViewTreeSavedStateRegistryOwner
andandroidx.savedstate.findViewTreeSavedStateRegistryOwner
to set and find a previously set owner. This replaces thesavedstate-ktx
API offindViewTreeSavedStateRegistryOwner
.
- For
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 onSavedStateRegistryController
. (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
andViewTreeSavedStateRegistryOwner
classes have been rewritten in Kotlin. ForViewTreeSavedStateRegistryOwner
, this is a source incompatible change for those classes written in Kotlin - you must now directly import and use the Kotlin extension methods onView
ofandroidx.savedstate.setViewTreeSavedStateRegistryOwner
andandroidx.savedstate.findViewTreeSavedStateRegistryOwner
to set and find a previously set owner. This replaces thesavedstate-ktx
API offindViewTreeSavedStateRegistryOwner
. 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 aSavedStateRegistry
viagetSavedStateProvider()
. (I7ea47, b/215406268)
API Changes
- The
SavedStateRegistryOwner
,SavedStateRegistryController
, andRecreator
classes have been rewritten in Kotlin. ForSavedStateRegistryOwner
, this is a source incompatible change for those classes written in Kotlin - you must now override thesavedStateRegistry
property rather than implement the previousgetSavedStateRegistry()
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 theSavedStateRegistry
viaperformAttach()
. (Ice4bf)
Behavior Changes
SavedStateRegistry
no longer saves an empty Bundle if there is no state to save. (aosp/1896865, b/203457956)
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 newViewTreeSavedStateRegistryOwner.get(View)
API allows you to retrieve the containingSavedStateRegistry
given aView
instance. You must upgrade to Activity1.2.0
, Fragment1.3.0
, and AppCompat1.3.0-alpha01
or higher to populate this correctly.savedstate-ktx
artifact: The newsavedstate-ktx
artifact has been added with afindViewTreeSavedStateRegistryOwner()
Kotlin extension for working withViewTreeSavedStateRegistryOwner
.
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
- A new
ViewTreeSavedStateRegistryOwner.get(View)
API allows you to retrieve the containingSavedStateRegistry
given aView
instance. You must upgrade to Activity1.2.0-alpha05
, Fragment1.3.0-alpha05
, and AppCompat1.3.0-alpha01
to populate this correctly. (aosp/1298679) - The new
savedstate-ktx
artifact has been added with afindViewTreeSavedStateRegistryOwner()
Kotlin extension for working withViewTreeSavedStateRegistryOwner
. (aosp/1299434)
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
- Fixed incorrect proguard rule (b/132655499)
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 methodAutoRecreated.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 toSavedStateRegistryOwner
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.