This table lists all the artifacts in the androidx.lifecycle group.
To add a dependency on Lifecycle, 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:
dependencies { def lifecycle_version = "2.10.0" def arch_version = "2.2.0" // ViewModel implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version" // ViewModel utilities for Compose implementation "androidx.lifecycle:lifecycle-viewmodel-compose:$lifecycle_version" // LiveData implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version" // Lifecycles only (without ViewModel or LiveData) implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version" // Lifecycle utilities for Compose implementation "androidx.lifecycle:lifecycle-runtime-compose:$lifecycle_version" // Saved state module for ViewModel implementation "androidx.lifecycle:lifecycle-viewmodel-savedstate:$lifecycle_version" // ViewModel integration with Navigation3 implementation "androidx.lifecycle:lifecycle-viewmodel-navigation3:2.11.0-rc01" // Annotation processor kapt "androidx.lifecycle:lifecycle-compiler:$lifecycle_version" // alternately - if using Java8, use the following instead of lifecycle-compiler implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version" // optional - helpers for implementing LifecycleOwner in a Service implementation "androidx.lifecycle:lifecycle-service:$lifecycle_version" // optional - ProcessLifecycleOwner provides a lifecycle for the whole application process implementation "androidx.lifecycle:lifecycle-process:$lifecycle_version" // optional - ReactiveStreams support for LiveData implementation "androidx.lifecycle:lifecycle-reactivestreams-ktx:$lifecycle_version" // optional - Test helpers for LiveData testImplementation "androidx.arch.core:core-testing:$arch_version" // optional - Test helpers for Lifecycle runtime testImplementation "androidx.lifecycle:lifecycle-runtime-testing:$lifecycle_version" }
dependencies { val lifecycle_version = "2.10.0" val arch_version = "2.2.0" // ViewModel implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version") // ViewModel utilities for Compose implementation("androidx.lifecycle:lifecycle-viewmodel-compose:$lifecycle_version") // LiveData implementation("androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version") // Lifecycles only (without ViewModel or LiveData) implementation("androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version") // Lifecycle utilities for Compose implementation("androidx.lifecycle:lifecycle-runtime-compose:$lifecycle_version") // Saved state module for ViewModel implementation("androidx.lifecycle:lifecycle-viewmodel-savedstate:$lifecycle_version") // ViewModel integration with Navigation3 implementation("androidx.lifecycle:lifecycle-viewmodel-navigation3:2.11.0-rc01") // Annotation processor kapt("androidx.lifecycle:lifecycle-compiler:$lifecycle_version") // alternately - if using Java8, use the following instead of lifecycle-compiler implementation("androidx.lifecycle:lifecycle-common-java8:$lifecycle_version") // optional - helpers for implementing LifecycleOwner in a Service implementation("androidx.lifecycle:lifecycle-service:$lifecycle_version") // optional - ProcessLifecycleOwner provides a lifecycle for the whole application process implementation("androidx.lifecycle:lifecycle-process:$lifecycle_version") // optional - ReactiveStreams support for LiveData implementation("androidx.lifecycle:lifecycle-reactivestreams-ktx:$lifecycle_version") // optional - Test helpers for LiveData testImplementation("androidx.arch.core:core-testing:$arch_version") // optional - Test helpers for Lifecycle runtime testImplementation ("androidx.lifecycle:lifecycle-runtime-testing:$lifecycle_version") }
dependencies { def lifecycle_version = "2.10.0" def arch_version = "2.2.0" // ViewModel implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version" // LiveData implementation "androidx.lifecycle:lifecycle-livedata:$lifecycle_version" // Lifecycles only (without ViewModel or LiveData) implementation "androidx.lifecycle:lifecycle-runtime:$lifecycle_version" // Saved state module for ViewModel implementation "androidx.lifecycle:lifecycle-viewmodel-savedstate:$lifecycle_version" // Annotation processor annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycle_version" // alternately - if using Java8, use the following instead of lifecycle-compiler implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version" // optional - helpers for implementing LifecycleOwner in a Service implementation "androidx.lifecycle:lifecycle-service:$lifecycle_version" // optional - ProcessLifecycleOwner provides a lifecycle for the whole application process implementation "androidx.lifecycle:lifecycle-process:$lifecycle_version" // optional - ReactiveStreams support for LiveData implementation "androidx.lifecycle:lifecycle-reactivestreams:$lifecycle_version" // optional - Test helpers for LiveData testImplementation "androidx.arch.core:core-testing:$arch_version" // optional - Test helpers for Lifecycle runtime testImplementation "androidx.lifecycle:lifecycle-runtime-testing:$lifecycle_version" }
dependencies { val lifecycle_version = "2.10.0" val arch_version = "2.2.0" // ViewModel implementation("androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version") // LiveData implementation("androidx.lifecycle:lifecycle-livedata:$lifecycle_version") // Lifecycles only (without ViewModel or LiveData) implementation("androidx.lifecycle:lifecycle-runtime:$lifecycle_version") // Saved state module for ViewModel implementation("androidx.lifecycle:lifecycle-viewmodel-savedstate:$lifecycle_version") // Annotation processor annotationProcessor("androidx.lifecycle:lifecycle-compiler:$lifecycle_version") // alternately - if using Java8, use the following instead of lifecycle-compiler implementation("androidx.lifecycle:lifecycle-common-java8:$lifecycle_version") // optional - helpers for implementing LifecycleOwner in a Service implementation("androidx.lifecycle:lifecycle-service:$lifecycle_version") // optional - ProcessLifecycleOwner provides a lifecycle for the whole application process implementation("androidx.lifecycle:lifecycle-process:$lifecycle_version") // optional - ReactiveStreams support for LiveData implementation("androidx.lifecycle:lifecycle-reactivestreams:$lifecycle_version") // optional - Test helpers for LiveData testImplementation("androidx.arch.core:core-testing:$arch_version") // optional - Test helpers for Lifecycle runtime testImplementation("androidx.lifecycle:lifecycle-runtime-testing:$lifecycle_version") }
For more information about dependencies, see Add Build Dependencies.
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.
June 03, 2026
androidx.lifecycle:lifecycle-*:2.11.0-rc01 is released. Version 2.11.0-rc01 contains these commits.
May 19, 2026
androidx.lifecycle:lifecycle-*:2.11.0-beta02 is released. Version 2.11.0-beta02 contains these commits.
API Changes
rememberViewModelStoreNavEntryDecorator overload that accepts a ViewModelStoreProvider. This allows developers to hoist the provider above the decorator's composition scope, enabling use cases such as supporting multiple back stacks where the same entry exists in both but requires separate state. (Ibbbfe, b/503420425)ViewModels when a sibling provider is removed from the composition. (Ib321a)Bug Fixes
LocalLifecycleOwner. This removes internal reflection fallbacks, improving performance but enforcing a strict dependency upgrade for clients. (I48e65)ViewModels were incorrectly cleared during recomposition when using rememberViewModelStoreProvider with unstable default parameters. The provider now only recreates when the parent owner or the explicit key changes. (Ibe296, b/501792448)April 22, 2026
androidx.lifecycle:lifecycle-*:2.11.0-beta01 is released. Version 2.11.0-beta01 contains these commits.
API Changes
rememberViewModelStoreOwner and rememberViewModelStoreProvider into separate functions for with-key and without-key use cases. (Ifd154, b/500802788)Bug Fixes
compileSdk to API 37. This means that a minimum AGP version of 9.2.0 is required when using Compose. (Id45cd, b/413674743)March 25, 2026
androidx.lifecycle:lifecycle-*:2.11.0-alpha03 is released. Version 2.11.0-alpha03 contains these commits.
API Changes
parentKey an optional parameter in ViewModelStoreProvider constructors, defaulting to null, and move it after parentStore or parentOwner. This simplifies instantiation when a shared scope key is not needed. (I66b67)ViewModelStoreNavEntryDecorator now accepts a ViewModelStoreOwner instead of a ViewModelStore. This allows Navigation3 to propagate the parent's default CreationExtras and ViewModelProvider.Factory to nested ViewModels, ensuring support for application-level dependencies. (Ia6018, b/434651920)ViewModelStoreProvider state using a unique key. This prevents sibling providers from unintentionally clearing each other's ViewModels during disposal. (I59c0b, b/434651920)ViewModelProvider.get<VM>(key: String) extension function. This enables developers to more cleanly retrieve ViewModels using a custom key with Kotlin reified type parameters, removing the need to explicitly pass the class type. (Ic1f59)defaultArgs support to ViewModelStoreProvider to allow initializing ViewModels with default SavedState values. (I1e933, b/434651920, b/165642391)removeViewModelStoreOnPop and the associated ViewModelStoreNavEntryDecoratorDefaults. This API was a workaround for configuration changes, which are now handled internally. All decorator state, including ViewModels, must now be cleared at the same time on pop. To keep decorator state around outside of when that back stack is passed to a NavDisplay, use the rememberDecoratedNavEntries API. (I852d9)Bug Fixes
toString() to CreationExtras.Key to display the class name. (I5de52)March 11, 2026
androidx.lifecycle:lifecycle-*:2.11.0-alpha02 is released. Version 2.11.0-alpha02 contains these commits.
Scoped ViewModels
This release introduces APIs to create scoped ViewModelStore instances in Compose. This allows ViewModels to be tied to specific UI scopes (such as individual Pager pages or LazyList items). These ViewModels survive configuration changes and are automatically cleared when their corresponding Composable is permanently removed from the hierarchy.
// Hoist the parent provider outside the Pager.
val provider = rememberViewModelStoreProvider()
val pagerState = rememberPagerState(pageCount = { 5 })
HorizontalPager(state = pagerState) { page ->
// Create an owner scoped to the current page.
val storeOwner = rememberViewModelStoreOwner(provider, key = page)
// Provide the scoped owner to this composition.
CompositionLocalProvider(LocalViewModelStoreOwner provides storeOwner) {
// ViewModel is now scoped specifically to this page.
val viewModel: PageViewModel = viewModel()
}
}
Note that ViewModelStoreProvider is available outside of Compose and can be used with Activities, Fragments and/or Views.
New Features
LifecycleOwner, ViewModelStoreOwner, and SavedStateRegistryOwner to reduce boilerplate when composing custom owner components. (I9682c)Lifecycle.addObserver extension function to allow observing lifecycle events without instantiating an anonymous object. (I94f38)defaultViewModelProviderFactory and defaultViewModelCreationExtras extensions to ViewModelStoreOwner. These provide safe access to defaults without requiring manual type checks against HasDefaultViewModelProviderFactory. (Ifa83d)API Changes
rememberViewModelStoreOwner to easily create scoped ViewModelStoreOwners in Compose. This function returns an owner that is tied to the Composable's lifecycle, automatically clearing its ViewModels when the Composable is removed from the UI. (I2a15b, b/165642391)rememberViewModelStoreProvider to allow creating scoped ViewModelStore instances in Compose. This function links a child store to a parent owner, ensuring ViewModels survive configuration changes while automatically clearing them when the Composable is removed from the hierarchy. (I0f57a, b/165642391)ViewModelStoreProvider to manage scoped ViewModelStore instances. This class enables the creation of child scopes that survive configuration changes but can be independently cleared. It also provides ViewModelStoreOwner wrappers that inherit default factories and creation extras from the parent. (I87f4f, b/434651920)February 25, 2026
androidx.lifecycle:lifecycle-*:2.11.0-alpha01 is released. Version 2.11.0-alpha01 contains these commits.
New Features
LocalView to allow platform hosts to provide a default ViewModelStoreOwner while maintaining LocalView as a safe fallback on Android. (I09fab, b/434940570, Ic8526, b/478146897)API Changes
toString implementation to ViewModelStore that lists all stored keys to help streamline debugging. (I0a6f4)HasDefaultViewModelProviderFactory.defaultViewModelProviderFactory, meaning you no longer need to override this property explicitly when implementing the interface. (Ia7095)ViewModel.onCleared with @EmptySuper to explicitly indicate that overriding methods do not need to call super.onCleared(). (I8c226)November 19, 2025
androidx.lifecycle:lifecycle-*:2.10.0 is released. Version 2.10.0 contains these commits.
Important changes since 2.9.0:
Add a rememberLifecycleOwner composable to enable creating scoped lifecycles directly within the UI. This is useful for components that need to manage their own lifecycles independently such as a HorizontalPager that wants to only make the current page RESUMED or libraries like Navigation3:
@Composable
fun MyComposable() {
// This LifecycleOwner is automatically moved to DESTROYED when
// it leaves composition and its maxLifecycle is the maximum of either
// the maxLifecycle you set or the Lifecycle.State of the parentLifecycleOwner
val lifecycleOwner = rememberLifecycleOwner(
maxLifecycle = RESUMED,
parentLifecycleOwner = LocalLifecycleOwner.current,
)
CompositionLocalProvider(LocalLifecycleOwner provides lifecycleOwner) {
val childLifecycleOwner = LocalLifecycleOwner.current
}
}
The lifecycle-viewmodel-navigation3 artifact provides a prebuilt integration for Navigation3, allowing you to enable scoping ViewModel instances to individual screens (‘entries’) by using the rememberViewModelStoreNavEntryDecorator() API:
@Composable
fun MyComposable() {
NavDisplay(
backStack = backStack,
entryDecorators =
listOf(
rememberSaveableStateHolderNavEntryDecorator(),
// Add this line to automatically scope ViewModels to each entry
rememberViewModelStoreNavEntryDecorator(),
),
entryProvider = entryProvider {
// Add your entries here
}
}
}
Add a builder factory function for CreationExtras, providing a more convenient and idiomatic Kotlin API.
override val defaultViewModelCreationExtras: CreationExtras
// Use the CreationExtras builder to add in a custom value to the default
// CreationExtras in your Activity or Fragment
get() = super.defaultViewModelCreationExtras + CreationExtras {
this[CustomKey] = "customValue"
}
Add native support for nullable types in SavedStateHandle.saved, simplifying saving and restoring nullable properties.
Mark SavedStateHandle constructors as @VisibleForTesting.
The minSdk has changed from API 21 to API 23.
November 05, 2025
androidx.lifecycle:lifecycle-*:2.10.0-rc01 is released. Version 2.10.0-rc01 contains these commits.
October 22, 2025
androidx.lifecycle:lifecycle-*:2.10.0-beta01 is released. Version 2.10.0-beta01 contains these commits.
API Changes
ViewModelStoreNavEntryDecoratorDefault has been renamed to ViewModelStoreNavEntryDecoratorDefaults, with an 's'. (I6d27b, b/444447434)Bug Fixes
rememberLifecycleOwner no longer crashes if the owner receives an Lifecycle.Event.ON_DESTROY event before moving to Lifeycle.State.CREATED. (I6f98e, b/444594991)October 08, 2025
androidx.lifecycle:lifecycle-*:2.10.0-alpha05 is released. Version 2.10.0-alpha05 contains these commits.
API Changes
removeViewModelStoreOnPopCallback() is now part of a ViewModelStoreNavEntryDecoratorDefault object which is where other platforms and implementations can call the default. (Ia1f23, b/444447434)ViewModelStoreNavEntryDecorator from a function to a class to better reflect its functionality as a factory for NavEntryDecorator, and renamed the decorator's shouldRemoveViewModelStore parameter to removeViewModelStoreOnPop to clarify that this callback is invoked only when an entry is popped from the backStack. (Iefdc5, b/444447434)September 24, 2025
androidx.lifecycle:lifecycle-*:2.10.0-alpha04 is released. Version 2.10.0-alpha04 contains these commits.
API Changes
LifecycleOwner composable to rememberLifecycleOwner. The function now returns the LifecycleOwner directly. To provide this owner to a sub-composition, use CompositionLocalProvider. (Ic57f0, b/444446629)August 27, 2025
androidx.lifecycle:lifecycle-*:2.10.0-alpha03 is released. Version 2.10.0-alpha03 contains these commits.
API Changes
August 13, 2025
androidx.lifecycle:lifecycle-*:2.10.0-alpha02 is released. Version 2.10.0-alpha02 contains these commits.
New Features
The LifecycleOwner composable can now create a standalone root lifecycle. By (explicitly) setting parent = null, the new lifecycle operates independently of any host (like an Activity, Fragment or NavBackStackEntry). It starts as soon as the composable enters the composition and is automatically destroyed when it leaves. (I8dfbe, b/433659048)
@Composable
fun IndependentComponent() {
// Create a standalone lifecycle, not tied to the parent Activity/Fragment.
LifecycleOwner(parent = null) {
val rootLifecycle = LocalLifecycleOwner.current.lifecycle
}
}
API Changes
LifecycleOwner composable, the parentLifecycleOwner parameter has been renamed to parent. (I080bc)Bug Fixes
LifecycleOwner composable now correctly moves its lifecycle to DESTROYED upon disposal. This prevents potential leaks for external code that holds a reference to the lifecycle. (I9e5b7, b/433659048)External Contribution
LifecycleOwner. Thanks Jake Wharton for the contribution. (Ideddb)July 30, 2025
androidx.lifecycle:lifecycle-*:2.10.0-alpha01 is released. Version 2.10.0-alpha01 contains these commits.
New Features
Add a LifecycleOwner composable to enable creating scoped lifecycles directly within the UI. This is useful for components that need to manage their own lifecycles independently. For an example of how Navigation3 integrates this new composable, see aosp/3708610. (76cbf7)
@Composable
fun MyComposable() {
LifecycleOwner(
maxLifecycle = RESUMED,
parentLifecycleOwner = LocalLifecycleOwner.current,
) {
val childLifecycleOwner = LocalLifecycleOwner.current
}
}
API Changes
CreationExtras, providing a more convenient and idiomatic Kotlin API. (Iab2bd)SavedStateHandle.saved, simplifying saving and restoring nullable properties. (I54d69, b/421325690)SavedStateHandle constructors as @VisibleForTesting. (Iff0e0, b/408002794)September 17, 2025
androidx.lifecycle:lifecycle-*:2.9.4 is released. Version 2.9.4 contains these commits.
Bug Fixes
August 27, 2025
androidx.lifecycle:lifecycle-*:2.9.3 is released. Version 2.9.3 contains these commits.
New Features
*-compose artifacts. Lifecycle now supports the following platforms in total: JVM (Android and Desktop), Native (Linux, iOS, watchOS, macOS, MinGW), and Web (JavaScript, WasmJS).
(I0a0e4)Bug Fixes
androidx.annotation to 1.9.1 (Ic9e4f, b/397701294)July 16, 2025
androidx.lifecycle:lifecycle-*:2.9.2 is released. Version 2.9.2 contains these commits.
Bug Fixes
*-compose artifacts, as this depends on the stable release of Compose 1.9. (I01cb8).Dependency updates
1.9.1 to enable support for the new KMP targets (Ic9e4f, b/397701294).June 4, 2025
androidx.lifecycle:lifecycle-*:2.9.1 is released. Version 2.9.1 contains these commits.
Bug Fixes
SavedStateHandle.remove(key) not clearing SavedStateHandle.getMutableStateFlow(key) states. (d5f939, b/418746333)May 7, 2025
androidx.lifecycle:lifecycle-*:2.9.0 is released. Version 2.9.0 contains these commits.
Important changes since 2.8.0
androidx.lifecycle:lifecycle-viewmodel-testing KMP artifact is available that provides a ViewModelScenario class for testing ViewModels in isolation, with support for onCleared and SavedStateHandle as well as testing process death and recreation via recreate().getMutableStateFlow to SavedStateHandle to return a MutableStateFlow. This new function is key-exclusive and cannot be used with getLiveData. An exception will be thrown if you try to use both to access the same state.CreationExtras now includes map-like operator overloads to enable idiomatic manipulation of content in Kotlin. It allows the use of in, +=, and + with CreationExtras.KotlinX Serialization Support
With the support of KotlinX Serialization added in SavedState 1.3.0, we have introduced saved, a lazy property delegate, to make it easy to store @Serializable classes in a SavedStateHandle 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 SavedStateHandle until it is accessed.
@Serializable
data class Person(val firstName: String, val lastName: String)
class MyViewModel(handle: SavedStateHandle) : ViewModel() {
var person by handle.saved { Person("John", "Doe") }
fun onPersonChanged(person: Person) {
this.person = person
}
}
Kotlin Multiplatform
lifecycle-testing module is now KMP compatible including APIs like TestLifecycleOwner.lifecycle-viewmodel-savedstate module is now KMP compatible including APIs like SavedStateHandle.androidx.compose.ui.platform.LocalLifecycleOwner is now available in the common source set.NewInstanceFactory is now available on JVM Desktop and Android targets.Behavior Changes
Lifecycle.DESTROYED state is terminal, and any attempt to move a Lifecycle from it to any other state will now result in an IllegalStateException.SavedStateHandle no longer includes any SavedStateProvider.saveState() where the returned Bundle is empty.April 23, 2025
androidx.lifecycle:lifecycle-*:2.9.0-rc01 is released. Version 2.9.0-rc01 contains these commits.
Lint API Compatibility Warning
KaCallableMemberCall from a class to an interface, which breaks binary compatibility. This can cause crashes if your project’s AGP version differs from the version used to compile lint checks. This update was made in aosp/3577172 but was missing from the release notes — we’re clarifying it here.
Recommended fix: Update to the latest stable AGP. If you can’t fully update, use android.experimental.lint.version to align lint checks with your AGP version — see Compose Runtime behavior changes for details.April 9, 2025
androidx.lifecycle:lifecycle-*:2.9.0-beta01 is released. Version 2.9.0-beta01 contains these commits.
API Changes
Lifecycle ViewModel Compose now uses the same Kotlin Multiplatform setup as Compose Runtime 1.7.1 and higher - the -desktop artifacts are now removed and -jvmStubs and -linuxx64Stubs artifacts have been added. None of these targets are meant to be used, they are placeholders to help Jetbrains Compose efforts. (I5cb14, b/406592090)Dependency Updates
Lifecycle ViewModel Compose now depends on Compose 1.7.8. (I5cb14, b/406592090)March 26, 2025
androidx.lifecycle:lifecycle-*:2.9.0-alpha13 is released with no notable public changes. Version 2.9.0-alpha13 contains these commits.
March 12, 2025
androidx.lifecycle:lifecycle-*:2.9.0-alpha12 is released. Version 2.9.0-alpha12 contains these commits.
API Changes
@MainThread annotation to ViewModelProvider.get in all supported KMP platforms. (I7e8dd, b/397736115)SavedState*Delegates to SavedState*Delegate. (I8589b, b/399629301)February 26, 2025
androidx.lifecycle:lifecycle-*:2.9.0-alpha11 is released. Version 2.9.0-alpha11 contains these commits.
API Changes
SavedStateConfig parameter to saved() delegates (I39b3a)February 12, 2025
androidx.lifecycle:lifecycle-*:2.9.0-alpha10 is released. Version 2.9.0-alpha10 contains these commits.
API Changes
MutableStateSerializer to savedstate-compose from lifecycle-viewmodel-compose. (I4f690, b/378895074)External Contribution
Lifecycle::currentState in composition, instead suggesting using currentStateAsalue().value to ensure that changes in the Lifecycle state correctly cause recomposition. Thanks Steven Schoen! (Iad484)January 29, 2025
androidx.lifecycle:lifecycle-*:2.9.0-alpha09 is released. Version 2.9.0-alpha09 contains these commits.
New Features
MutableStateSerializer for serializing androidx.compose.runtime.MutableState. (Idfc48, b/378895074)API Changes
SavedStateHandle.saved() delegate functions with default parameters (Icd1c1)AbstractSavedStateViewModelFactory is deprecated as it creates a SavedStateHandle for every ViewModel, causing unnecessary overhead. Use ViewModelProvider.Factory with CreationExtras.createSavedStateHandle instead for more efficient ViewModel creation. (Ia920b, b/388590327)December 11, 2024
androidx.lifecycle:lifecycle-*:2.9.0-alpha08 is released. Version 2.9.0-alpha08 contains these commits.
New Features
ViewModelScenario.recreate to simulate a System Process Death recreating the ViewModel under test and all associated components. (Id6a69, b/381063087)LifecycleOwner and ViewModelStoreOwner instances retrieved via their respective findViewTree APIs can now be resolved through disjoint parents of a view, such as a ViewOverlay. See the release notes of core or the documentation in ViewTree.setViewTreeDisjointParent for more information on disjoint view parents. (I800f4)API Changes
SavedStateRegistryOwnerDelegate (I8c135, b/376026744)Bug Fixes
-Xjspecify-annotations=strict (this is the default starting with version 2.1.0 of the Kotlin compiler). (Ie4340, b/326456246)ViewModel.onCleared clearing sequence. (I586c7, b/363984116)November 13, 2024
androidx.lifecycle:lifecycle-*:2.9.0-alpha07 is released. Version 2.9.0-alpha07 contains these commits.
Kotlin Multiplatform Compatibility
ViewModel SavedState is now KMP compatible. This allows you to use SavedStateHandle in common code. (Ib6394, b/334076622)KotlinX Serialization Support
With the support of KotlinX Serialization added in SavedState 1.3.0-alpha05, we have introduced saved, a lazy property delegate, to make it easy to store @Serializable classes in a SavedStateHandle 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 SavedStateHandle until it is accessed. (I47a88, b/376026744)
@Serializable
data class Person(val firstName: String, val lastName: String)
class MyViewModel(handle: SavedStateHandle) : ViewModel() {
var person by handle.saved { Person("John", "Doe") }
fun onPersonChanged(person: Person) {
this.person = person
}
}
API Changes
getMutableStateFlow to SavedStateHandle to return a MutableStateFlow. This new function is key-exclusive and cannot be used with getLiveData. An exception will be thrown if you try to use both to access the same state. (I04a4f, b/375408415)October 30, 2024
androidx.lifecycle:lifecycle-*:2.9.0-alpha06 is released. Version 2.9.0-alpha06 contains these commits.
Behavior Changes
Lifecycle.DESTROYED state is terminal, and any attempt to move a Lifecycle from it to any other state will now result in an IllegalStateException. (I116c4, b/370577987)SavedStateHandle no longer includes any SavedStateProvider.saveState() where the returned Bundle is empty. (I910b5, b/370577987)Bug Fixes
Lifecycle.eventFlow now correctly complete when Lifecycle is DESTROYED (I293b2, b/374043130)October 16, 2024
androidx.lifecycle:lifecycle-*:2.9.0-alpha05 is released with no notable changes. Version 2.9.0-alpha05 contains these commits.
October 2, 2024
androidx.lifecycle:lifecycle-*:2.9.0-alpha04 is released. Version 2.9.0-alpha04 contains these commits.
Kotlin Multiplatform
lifecycle-viewmodel-savedstate module is now configured to be KMP compatible in preparation for APIs like SavedStateHandle being made available in the common source set in a future release. (I503ed, I48764, b/334076622)September 18, 2024
androidx.lifecycle:lifecycle-*:2.9.0-alpha03 is released. Version 2.9.0-alpha03 contains these commits.
Bug Fixes
2.8.6: The NullSafeMutableLiveData Lint error has improved support for smart casts, avoiding false positives. (85fed6, b/181042665)Dependency Updates
2.8.6: Lifecycle Runtime Compose now depends on Compose Runtime 1.7.11.4.0September 4, 2024
androidx.lifecycle:lifecycle-*:2.9.0-alpha02 is released. Version 2.9.0-alpha02 contains these commits.
Bug Fixes
2.8.5: Update the androidx.lifecycle.ReportFragment ProGuard rules to allow obfuscation . (ff898e1)External Contribution
androidx.compose.ui.platform.LocalLifecycleOwner to common source set (KMP). Thanks Ivan Matkov from JetBrains for the contribution. (8cd5d03)2.8.5: SavedStateHandle.saveable` extension delegate now supports nullable values. Thanks Roman Kalukiewicz for the contribution. (0d78ea6)August 7, 2024
androidx.lifecycle:lifecycle-*:2.9.0-alpha01 is released. Version 2.9.0-alpha01 contains these commits.
Kotlin Multiplatform
lifecycle-testing is now KMP compatible. (Iea41e)linuxArm64 kotlin multiplatform target (I139d3, b/338268719)New Features
androidx.lifecycle:lifecycle-viewmodel-testing KMP artifact is available that provides a ViewModelScenario class for testing ViewModels in isolation, with support for onCleared (all platforms) and SavedStateHandle (Android only). (337f68d, c9b3409, 9799a95c, b/264602919)ViewModel with ViewModelProvider is now thread safe; @MainThread annotations have been removed. (Ifd978, b/237006831)API Changes
CreationExtras.Key() factory function to simplify the creation of anonymous CreationExtras.Key objects. (I970ee)CreationExtras now includes map-like operator overloads to enable idiomatic manipulation of content in Kotlin. It allows the use of in, +=, and + with CreationExtras. (Ib4353)CreationExtras now implements equals, hashCode, and toString methods. (Ib4353)NewInstanceFactory is now available on JVM Desktop and Android targets. (d3d0892)Bug Fixes
October 30, 2024
androidx.lifecycle:lifecycle-*:2.8.7 is released. Version 2.8.7 contains these commits.
API Changes
androidx.compose.ui.platform.LocalLifecycleOwner is now available in the common source set (KMP). (6a3f5b3)lifecycle-runtime-compose: desktop artifacts were removed and -jvmStubs and -linuxx64Stubs artifacts were added. None of these targets are meant to be used, they are placeholders to help Jetbrains Compose efforts. (6a3f5b3)September 18, 2024
androidx.lifecycle:lifecycle-*:2.8.6 is released. Version 2.8.6 contains these commits.
Bug Fixes
NullSafeMutableLiveData Lint error has improved support for smart casts, avoiding false positives. (85fed6, b/181042665)Dependency Updates
1.7.1September 4, 2024
androidx.lifecycle:lifecycle-*:2.8.5 is released. Version 2.8.5 contains these commits.
Bug Fixes
androidx.lifecycle.ReportFragment ProGuard rules to allow obfuscation . (ff898e1)External Contribution
SavedStateHandle.saveable extension delegate now supports nullable values. Thanks Roman Kalukiewicz for the contribution. (0d78ea6)July 24, 2024
androidx.lifecycle:lifecycle-*:2.8.4 is released. Version 2.8.4 contains these commits.
Bug Fixes
LiveData.asFlow() now correctly handles cases where the returned Flow is immediately completed after receiving a value already set on the LiveData (for example, when using take(1)). (I9c566)Lifecycle*Effect completion is now idempotent (i.e., if the onStopOrDispose was called because of the Lifecycle being stopped, it won’t be called a second time upon disposal unless the Lifecycle goes back up to STARTED again). (I5f607, b/352364595)July 1, 2024
androidx.lifecycle:lifecycle-*:2.8.3 is released. Version 2.8.3 contains these commits.
Bug Fixes
June 12, 2024
androidx.lifecycle:lifecycle-*:2.8.2 is released. Version 2.8.2 contains these commits.
Bug Fixes
CompositionLocal LocalLifecycleOwner not present errors when using Lifecycle 2.8.X with Compose 1.6.X or earlier - you can now use Lifecycle 2.8.2 with any version of Compose without any workarounds required. (aosp/3105647, b/336842920)ViewModelProvider will no longer crash when mixing previous versions of compileOnly Lifecycle dependencies with versions 2.8+, fixing issues with libraries such as LeakCanary. (I80383, b/341792251)May 29, 2024
androidx.lifecycle:lifecycle-*:2.8.1 is released. Version 2.8.1 contains these commits.
Bug Fixes
lifecycle-viewmodel-compose now only has a common dependency on compose-runtime, removing its common dependency on compose-ui. The Android artifact retains its compose-ui for compatibility. (aosp/3079334, b/339562627)ViewModel’s saveable integration using property delegates now uses the class name as part of the auto-generated key, avoiding conflicts if multiple classes use the same SavedStateHandle. (aosp/3063463)May 14, 2024
androidx.lifecycle:lifecycle-*:2.8.0 is released. Version 2.8.0 contains these commits.
Important changes since 2.7.0
LocalLifecycleOwner has been moved from Compose UI to lifecycle-runtime-compose so that its Compose-based helper APIs can be used outside of Compose UI.lifecycle-runtime-compose artifact now contains the dropUnlessResumed and dropUnlessStarted APIs which allow you to drop click or other events that occur even after the LifecycleOwner has dropped below the given Lifecycle.State. For example, this can be used with Navigation Compose to avoid handling click events after a transition to another screen has already begun: onClick: () -> Unit = dropUnlessResumed { navController.navigate(NEW_SCREEN) }ViewModel.viewModelScope is now an overridable constructor parameter, allowing you to inject your own dispatcher and SupervisorJob() or to override the default by using the backgroundScope available within runTest. (I2817c, b/264598574)
class MyViewModel(
// Make Dispatchers.Main the default, rather than Dispatchers.Main.immediate
viewModelScope: CoroutineScope = Dispatchers.Main + SupervisorJob()
) : ViewModel(viewModelScope) {
// Use viewModelScope as before, without any code changes
}
// Allows overriding the viewModelScope in a test
fun Test() = runTest {
val viewModel = MyViewModel(backgroundScope)
}
ViewModel has been rewritten in Kotlin and now uses AutoClosable instead of Closeable. It now supports adding AutoCloseable objects with a key that allows retrieving them via getCloseable().
Calling LifecycleStartEffect and LifecycleResumeEffect without a key is now an error, following the same convention as the DisposableEffect API that these APIs mirror.
Deprecated LiveDataReactiveStreams.toPublisher(lifecycleOwner, liveData) in favor of LiveData.toPublisher(lifecycleOwner).
The lifecycle-livedata-core-ktx kotlin extensions have now been moved to the lifecycle-livedata-core module.
The NullSafeMutableLiveData has been refactored to avoid many false positives.
Lifecycle Kotlin Multiplatform Compatibility
The core Lifecycle APIs in Lifecycle, LifecycleOwner, LifecycleObserver, Lifecycle.State, Lifecycle.Event, and LifecycleRegistry are now shipped in artifacts compatible with Kotlin Multiplatform.
Artifacts impacted:
lifecycle-common moves most APIs to common and supports jvm and iOS in addition to Android.lifecycle-runtime moves most APIs to common and supports jvm and iOS in addition to Android.lifecycle-runtime-ktx is now empty, with all APIs being moved into lifecycle-runtime.lifecycle-runtime-compose moves all APIs to common and ships an Android artifact, matching the multiplatform support of androidx.compose.ViewModel Kotlin Multiplatform Compatibility
The lifecycle-viewmodel artifact and APIs like ViewModel, ViewModelStore, ViewModelStoreOwner, and ViewModelProvider are now shipped in artifacts compatible with Kotlin Multiplatform.
To accommodate this change, methods such as those on ViewModelProvider that took a java.lang.Class<T> now have an equivalent method that takes a kotlin.reflect.KClass<T>.
Binary compatibility on Android has been maintained, but there are a few notable changes when comparing the Android API surface to the common API surface:
ViewModelProvider instance is now done through the ViewModelProvider.create() methods rather than directly calling its constructor.ViewModelProvider.NewInstanceFactory and ViewModelProvider.AndroidViewModelFactory are only available on Android.
ViewModelProvider.Factory and use the create method that takes a CreationExtras or use the viewModelFactory Kotlin DSL.ViewModelProvider without a custom factory on non-JVM platforms will result in an UnsupportedOperationException. On JVM platforms, compatibility is preserved by using the no-args ViewModel constructor if a custom factory is not provided.viewModelScope will fallback to an EmptyCoroutineContext in platforms where Dispatchers.Main is not available (e.g., Linux).Artifacts impacted:
lifecycle-viewmodel moves most APIs to common and supports jvm and iOS in addition to Android.lifecycle-viewmodel-ktx is now empty, with all APIs being moved into lifecycle-viewmodel.lifecycle-viewmodel-compose moves all APIs to common and ships an Android artifact, matching the multiplatform support of androidx.compose.Behavior Changes
InitializerViewModelFactory (including viewModelFactory builder function) will now throw an IllegalArgumentException if a initializer with the same clazz: KClass<VM : ViewModel> has already been added. (Ic3a36)Known Issues
lifecycle-*:2.8.0 requires a minimum Compose version of 1.7.0-alpha05 (b/336842920).May 1, 2024
androidx.lifecycle:lifecycle-*:2.8.0-rc01 is released. Version 2.8.0-rc01 contains these commits.
Bug Fixes
lifecycle-common classes was not properly packaged. These are now packaged in the lifecycle-runtime AAR. (aosp/3038274, b/322382422)AutoCloseable instances attached to a ViewModel are cleared - the previous order of addCloseable(String, AutoCloseable), then addClosable(AutoCloseable), then onCleared() has been restored. (aosp/3041632)viewModelScope for native and JVM Desktop environments. (aosp/3039221)External Contribution
April 17, 2024
androidx.lifecycle:lifecycle-*:2.8.0-beta01 is released. Version 2.8.0-beta01 contains these commits.
New Features
lifecycle-runtime-compose artifact is now compatible with Kotlin Multiplatform, moving its code to common and ships an Android artifact, matching the multiplatform support for androidx.compose. (If7a71, I4f4a0, b/331769623)April 3, 2024
androidx.lifecycle:lifecycle-*:2.8.0-alpha04 is released. Version 2.8.0-alpha04 contains these commits.
New Features
lifecycle-viewmodel-compose artifact is now compatible with Kotlin Multiplatform, moving its code to common and ships an Android artifact, matching the multiplatform support of androidx.compose. The accommodate this change, the Composable viewModel method now accepts a KClass in addition to a java.lang.Class. (b/330323282)Bug Fixes
NullSafeMutableLiveData has been refactored to avoid many false positives. (I2d8c1, Iafb18, I03463, I7ecef)Dependency update
lifecycle-viewmodel-compose artifact now depends on Compose 1.6.0.March 20, 2024
androidx.lifecycle:lifecycle-*:2.8.0-alpha03 is released. Version 2.8.0-alpha03 contains these commits.
New Features
ViewModel.viewModelScope is now an overridable constructor parameter, allowing you to inject your own dispatcher and SupervisorJob() or to override the default by using the backgroundScope available within runTest. (I2817c, b/264598574)
class MyViewModel(
// Make Dispatchers.Main the default, rather than Dispatchers.Main.immediate
viewModelScope: CoroutineScope = Dispatchers.Main + SupervisorJob()
) : ViewModel(viewModelScope) {
// Use viewModelScope as before, without any code changes
}
// Allows overriding the viewModelScope in a test
fun Test() = runTest {
val viewModel = MyViewModel(backgroundScope)
}
Kotlin Multiplatform Compatibility
The lifecycle-viewmodel artifact and APIs like ViewModel, ViewModelStore, ViewModelStoreOwner, and ViewModelProvider are now shipped in artifacts compatible with Kotlin Multiplatform. (b/214568825)
To accommodate this change, methods such as those on ViewModelProvider that took a java.lang.Class<T> now have an equivalent method that takes a kotlin.reflect.KClass<T>.
Binary compatibility on Android has been maintained, but there are a few notable changes when comparing the Android API surface to the common API surface:
ViewModelProvider instance is now done through the ViewModelProvider.create() methods rather than directly calling its constructor.ViewModelProvider.NewInstanceFactory and ViewModelProvider.AndroidViewModelFactory are only available on Android.
ViewModelProvider.Factory and use the create method that takes a CreationExtras or use the viewModelFactory Kotlin DSL.ViewModelProvider without a custom factory on non-JVM platforms will result in an UnsupportedOperationException. On JVM platforms, compatibility is preserved by using the no-args ViewModel constructor if a custom factory is not provided.viewModelScope will fallback to an EmptyCoroutineContext in platforms where Dispatchers.Main is not available (e.g., Linux).Behavior Changes
InitializerViewModelFactory (including viewModelFactory builder function) will now throw an IllegalArgumentException if a initializer with the same clazz: KClass<VM : ViewModel> has already been added. (Ic3a36)Bug Fixes
ViewModel.getCloseable now handles duplicated keys: if the key already has an AutoCloseable resource associated with it, the old resource will be replaced and closed immediately. (Ibeb67)viewModelScope of a ViewModel is now thread safe. (If4766, b/322407038)External Contribution
LocalLifecycleOwner moved from Compose UI to lifecycle-runtime-compose so that its Compose-based helper APIs can be used outside of Compose UI. Thanks Jake Wharton for the contribution. (I6c41b, b/328263448)February 21, 2024
androidx.lifecycle:lifecycle-*:2.8.0-alpha02 is released. Version 2.8.0-alpha02 contains these commits.
New Features
dropUnlessResumed and dropUnlessStarted APIs have been added which allow you to drop click or other events that occur even after the LifecycleOwner has dropped below the given Lifecycle.State. For example, this can be used with Navigation Compose to avoid handling click events after a transition to another screen has already begun: onClick: () -> Unit = dropUnlessResumed { navController.navigate(NEW_SCREEN) } (Icba83, b/317230685)Kotlin Conversions
ViewModel is now written in Kotlin (I16f26, b/214568825)lifecycle-viewmodel-ktx kotlin extensions have now been moved to the base lifecycle module. (Id787b, b/274800183)lifecycle-runtime-ktx kotlin extensions have now been moved to the base lifecycle module. (Ic3686, b/274800183)lifecycle-livedata-core-ktx kotlin extensions have now been moved to the base lifecycle module. (I54a3d, b/274800183)Kotlin Multiplatform Compatibility
Lifecycle, LifecycleOwner, LifecycleObserver, Lifecycle.State, Lifecycle.Event, and LifecycleRegistry are now shipped in artifacts compatible with Kotlin Multiplatform. (b/317249252)API Changes
LifecycleStartEffect and LifecycleResumeEffect without a key is now an error, following the same convention as the DisposableEffect API that these APIs mirror. (Ib0e0c, b/323518079)ViewModel now uses AutoCloseable instead of Closeable. That is a backward compatible change. (I27f8e, b/214568825)LiveDataReactiveStreams.toPublisher(lifecycleOwner, liveData) in favor of LiveData.toPublisher(lifecycleOwner). (Iabe29, b/262623005)External Contribution
January 24, 2024
androidx.lifecycle:lifecycle-*:2.8.0-alpha01 is released. Version 2.8.0-alpha01 contains these commits.
New Features
ViewModel now supports adding Closeable objects with a key that allows retrieving them via getCloseable(). (I3cf63)January 10, 2024
androidx.lifecycle:lifecycle-*:2.7.0 is released. Version 2.7.0 contains these commits.
Important changes since 2.6.0
TestLifecycleOwner now includes a suspending function setCurrentState() which ensures that the state change and all LifecycleObserver callbacks are completed before returning. Notably, unlike setting the currentState property directly, this does not use runBlocking, making it safe to use within a coroutine such as one provided by runTest.LiveData extensions of map and switchMap now mirror the behavior of distinctUntilChanged - if the LiveData has a value set, the map/switchMap function will be immediately called to populate the value of the returned LiveData. This ensures that the initial value will be set as part of the first composition (when used with observeAsState()), but does not change the observation behavior - updates values from the source LiveData will still only apply once you start observing the LiveData.SavedStateHandle would not properly restore custom Parcelable classes after process death and recreation. Due to type information that is lost by the Android framework, arrays of custom Parcelables require additional work (manually creating a typed array of the right type) and the documentation on get, getLiveData, and getStateFlow now specifically calls this limitation out.LifecycleObserver have been removed. This means that proguarded code that wishes to use APIs via reflection (such as using the long since deprecated @OnLifecycleEvent annotation) will need to provide their own keep rules for their specific use case.Lifecycle Event Observability
LifecycleEventObserver, you can now observe a Flow of Lifecycle.Event via the Lifecycle.asFlow() extension method.LifecycleEventEffect to run Compose side effects based on Lifecycle.Event.@Composable
fun HomeScreen(viewModel: HomeViewModel = viewModel()) {
LifecycleEventEffect(Lifecycle.Event.ON_RESUME) {
viewModel.refreshData()
}
// …
}
LifecycleStartEffect and LifecycleResumeEffect to handle pairs of events - started to stopped and resumed to paused, respectively. This API mirrors the one found in DisposableEffect and is suitable for cases where the change being made when the state is going up needs to be reversed when going back down.fun HomeScreen(viewModel: HomeViewModel = viewModel()) {
LifecycleStartEffect(viewModel) {
val timeTracking = viewModel.startTrackingTimeOnScreen()
onStopOrDispose {
timeTracking.stopTrackingTimeOnScreen()
}
}
// …
}
See Run code on lifecycle events for more information.
Lifecycle State Observability
Lifecycle.State can now be observed via the Lifecycle.currentStateFlow property, which returns a StateFlow where the value is the current Lifecycle.State.Lifecycle.currentStateAsState() extension to directly expose Lifecycle.State as Compose State. This is equivalent (and a shorter alternative) to lifecycle.currentStateFlow.collectAsState().See Collect lifecycle state with flows for more information.
December 13, 2023
androidx.lifecycle:lifecycle-*:2.7.0-rc02 is released. Version 2.7.0-rc02 contains these commits.
Bug Fixes
SavedStateHandle would not properly restore custom Parcelable classes after process death and recreation. Due to type information that is lost by the Android framework, arrays of custom Parcelables require additional work (manually creating a typed array of the right type) and the documentation on get, getLiveData, and getStateFlow now specifically calls this limitation out. (I0b55a)November 15, 2023
androidx.lifecycle:lifecycle-*:2.7.0-rc01 is released. Version 2.7.0-rc01 contains these commits.
Bug Fixes
LifecycleStartEffect and LifecycleResumeEffect now correctly dispose and recreate the effect block if the LifecycleOwner is changed. (Ia25c6)November 1, 2023
androidx.lifecycle:lifecycle-*:2.7.0-beta01 is released with no changes. Version 2.7.0-beta01 contains these commits.
October 18, 2023
androidx.lifecycle:lifecycle-*:2.7.0-alpha03 is released. Version 2.7.0-alpha03 contains these commits.
New Features
lifecycle-runtime-testing now contains a new Lint check to avoid setting the Lifecycle.State of the TestLifecycleOwner by using the currentState field when inside of a coroutine. The Lint check now suggests the suspending setCurrentState which allows setting the Lifecycle.State without blocking. (Icf728, b/297880630)Bug Fixes
LiveData.switchMap where returning the same LiveData instance both on the initial call and a subsequent call would prevent the LiveData instance from being added as a source. (Ibedcba7)September 6, 2023
androidx.lifecycle:lifecycle-*:2.7.0-alpha02 is released. Version 2.7.0-alpha02 contains these commits.
New Features
TestLifecycleOwner now includes the suspending function setCurrentState() to give users the option of using TestLifecycleOwner from within a coroutine such as one provided by runTest. (I329de, b/259344129)API Changes
lifecycle-livedata-ktx modules have been moved into the main lifecycle-livedata module. (I10c6f, b/274800183)Behavior Changes
LiveData.map() and LiveData.switchMap() extensions now sets the value of the returned LiveData if the previous LiveData has had a value set on it, ensuring that using the resulting LiveData in Jetpack Compose has the right state on the initial composition. (I91d2b, b/269479952)ViewModel's addCloseable() now immediately closes the Closeable if the ViewModel has already received a call to onCleared(). (I4712e, b/280294730)Bug Fixes
2.6.2: Fixed an issue where SavedStateHandle would not correctly be restored after process death if the state was restored, save() was called without actually saving the state in the parent SavedStateRegistry, and then the state was restored again. This fixes the interaction between rememberSaveable and Navigation Compose’s NavHost. (aosp/2729289)July 26, 2023
androidx.lifecycle:lifecycle-*:2.7.0-alpha01 is released. Version 2.7.0-alpha01 contains these commits.
API Changes
Lifecycle.State is now Compose-observable via Lifecycle.currentStateFlow, which returns a StateFlow where the value is the current Lifecycle.State. (Ib212d, b/209684871)Lifecycle.Events can now able to be observed as a Flow with Lifecycle.asFlow(). (If2c0f, b/176311030)LifecycleResumeEffect API has been added to run Compose SideEffects based on both Lifecycle.Event.ON_RESUME and Lifecycle.Event.ON_PAUSE event callbacks. (I60386, b/235529345)LifecycleStartEffect API has been added to run Compose SideEffects based on Lifecycle.Event.ON_START and Lifecycle.Event.ON_STOP event callbacks. (I5a8d1, b/235529345)LifecycleEventEffect API has been added to run Compose SideEffects based on Lifecycle.Event. (Ic9794, b/235529345)Lifecycle.collectAsState() extension has been added to directly expose Lifecycle.State as Compose State. This is equivalent (and a shorter alternative) to lifecycle.currentStateFlow.collectAsState(). (I11015, b/235529345)Bug Fixes
LiveData.distinctUntilChanged() extension now sets the value of the returned LiveData if the previous LiveData has had a value set on it. This does not change the observation behavior - updated values from the source LiveData will still only apply once you start observing the LiveData returned from distinctUntilChanged(). (Ib482f)LifecycleObserver have been removed. This means that proguarded code that wishes to use APIs via reflection will need to provide their own keep rules for their specific use case. (Ia12fd)September 6, 2023
androidx.lifecycle:lifecycle-*:2.6.2 is released. Version 2.6.2 contains these commits.
Bug Fixes
SavedStateHandle would not correctly be restored after process death if the state was restored, save() was called without actually saving the state in the parent SavedStateRegistry, and then the state was restored again. This fixes the interaction between rememberSaveable and Navigation Compose’s NavHost. (aosp/2729289)March 22, 2023
androidx.lifecycle:lifecycle-*:2.6.1 is released. Version 2.6.1 contains these commits.
Dependency Updates
lifecycle-viewmodel-savedstate now depends on SavedState 1.2.1. (cd7251)1.3.0. (f9d30b)March 8, 2023
androidx.lifecycle:lifecycle-*:2.6.0 is released. Version 2.6.0 contains these commits.
Important changes since 2.5.0
LiveData now includes a new isInitialized property that indicates whether an explicit value has ever been set on the LiveData, allowing you to distinguish between liveData.value returning null because no value has ever been set or an explicit null value.MediatorLiveData now includes a constructor to set an initial value.StateFlow and Flow of collectAsStateWithLifecycle() that collect from flows and represents its latest value as Compose State in a lifecycle-aware manner.Lifecycle.launchWhenX methods and Lifecycle.whenX methods have been deprecated as the use of a pausing dispatcher can lead to wasted resources in some cases. It is recommended to use Lifecycle.repeatOnLifecycle. For more information about one-time suspending work, please see this explanation on why this is inherently unsafe.ViewTreeLifecycleOwner, LiveDataReactiveStreams, HasDefaultViewModelProviderFactory, ViewTreeViewModelStoreOwner, Transformations, ViewModelStoreOwner, LifecycleOwnerThe table below provides the source conversions for the new version of lifecycle.
| Lifecycle 2.5 | Lifecycle 2.5 (KTX) | Lifecycle 2.6 |
|---|---|---|
Transformations.switchMap(liveData) {...} |
liveData.switchMap {...} |
liveData.switchMap {...} |
Transformations.map(liveData) {...} |
liveData.map {...} |
liveData.map {...} |
Transformations.distinctUntilChanged(liveData) {...} |
liveData.distinctUntilChanged{...} |
liveData.distinctUntilChanged{...} |
LiveDataReactiveStreams.fromPublisher(publisher) |
publisher.toLiveData() |
publisher.toLiveData() |
LiveDataReactiveStreams.toPublisher(lifecycleOwner, liveData) |
liveData.toPublisher(lifecycleOwner) |
liveData.toPublisher(lifecycleOwner) |
override fun getDefaultViewModelProviderFactory(): ViewModelProvider.Factory = factory |
override fun getDefaultViewModelProviderFactory(): ViewModelProvider.Factory = factory |
override val defaultViewModelProviderFactory = factory |
override fun getDefaultViewModelCreationExtras(): CreationExtras = extras |
override fun getDefaultViewModelCreationExtras(): CreationExtras = extras |
override val defaultViewModelProviderCreationExtras = extras |
ViewTreeLifecycleOwner.set(view, owner) |
ViewTreeLifecycleOwner.set(view, owner) |
view.setViewTreeLifecycleOwner(owner) |
ViewTreeLifecycleOwner.get(view) |
view.findViewTreeLifecycleOwner() |
view.findViewTreeLifecycleOwner() |
override fun getViewModelStore(): ViewModelStore = store |
override fun getViewModelStore(): ViewModelStore = store |
override val viewModelStore: ViewModelStore = store |
override fun getLifecycle(): Lifecycle = registry |
override fun getLifecycle(): Lifecycle = registry |
override val lifecycle: Lifecycle get() = registry |
onChanged method of a Observer created in Kotlin now matches the nullability of the generic type. If you want Observer.onChanged() to accept a nullable type, you must instantiate the Observer with a nullable type.DefaultLifecycleObserver, LifecycleEventObserver, Lifecycle, LifecycleRegistry, LifecycleObserver, ViewModelStore, AndroidViewModel, AbstractSavedStateViewModelFactory, LifecycleService, ServiceLifecycleDispatcher, and ProcessLifecycleOwnerFebruary 22, 2023
androidx.lifecycle:lifecycle-*:2.6.0-rc01 is released. Version 2.6.0-rc01 contains these commits.
Bug Fixes
LiveData.distinctUntilChanged() extension now sets the value of the returned LiveData if the previous LiveData has had a value set on it. This does not change the observation behavior - updated values from the source LiveData will still only apply once you start observing the LiveData returned from distinctUntilChanged(). (Ib482f)February 8, 2023
androidx.lifecycle:lifecycle-*:2.6.0-beta01 is released. Version 2.6.0-beta01 contains these commits.
Kotlin Conversions
LifecycleOwner is now written in Kotlin. This is a source incompatible change for classes written in Kotlin - they must now override the lifecycle property rather than implementing the previous getLifecycle() function. (I75b4b, b/240298691)ViewModelStoreOwner is now in Kotlin. This is a source incompatible change for classes written in Kotlin - they must now override the viewModelStore property rather than implementing the previous getViewModelStore() function. (I86409, b/240298691)LifecycleOwner that provides the lifecycleScope field has been moved to the lifecycle-common artifact from lifecycle-runtime-ktx. (I41d78, b/240298691)Lifecycle that provides the coroutineScope field has been moved to the lifecycle-common artifact from lifecycle-runtime-ktx. (Iabb91, b/240298691)January 25, 2023
androidx.lifecycle:lifecycle-*:2.6.0-alpha05 is released. Version 2.6.0-alpha05 contains these commits.
Kotlin Conversions
Transformations is now written in Kotlin. This is a source incompatible change for those classes written in Kotlin that were directly using syntax such as Transformations.map - Kotlin code must now use the Kotlin extension method syntax that was previously only available when using lifecycle-livedata-ktx. When using the Java programming language, the versions of these methods that take an androidx.arch.core.util.Function method are deprecated and replaced with the versions that take a Kotlin Function1. This change maintains binary compatibility. (I8e14f)ViewTreeViewModelStoreOwner is now written in Kotlin. 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.lifecycle.setViewTreeViewModelStoreOwner and androidx.lifecycle.findViewTreeViewModelStoreOwner to set and find a previously set owner. This is binary compatible and remains source compatible for implementations written in the Java programming language. (Ia06d8, Ib22d8, b/240298691)HasDefaultViewModelProviderFactory interface is now written in Kotlin. This is a source incompatible change for classes written in Kotlin - they must now override the defaultViewModelProviderFactory and defaultViewModelCreationExtras properties rather than implementing the previous corresponding functions. (Iaed9c, b/240298691)Observer is now written in Kotlin. Its onChanged() method now uses the name value for its parameter. (Iffef2, I4995e, b/240298691)AndroidViewModel, AbstractSavedStateViewModelFactory, LifecycleService, ServiceLifecycleDispatcher, and ProcessLifecycleOwner are now written in Kotlin (I2e771, Ibae40, I160d7, I08884, I1cda7, b/240298691)January 11, 2023
androidx.lifecycle:lifecycle-*:2.6.0-alpha04 is released. Version 2.6.0-alpha04 contains these commits.
New Features
LiveData now includes a new isInitialized property that indicates whether an explicit value has ever been set on the LiveData, allowing you to distinguish between liveData.value returning null because no value has ever been set or an explicit null value. (Ibd018)API Changes
collectAsStateWithLifecycle() APIs of lifecycle-runtime-compose are no longer in experimental status. (I09d42, b/258835424)Lifecycle.launchWhenX methods and Lifecycle.whenX methods have been deprecated as the use of a pausing dispatcher can lead to wasted resources in some cases. It is recommended to use Lifecycle.repeatOnLifecycle. (Iafc54, b/248302832)Kotlin Conversions
ViewTreeLifecycleOwner is now written in Kotlin. 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.lifecycle.setViewTreeLifecycleOwner and androidx.lifecycle.findViewTreeLifecycleOwner to set and find a previously set owner. This replaces the previous Kotlin extension in lifecycle-runtime-ktx. This is binary compatible and remains source compatible for implementations written in the Java programming language. (I8a77a, I5234e, b/240298691)LiveDataReactiveStreams is now written in Kotlin. The Kotlin extensions previously in lifecycle-reactivestreams-ktx have been moved into the lifecycle-reactivestreams module and have become the primary surface for code written in Kotlin. This is a source incompatible change for code written in Kotlin if you were not already using the Kotlin extension method APIs. (I2b1b9, I95d22, b/240298691)DefaultLifecycleObserver, LifecycleEventObserver, Lifecycle, LifecycleRegistry, LifecycleObserver, and ViewModelStore are now written in Kotlin (Iadffd, (I60034, I8c52c, I9593d, I01fe1, I59a23, b/240298691)Bug Fixes
SavedStateHandle no longer crashes with a ClassCastException when calling get() with the incorrect class
type. (I6ae7c)October 24, 2022
androidx.lifecycle:lifecycle-*:2.6.0-alpha03 is released. Version 2.6.0-alpha03 contains these commits.
Bug Fixes
LifecycleRegistry.moveToState() now include a more helpful error messaging that informs developers of the component causing the error. (Idf4b2, b/244910446)September 7, 2022
androidx.lifecycle:lifecycle-*:2.6.0-alpha02 is released. Version 2.6.0-alpha02 contains these commits.
API Changes
MediatorLiveData now includes a constructor to set an initial value. (Ib6cc5, b/151244085)Bug Fixes
Lifecycle artifacts now include constraints that ensure that all inter-dependent Lifecycle artifacts use the same version, automatically upgrading other dependencies when one is upgraded. b/242871265FlowLiveData.asFlow() now creates a callbackFlow rather than using its own Channel implementation to ensure
thread-safety and context preservation. (I4a8b2, b/200596935)FlowLiveData's asLiveData function will now preserve the initial value of a StateFlow when creating
the new LiveData object. (I3f530, b/157380488)2.5.1: Custom implementations of AndroidViewModelFactory now correctly calls the create(modelClass) function when using the stateful constructor with Lifecycle 2.4+ (I5b315, b/238011621)June 29, 2022
androidx.lifecycle:lifecycle-*:2.6.0-alpha01 is released. Version 2.6.0-alpha01 contains these commits.
New Features
StateFlow and Flow of collectAsStateWithLifecycle that collect from flows and represents its latest value as Compose State in a lifecycle-aware manner. The flow is collected and the new emission is set to the State's value when the lifecycle is at least in a certain Lifecycle.State. When the lifecycle falls below that Lifecycle.State, the flow collection stops and the State's value is not updated. (I1856e, b/230557927)July 27, 2022
androidx.lifecycle:lifecycle-*:2.5.1 is released. Version 2.5.1 contains these commits.
Bug Fixes
AndroidViewModelFactory now correctly call the create(modelClass) function when using the stateful AndroidViewModelFactory constructor with Lifecycle 2.4+. (I5b315, b/238011621)June 29, 2022
androidx.lifecycle:lifecycle-*:2.5.0 is released. Version 2.5.0 contains these commits.
Important changes since 2.4.0
SavedStateHandle now offers a getStateFlow() API that returns a Kotlin StateFlow for monitoring value changes as an alternative to using LiveData.
ViewModel CreationExtras - when writing a custom ViewModelProvider.Factory, it is no longer required to extend AndroidViewModelFactory or AbstractSavedStateViewModelFactory to gain access to an Application or SavedStateHandle, respectively. Instead, these fields are provided to every ViewModelProvider.Factory subclass as CreationExtras via the new overload of create: create(Class<T>, CreationExtras). These extras are provided automatically by your Activity or Fragment when using Activity 1.5.0 and Fragment 1.5.0, respectively.
class CustomFactory : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>, extras: CreationExtras): T {
return when (modelClass) {
HomeViewModel::class -> {
// Get the Application object from extras
val application = checkNotNull(extras[ViewModelProvider.AndroidViewModelFactory.APPLICATION_KEY])
// Pass it directly to HomeViewModel
HomeViewModel(application)
}
DetailViewModel::class -> {
// Create a SavedStateHandle for this ViewModel from extras
val savedStateHandle = extras.createSavedStateHandle()
DetailViewModel(savedStateHandle)
}
else -> throw IllegalArgumentException("Unknown class $modelClass")
} as T
}
}
lifecycle-viewmodel now provides a viewModelFactory Kotlin DSL that allows you define your ViewModelProvider.Factory in terms of one or more lambda initializers, one for each particular ViewModel class your custom factory supports, using CreationExtras as the primary data source.
val customFactory = viewModelFactory {
// The return type of the lambda automatically sets what class this lambda handles
initializer {
// Get the Application object from extras provided to the lambda
val application = checkNotNull(get(ViewModelProvider.AndroidViewModelFactory.APPLICATION_KEY))
HomeViewModel(application)
}
initializer {
val savedStateHandle = createSavedStateHandle()
DetailViewModel(savedStateHandle)
}
}
lifecycle-viewmodel-compose now offers a viewModel() API that takes a lambda factory for creating a ViewModel instance without requiring the creation of a custom ViewModelProvider.Factory.
// Within a @Composable, you can now skip writing a custom Factory
// and instead write a lambda to do the initialization of your ViewModel
val detailViewModel = viewModel {
// This lambda is only called the first time the ViewModel is created
// and all CreationExtras are available inside the lambda
val savedStateHandle = createSavedStateHandle()
DetailViewModel(savedStateHandle)
}
SavedStateHandle Compose Saver Integration - the lifecycle-viewmodel-compose artifact now contains new experimental APIs in SavedStateHandle.saveable that allow rememberSaveable like behavior backed by the SavedStateHandle of a `ViewModel.
class ListScreenViewModel(handle: SavedStateHandle): ViewModel() {
// This value survives both configuration changes and process death and recreation
val editMode by handle.saveable { mutableStateOf(false) }
}
Added an addCloseable() API and a new constructor overload that allow you to add one or more Closeable objects to the ViewModel that will be closed when the ViewModel is cleared without requiring any manual work in onCleared().
For instance, to create a coroutine scope that you can inject into a ViewModel, but control via testing, you can create a CoroutineScope that implements Closeable:
class CloseableCoroutineScope(
context: CoroutineContext = SupervisorJob() + Dispatchers.Main.immediate
) : Closeable, CoroutineScope {
override val coroutineContext: CoroutineContext = context
override fun close() {
coroutineContext.cancel()
}
}
Which can then be used in your ViewModel constructor while maintaining the same lifetime as viewModelScope:
class TestScopeViewModel(
val customScope: CloseableCoroutineScope = CloseableCoroutineScope()
) : ViewModel(customScope) {
// You can now use customScope in the same way as viewModelScope
}
Behavior changes
Lifecycle.State from INITIALIZED to DESTROYED will now always throw an IllegalStateException regardless
of whether the Lifecycle has an attached observer.LifecycleRegistry will now clear their observers when they reach the DESTROYED state.June 15, 2022
androidx.lifecycle:lifecycle-*:2.5.0-rc02 is released. Version 2.5.0-rc02 contains these commits.
Bug Fixes
ViewModelProvider will no longer crash when mixing previous versions of compileOnly Lifecycle dependencies with versions 2.5+. (I81a66, b/230454566)May 11, 2022
androidx.lifecycle:lifecycle-*:2.5.0-rc01 is released. Version 2.5.0-rc01 contains these commits.
Bug Fixes
MediatorLiveData.addSource() now throws a NullPointerException when passed a null source instead of propagating the null source to observers.(Ibd0fb, b/123085232)April 20, 2022
androidx.lifecycle:lifecycle-*:2.5.0-beta01 is released. Version 2.5.0-beta01 contains these commits.
API Changes
SavedStateHandle.saveable property delegates to use property names as keys for persisting state into the SavedStateHandle (I8bb86, b/225014345)Bug Fixes
NavHost within another NavHost in a non-primary bottom navigation tab would lead to an IllegalStateException when using multiple back stacks. (I11bd5, b/228865698)April 6, 2022
androidx.lifecycle:lifecycle-*:2.5.0-alpha06 is released. Version 2.5.0-alpha06 contains these commits.
New Features
MutableState overload to SavedStateHandle.saveable for
parity with rememberSaveable (I38cfe, b/224565154)API Changes
CreationExtras is now abstract instead of sealed. (Ib8a7a)Bug Fixes
IllegalStateException: Already attached to lifecycleOwner error caused by SavedStateHandleController. (I7ea47, b/215406268)March 23, 2022
androidx.lifecycle:lifecycle-*:2.5.0-alpha05 is released. Version 2.5.0-alpha05 contains these commits.
New Features
lifecycle-viewmodel-compose module now provides SavedStateHandleSaver, an experimental API that ensures values in a SavedStateHandle are integrated correctly with the same saved instance state that rememberSaveable uses. (Ia88b7, b/195689777)API Changes
Bug Fixes
SavedStateViewFactory now supports using CreationExtras even when it was initialized with a SavedStateRegistryOwner. If extras are provided, the initialized arguments are ignored. (I6c43b, b/224844583)March 9, 2022
androidx.lifecycle:lifecycle-*:2.5.0-alpha04 is released. Version 2.5.0-alpha04 contains these commits.
API Changes
SavedStateHandle now offers a getStateFlow() API that returns a Kotlin StateFlow for monitoring value changes as an
alternative to using LiveData. (Iad3ab, b/178037961)February 23, 2022
androidx.lifecycle:lifecycle-*:2.5.0-alpha03 is released. Version 2.5.0-alpha03 contains these commits.
New Features
addCloseable() API and a new
constructor overload that allow you to add one or more
Closeable objects to the ViewModel that will be
closed when the ViewModel is cleared without requiring
any manual work in onCleared(). (I55ea0)lifecycle-viewmodel now provides an
InitializerViewModelFactory that allows you to add lambda for handling
particular ViewModel classes, using CreationExtras as the primary data
source. (If58fc, b/216687549)lifecycle-viewmodel-compose now offers a viewModel() API that
takes a lambda factory for creating a ViewModel instance without
requiring the creation of a custom ViewModelProvider.Factory. (I97fbb, b/216688927)API Changes
ViewModel with CreationExtras via
lifecycle-viewmodel-compose. (I08887, b/216688927)Behavior changes
Lifecycle.State from INITIALIZED to
DESTROYED will now always throw an IllegalStateException regardless
of whether the Lifecycle has an attached observer. (I7c390, b/177924329)LifecycleRegistry will now clear their observers when they
reach the DESTROYED state. (I4f8dd, b/142925860)February 9, 2022
androidx.lifecycle:lifecycle-*:2.5.0-alpha02 is released. Version 2.5.0-alpha02 contains these commits.
API Changes
SavedStateHandle and SavedStateViewModelFactory have been converted to Kotlin. This has improved the nullability of the generics in both classes.
(Ib6ce2, b/216168263,
I9647a, b/177667711)LiveData switchMap function parameter can
now have a nullable output. (I40396, b/132923666)LiveData -ktx extensions are now annotated with
@CheckResult to enforce that the result is used when calling these
functions. (Ia0f05, b/207325134)Behavior changes
SavedStateHandle now properly stores the defaultValue when
no value for the specified key exists. (I1c6ce, b/178510877)Bug Fixes
2.4.1: Updated lifecycle-process to depend on Startup 1.1.1 to ensure that fixes that prevent ProcessLifecycleInitializer from throwing a StartupException are available by default. (Ib01df, b/216490724)AndroidViewModel classes have
parameters in the wrong order and attempt to create a ViewModel. (I340f7, b/177667711)CreationExtras using the AndroidViewModelFactory without setting an application. (I6ebef, b/217271656)January 26, 2022
androidx.lifecycle:lifecycle-*:2.5.0-alpha01 is released. Version 2.5.0-alpha01 contains these commits.
ViewModel CreationExtras
With this release, we are laying the groundwork for restructuring how a ViewModel is constructed. Instead of a rigid set of subclasses of ViewModelProvider.Factory that each add additional functionality (allowing an Application constructor parameter via AndroidViewModelFactory, allowing a SavedStateHandle constructor parameter via SavedStateViewModelFactory and AbstractSavedStateViewModelFactory, etc.), we are moving to a world of stateless factories that rely on a new concept, CreationExtras. (Ia7343, b/188691010, b/188541057)
With this change, ViewModelProvider no longer makes direct calls into the previous create(Class<T>) method of ViewModelProvider.Factory. Instead, it calls into a new overload of create: create(Class<T>, CreationExtras). This means that any direct implementation of the ViewModelProvider.Factory instance now has access to each of these new CreationExtras:
ViewModelProvider.NewInstanceFactory.VIEW_MODEL_KEY: this String provides access to the custom key you passed to ViewModelProvider.get().ViewModelProvider.AndroidViewModelFactory.APPLICATION_KEY provides access to the Application class.SavedStateHandleSupport.SAVED_STATE_REGISTRY_OWNER_KEY provides access to the SavedStateRegistryOwner that is being used to construct this ViewModel.SavedStateHandleSupport.VIEW_MODEL_STORE_OWNER_KEY provides access to the ViewModelStoreOwner that is being used to construct this ViewModel.SavedStateHandleSupport.DEFAULT_ARGS_KEY provides access to the Bundle of arguments that should be used to construct a SavedStateHandle.These extras are provided by default when using Activity 1.5.0-alpha01, Fragment 1.5.0-alpha01, and Navigation 2.5.0-alpha01. If you use an earlier version of those libraries, your CreationExtras will be empty - all of the existing subclasses of ViewModelProvider.Factory have been rewritten to support both the legacy creation path used by earlier versions of those libraries and the CreationExtras path which will be used going forward.
These CreationExtras allow you to construct a ViewModelProvider.Factory that passes just the information you need to each ViewModel without relying on a strict hierarchy of Factory subclasses:
class CustomFactory : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>, extras: CreationExtras): T {
return when (modelClass) {
HomeViewModel::class -> {
// Get the Application object from extras
val application = checkNotNull(extras[ViewModelProvider.AndroidViewModelFactory.APPLICATION_KEY])
// Pass it directly to HomeViewModel
HomeViewModel(application)
}
DetailViewModel::class -> {
// Create a SavedStateHandle for this ViewModel from extras
val savedStateHandle = extras.createSavedStateHandle()
DetailViewModel(savedStateHandle)
}
else -> throw IllegalArgumentException("Unknown class $modelClass")
} as T
}
}
We use the createSavedStateHandle() Kotlin extension function on CreationExtras from SavedStateHandleSupport to construct a SavedStateHandle only for the one ViewModel that needs it. (Ia6654, b/188541057)
Custom CreationExtras can be provided by overriding getDefaultViewModelCreationExtras() in your ComponentActivity or Fragment, thus making them available to your custom ViewModelProvider.Factory as a built in form of assisted injection. These extras will automatically be made available to your custom Factory when used directly with ViewModelProvider or when using the by viewModels() and by activityViewModels() Kotlin property extensions. (I79f2b, b/207012584, b/207012585, b/207012490)
Bug Fixes
SavedStateHandle would reappear after process
death and recreation, even if it was specifically
removed from the SavedStateHandle. As a consequence of
this, SavedStateHandle will no longer merge default
values and restored values together, instead only using
the restored values as the source of truth. (I53a4b)February 9, 2022
androidx.lifecycle:lifecycle-*:2.4.1 is released. Version 2.4.1 contains these commits.
Bug Fixes
2.5.0-alpha01: Fixed an issue where the default value provided
to a SavedStateHandle would re-appear after process
death and recreation, even if it was specifically
removed from the SavedStateHandle. As a consequence of
this, SavedStateHandle will no longer merge default
values and restored values together, instead only using
the restored values as the source of truth. (I53a4b)lifecycle-process now depends on Androidx Startup 1.1.1 which fixed a regression in where using ProcessLifecycleInitializer would cause an StartupException. (b/216490724)October 27, 2021
androidx.lifecycle:lifecycle-*:2.4.0 is released. Version 2.4.0 contains these commits.
Important changes since 2.3.0
@OnLifecycleEvent was deprecated. LifecycleEventObserver or DefaultLifecycleObserver should be used instead.androidx.lifecycle:lifecycle-viewmodel-compose library was added. It provides viewModel() composable and LocalViewModelStoreOwner.
ViewModelProvider has been rewritten in Kotlin. ViewModelProvider.Factory.create method no longer allows nullable generic.androidx.lifecycle:lifecycle-runtime-ktx:Lifecycle.repeatOnLifecycle, API that executes a block of code in a coroutine when the Lifecycle is at least in a certain state. The block will cancel and re-launch as the lifecycle moves in and out of the target state;Flow.flowWithLifecycle, API that emits values from the upstream Flow when the lifecycle is at least in a certain state.DefaultLifecycleObserver was moved from lifecycle.lifecycle-common-java8 to lifecycle.lifecycle-common. lifecycle.lifecycle-common-java8doesn't provide anymore any additional functionality on top of lifecycle.lifecycle-common, so dependency on it can be replaced by lifecycle.lifecycle-common.lifecycle-viewmodel-ktx have been moved to the lifecycle-viewmodel module.lifecycle-process now uses androidx.startup to initialize the ProcessLifecycleOwner.
Previously, this was being done by androidx.lifecycle.ProcessLifecycleOwnerInitializer.
If you used tools:node="remove" the ContentProvider being used to initialize
process lifecycle in the past, then you need to do the following instead.
<provider
android:name="androidx.startup.InitializationProvider"
android:authorities=\"${applicationId}.androidx-startup"
android:exported="false"
tools:node=\"merge">
<!-- If you are using androidx.startup to initialize other components -->
<meta-data
android:name="androidx.lifecycle.ProcessLifecycleInitializer"
android:value="androidx.startup"
tools:node="remove" />
</provider>
(or)
<!-- If you want to disable androidx.startup completely. -->
<provider
android:name="androidx.startup.InitializationProvider"
android:authorities="${applicationId}.androidx-startup"
tools:node="remove">
</provider>
September 29, 2021
androidx.lifecycle:lifecycle-*:2.4.0-rc01 is released with no changes from Lifecycle 2.4.0-beta01. Version 2.4.0-rc01 contains these commits.
September 15, 2021
androidx.lifecycle:lifecycle-*:2.4.0-beta01 is released. Version 2.4.0-beta01 contains these commits.
API Changes
@OnLifecycleEvent was deprecated. LifecycleEventObserver or
DefaultLifecycleObserver should be used instead. (I5a8fa)androidx.lifecycle.lifecycle-common-java8 to
androidx.lifecycle.lifecycle-common. androidx.lifecycle.lifecycle-common-java8 doesn't provide anymore any additional functionality on top of androidx.lifecycle.lifecycle-common, so dependency on it can be replaced by androidx.lifecycle.lifecycle-common. (I021aa)lifecycle-viewmodel-ktx have been moved to the
lifecycle-viewmodel module. (I6d5b2)External Contribution
August 4, 2021
androidx.lifecycle:lifecycle-*:2.4.0-alpha03 is released. Version 2.4.0-alpha03 contains these commits.
API Changes
ViewModelProvider.Factory.create method no longer allows nullable
generic. (I9b9f6)Behavior Changes
Lifecycle.repeatOnLifecycle: block is now always invoked serially when repeating execution. (Ibab33)External Contribution
repeatOnLifecycle documentation. #205.June 16, 2021
androidx.lifecycle:lifecycle-*:2.4.0-alpha02 is released. Version 2.4.0-alpha02 contains these commits.
New Features
RepeatOnLifecycleWrongUsage lint check to lifecycle-runtime-ktx that detects when repeateOnLifecycle is incorrectly used in onStart() or onResume(). (706078, b/187887400)API Changes
LifecycleOwner.addRepeatingJob API is removed in
favor of Lifecycle.repeatOnLifecycle that respects structured
concurrency and is easier to reason about. (I4a3a8)ProcessLifecycleInitializer public so other androidx.startup.Initializers can use these as dependencies. (I94c31)Bug Fixes
NullSafeMutableLiveData lint check when the field has modifiers. (#147, b/183696616)NullSafeMutableLiveData lint check when using generics. (#161, b/184830263)External Contribution
NullSafeMutableLiveData lint check. (#147, b/183696616)NullSafeMutableLiveData lint check. (#161, b/184830263)March 24, 2021
androidx.lifecycle:lifecycle-*:2.4.0-alpha01 is released. Version 2.4.0-alpha01 contains these commits.
Behavior Changes
lifecycle-process now uses androidx.startup to initialize the ProcessLifecycleOwner.
Previously, this was being done by androidx.lifecycle.ProcessLifecycleOwnerInitializer.
If you used tools:node="remove" the ContentProvider being used to initialize
process lifecycle in the past, then you need to do the following instead.
<provider
android:name="androidx.startup.InitializationProvider"
android:authorities=\"${applicationId}.androidx-startup"
android:exported="false"
tools:node=\"merge">
<!-- If you are using androidx.startup to initialize other components -->
<meta-data
android:name="androidx.lifecycle.ProcessLifecycleInitializer"
android:value="androidx.startup"
tools:node="remove" />
</provider>
(or)
<!-- If you want to disable androidx.startup completely. -->
<provider
android:name="androidx.startup.InitializationProvider"
android:authorities="${applicationId}.androidx-startup"
tools:node="remove">
</provider>
API Changes
Flow.flowWithLifecycle API that emits values from the upstream Flow when the lifecycle is at least in a certain state using the Lifecycle.repeatOnLifecycle API. This is an alternative to the also new LifecycleOwner.addRepeatinJob API.
(I0f4cd)Bug Fixes
NonNullableMutableLiveData lint rule can now properly
differentiate between field variables with different nullability. (b/169249668)June 16, 2021
androidx.lifecycle:lifecycle-viewmodel-compose:1.0.0-alpha07 is released. Version 1.0.0-alpha07 contains these commits.
Breaking API Changes
viewModel() now takes an
optional ViewModelStoreOwner, making it easier to work
with owners other than the LocalViewModelStoreOwner. For example, you can now use viewModel(navBackStackEntry) to retrieve a ViewModel associated with a particular navigation graph. (I2628d, b/188693123)June 2, 2021
androidx.lifecycle:lifecycle-viewmodel-compose:1.0.0-alpha06 is released. Version 1.0.0-alpha06 contains these commits.
Updated to be compatible with Compose version 1.0.0-beta08.
May 18, 2021
androidx.lifecycle:lifecycle-viewmodel-compose:1.0.0-alpha05 is released. Version 1.0.0-alpha05 contains these commits.
New Features
1.0.0-beta07.Bug Fixes
April 7, 2021
androidx.lifecycle:lifecycle-viewmodel-compose:1.0.0-alpha04 is released. Version 1.0.0-alpha04 contains these commits.
Dependency Changes
androidx.hilt:hilt-navigation-compose and androidx.navigation:navigation-compose
to sync dependencies on androidx.compose.compiler:compiler:1.0.0-beta04 and androidx.compose.runtime:runtime:1.0.0-beta04.
For 1.0.0, it is required that the compiler and runtime match.March 10, 2021
androidx.lifecycle:lifecycle-viewmodel-compose:1.0.0-alpha03 is released. Version 1.0.0-alpha03 contains these commits.
API Changes
LocalViewModelStoreOwner.current now returns a
nullable ViewModelStoreOwner to better determine
whether a ViewModelStoreOwner is available in the current
composition. APIs that require a ViewModelStoreOwner, such
as viewModel() and NavHost, still throw an exception
if a ViewModelStoreOwner is not set. (Idf39a)February 24, 2021
androidx.lifecycle:lifecycle-viewmodel-compose:1.0.0-alpha02 is released. Version 1.0.0-alpha02 contains these commits.
API Changes
LocalViewModelStoreOwner now has a provides functions that can
be used with CompositionLocalProvider, replacing the asProvidableCompositionLocal() API. (I45d24)February 10, 2021
androidx.lifecycle:lifecycle-viewmodel-compose:1.0.0-alpha01 is released. Version 1.0.0-alpha01 contains these commits.
New Features
viewModel() composable and LocalViewModelStoreOwner were moved from androidx.compose.ui.viewinterop to this artifact in the androidx.lifecycle.viewmodel.compose package. (I7a374)March 24, 2021
androidx.lifecycle:lifecycle-*:2.3.1 is released. Version 2.3.1 contains these commits.
Bug Fixes
NonNullableMutableLiveData lint rule can now properly
differentiate between field variables with different nullability. (b/169249668)February 10, 2021
androidx.lifecycle:lifecycle-*:2.3.0 is released. Version 2.3.0 contains these commits.
Major changes since 2.2.0
SavedStateHandle support for non-parcelable classes: SavedStateHandle now supports lazy serialization by allowing you to call setSavedStateProvider() for a given key, providing a SavedStateProvider that will get a callback to saveState() when the SavedStateHandle is asked to save its state. See Saving non-parcelable classes.DESTROYED as a terminal state.LifecycleRegistry now verifies that its methods are called on main thread. It was always a requirement for lifecycles of activities, fragments etc. An addition of observers from non-main threads resulted in hard to catch crashes in runtime. For LifecycleRegistry objects that owned by your own components, you can explicitly opt out from checks by using LifecycleRegistry.createUnsafe(...), but then you have to ensure that a proper synchronization is in place when this LifecycleRegistry is accessed from different threads.downFrom(State), downTo(State), upFrom(State), upTo(State) to Lifecycle.Event for generating the Event given a State and transition direction. Added the getTargetState() method that provides the State that the Lifecycle will transition to directly following the Event.withStateAtLeast: Added Lifecycle.withStateAtLeast APIs that await a lifecycle state and run a non-suspending block of code synchronously at the point of state change, then resume with the result. These APIs differ from the existing when* methods as they do not permit running suspending code and do not employ a custom dispatcher. (aosp/1326081)ViewTree APIs: A new ViewTreeLifecycleOwner.get(View) and ViewTreeViewModelStoreOwner.get(View) API allows you to retrieve the containing LifecycleOwner and ViewModelStoreOwner, respectively, given a View instance. You must upgrade to Activity 1.2.0 and Fragment 1.3.0, and AppCompat 1.3.0-alpha01 or higher to populate this correctly. The findViewTreeLifecycleOwner and findViewTreeViewModelStoreOwner Kotlin extensions are available in lifecycle-runtime-ktx and lifecycle-viewmodel-ktx, respectively.LiveData.observe() Kotlin extension deprecation: The LiveData.observe() Kotlin extension necessary to use lambda syntax is now deprecated as it is not necessary when using Kotlin 1.4.December 16, 2020
androidx.lifecycle:lifecycle-*:2.3.0-rc01 is released. Version 2.3.0-rc01 contains these commits.
Bug Fixes
keys() method of SavedStateHandle is now consistent before and after the state is saved - it now includes keys previously used with setSavedStateProvider() in addition to the keys used with set() and getLiveData(). (aosp/1517919, b/174713653)External Contribution
yield(). Thanks Nicklas Ansman Giertz! (aosp/1430830, b/168777346)October 1, 2020
androidx.lifecycle:lifecycle-*:2.3.0-beta01 is released. Version 2.3.0-beta01 contains these commits.
API Changes
LiveData.observe() Kotlin extension
necessary to use lambda syntax is now deprecated as it
is not necessary when using Kotlin 1.4. (I40d3f)Bug Fixes
Documentation Changes
liveData builder and asLiveData() docs have been updated to include details about changing the given timeout values. (aosp/1122324)August 19, 2020
androidx.lifecycle:lifecycle-*:2.3.0-alpha07 is released. Version 2.3.0-alpha07 contains these commits.
Bug Fixes
NullSafeMutableLiveData Lint check. (aosp/1395367)July 22, 2020
androidx.lifecycle:lifecycle-*:2.3.0-alpha06 is released. Version 2.3.0-alpha06 contains these commits.
New Features
downFrom(State), downTo(State), upFrom(State), upTo(State) to Lifecycle.Event for generating the Event given a State and transition direction. Added the getTargetState() method that provides the State that the Lifecycle will transition to directly following the Event. (I00887)Lifecycle.withStateAtLeast APIs that await a lifecycle state and run a non-suspending block of code synchronously at the point of state change, then resume
with the result. These APIs differ from the existing when* methods as they do not permit running suspending code and do not employ a custom dispatcher. (aosp/1326081)Behavior Changes
DESTROYED as a terminal state. (I00887)LifecycleRegistry now verifies that its methods are called on main thread. It was always a requirement for lifecycles of activities, fragments etc. An addition of observers from non-main threads resulted in hard to catch crashes in runtime. For LifecycleRegistry objects that owned by your own components, you can explicitly opt out from checks by using LifecycleRegistry.createUnsafe(...), but then you have to ensure that a proper synchronization is in place when this LifecycleRegistry is accessed from different threads (Ie7280, b/137392809)Bug Fixes
NullSafeMutableLiveData. (b/159987480)ObsoleteLintCustomCheck for Lint checks bundled with lifecycle-livedata-core-ktx (and specifically NullSafeMutableLiveData). (b/158699265)June 24, 2020
androidx.lifecycle:lifecycle-*:2.3.0-alpha05 is released. Version 2.3.0-alpha05 contains these commits.
Bug Fixes
LiveData now better handles reentrant cases, avoiding duplicate calls to onActive() or onInactive(). (b/157840298)June 10, 2020
androidx.lifecycle:lifecycle-*:2.3.0-alpha04 is released. Version 2.3.0-alpha04 contains these commits.
Bug Fixes
NonNullableMutableLiveData Lint check. (b/157294666)NonNullableMutableLiveData Lint check now covers significantly more cases where a null value was set on a MutableLiveData with a non-null type parameter. (b/156002218)May 20, 2020
androidx.lifecycle:lifecycle-*:2.3.0-alpha03 are released. Version 2.3.0-alpha03 contains these commits.
New Features
SavedStateHandle now supports lazy serialization by allowing you to call setSavedStateProvider() for a given key, providing a SavedStateProvider that will get a callback to saveState() when the SavedStateHandle is asked to save its state. (b/155106862)ViewTreeViewModelStoreOwner.get(View) API allows you to retrieve the containing ViewModelStoreOwner given a View instance. You must upgrade to Activity 1.2.0-alpha05, Fragment 1.3.0-alpha05, and AppCompat 1.3.0-alpha01 to populate this correctly. A findViewModelStoreOwner() Kotlin extension has been added to lifecycle-viewmodel-ktx. (aosp/1295522)Bug Fixes
MutableLiveData Lint checks released in Lifecycle 2.3.0-alpha01 from being published alongside the lifecycle-livedata-core-ktx artifact. (b/155323109)April 29, 2020
androidx.lifecycle:lifecycle-*:2.3.0-alpha02 is released. Version 2.3.0-alpha02 contains these commits.
API Changes
SavedStateViewModelFactory now allows you to pass a null Application to its constructor to better support cases where one is not readily available and support for AndroidViewModel is not needed. (aosp/1285740)Bug Fixes
March 4, 2020
androidx.lifecycle:lifecycle-*:2.3.0-alpha01 is released. Version 2.3.0-alpha01 contains these commits.
New Features
ViewTreeLifecycleOwner.get(View) API allows you to retrieve the containing LifecycleOwner given a View instance. You must upgrade to Activity 1.2.0-alpha01 and Fragment 1.3.0-alpha01 to populate this correctly. A findViewTreeLifecycleOwner Kotlin extension is available in lifecycle-runtime-ktx. (aosp/1182361, aosp/1182956)null value on a MutableLiveData that has been defined in Kotlin as non-null. This is available when using the livedata-core-ktx or livedata-ktx artifacts. (aosp/1154723, aosp/1159092)lifecycle-runtime-testing artifact is available that provides a TestLifecycleOwner that implements LifecycleOwner and provides a thread safe mutable Lifecycle. (aosp/1242438)Bug fixes
lifecycle-runtime artifact now has a unique package name. (aosp/1187196)February 5, 2020
androidx.lifecycle:lifecycle-viewmodel-savedstate:2.2.0 is released. Version 2.2.0 contains these commits.
Lifecycle ViewModel SavedState now shares the same version as other Lifecycle artifacts. The behavior of 2.2.0 is identical to the behavior of 1.0.0.
January 22, 2020
androidx.lifecycle:lifecycle-*:2.2.0 is released. Version 2.2.0 contains these commits.
Important changes since 2.1.0
lifecycle-runtime-ktx artifact adds integration between Lifecycle and Kotlin coroutines. The lifecycle-livedata-ktx has also been expanded to take advantage of coroutines. See Use Kotlin coroutines with Architecture Components for more details.ViewModelProviders.of() deprecation: ViewModelProviders.of() has been deprecated. You can pass a Fragment or FragmentActivity to the new ViewModelProvider(ViewModelStoreOwner) constructor to achieve the same functionality when using Fragment 1.2.0.lifecycle-extensions Artifact Deprecation: With the above deprecation of ViewModelProviders.of(), this release marks the deprecation of the last API in lifecycle-extensions and this artifact should now be considered deprecated in its entirety. We strongly recommend depending on the specific Lifecycle artifacts you need (such as lifecycle-service if you’re using LifecycleService and lifecycle-process if you’re using ProcessLifecycleOwner) rather than lifecycle-extensions as there will not be a future 2.3.0 release of lifecycle-extensions.DefautLifecycleObserver instead; and if
it's written in the Java 7 programming language you can use LifecycleEventObserver.December 4, 2019
androidx.lifecycle:lifecycle-*:2.2.0-rc03 is released. Version 2.2.0-rc03 contains these commits.
Bug fixes
ViewModel was stored in ViewModelStore and queried later with default factory.Dispatchers.Main.immediate in launchWhenCreated and similar methods to be called synchronously during corresponding lifecycle event. (aosp/1156203)External contributions
Dependency changes
1.2.0-rc03.November 7, 2019
androidx.lifecycle:lifecycle-*:2.2.0-rc02 is released. Version 2.2.0-rc02 contains these commits.
Bug fixes
October 23, 2019
androidx.lifecycle:lifecycle-*:2.2.0-rc01 is released. Version 2.2.0-rc01 contains these commits.
Bug fixes
launchWhenCreated and related methods would run one frame later than the associated lifecycle method due to its use of Dispatchers.Main instead of Dispatchers.Main.immediate. (aosp/1145596)External contributions
October 9, 2019
androidx.lifecycle:lifecycle-*:2.2.0-beta01 is released. Version 2.2.0-beta01 contains these commits.
Bug fixes
ProcessLifecycleOwner and the activity’s LifecycleOwner moving to started and resumed on Android 10 devices. (aosp/1128132)2.2.0-alpha05 which would cause a NullPointerException when using version 2.0.0 or 2.1.0 of lifecycle-process. (b/141536990)September 18, 2019
androidx.lifecycle:lifecycle-*:2.2.0-alpha05 is released. Version 2.2.0-alpha05 contains these commits.
Bug fixes
September 5, 2019
androidx.lifecycle:lifecycle-*:2.2.0-alpha04 is released. The commits included in this version can be found here.
New features
lifecycleScope, whenCreated, whenStarted, whenResumed, viewModelScope, and the underlying implementation of liveData now use Dispatchers.Main.immediate instead of Dispatchers.Main. (b/139740492)External contributions
Dispatchers.Main.immediate! (aosp/1106073)August 7, 2019
androidx.lifecycle:lifecycle-*:2.2.0-alpha03 is released. The commits included in this version can be found here.
New features
ViewModelStoreOwner can now optionally implement HasDefaultViewModelProviderFactory to provide a default ViewModelProvider.Factory. This has been done for Activity 1.1.0-alpha02, Fragment 1.2.0-alpha02, and Navigation 2.2.0-alpha01. (aosp/1092370, b/135716331)API changes
ViewModelProviders.of() has been deprecated. You can pass a Fragment or FragmentActivity to the new ViewModelProvider(ViewModelStoreOwner) constructor to achieve the same functionality. (aosp/1009889)July 2, 2019
androidx.lifecycle:*:2.2.0-alpha02 is released. The commits included in this version can be found here.
API changes
LiveDataScope.initialValue with LiveDataScope.latestValue which will track the current emitted value of the liveData block.liveData builder that receives timeout parameter as type DurationMay 7, 2019
androidx.lifecycle:*:2.2.0-alpha01 is released. The commits included in this version can be found here.
New features
January 22, 2020
androidx.lifecycle:lifecycle-viewmodel-savedstate:1.0.0 is released. Version 1.0.0 contains these commits.
Important features in 1.0.0
ViewModel classes to access and to contribute to the saved state. This object can be received in constructor of ViewModel class and factories provided by default by Fragments and AppCompatActivity will inject SavedStateHandle automatically.ViewModel and provide them access to SavedStateHandle.December 4, 2019
androidx.lifecycle:lifecycle-viewmodel-savedstate:1.0.0-rc03 is released. Version 1.0.0-rc03 contains these commits.
Dependency changes
2.2.0-rc03.November 7, 2019
androidx.lifecycle:lifecycle-viewmodel-savedstate:1.0.0-rc02 is released. Version 1.0.0-rc02 contains these commits.
Dependency changes
2.2.0-rc02.October 23, 2019
androidx.lifecycle:lifecycle-viewmodel-savedstate:1.0.0-rc01 is released with no changes from 1.0.0-beta01. Version 1.0.0-rc01 contains these commits.
October 9, 2019
androidx.lifecycle:lifecycle-viewmodel-savedstate:1.0.0-beta01 is released. Version 1.0.0-beta01 contains these commits.
Bug fixes
Activity.onActivityResult() would result in an IllegalStateException. (b/139093676)IllegalStateException when using AbstractSavedStateViewModelFactory. (b/141225984)September 18, 2019
androidx.lifecycle:lifecycle-viewmodel-savedstate:1.0.0-alpha05 is released. Version 1.0.0-alpha05 contains these commits.
API changes
SavedStateViewModelFactory no longer extends AbstractSavedStateViewModelFactory and SavedStateHandle is created only for ViewModels that requested have it (aosp/1113593)August 7, 2019
androidx.lifecycle:lifecycle-viewmodel-savedstate:1.0.0-alpha03 is released. The commits included in this version can be found here.
Breaking Changes
lifecycle-viewmodel-savedstate no longer depends on fragment and the related SavedStateViewModelFactory(Fragment) and SavedStateViewModelFactory(FragmentActivity) constructors have been removed. Instead, SavedStateViewModelFactory is now the default factory for Activity 1.1.0-alpha02, Fragment 1.2.0-alpha02, and Navigation 2.2.0-alpha01. (b/135716331)July 2, 2019
androidx.lifecycle:lifecycle-viewmodel-savedstate:1.0.0-alpha02 is released. The commits included in this version can be found here.
New features
SavedStateHandle.getLiveData() overload which accepts a default value.API Changes
SavedStateVMFactory is renamed to SavedStateViewModelFactory.AbstractSavedStateVMFactory is renamed to AbstractSavedStateViewModelFactory.March 13, 2019
androidx.lifecycle:lifecycle-viewmodel-savedstate:1.0.0-alpha01 is released. The full commit log for this initial release can be found here.
New features
ViewModels can contribute to savedstate. To do that you use newly introduced viewmodel’s factory SavedStateVMFactory and your ViewModel should have a constructor that receives SavedStateHandleobject as a parameter.Important changes since 2.0.0
LifecycleEventObserver for the cases when a stream of lifecycle events is needed. It is a public API instead of a hidden GenericLifecycleObserver class.LiveData.observe methods and Transformations.* methods.Transformations.distinctUntilChanged, which creates a new LiveData object that does not emit a value until the source LiveData value has been changed.ViewModel.viewModelScope.September 5, 2019
androidx.lifecycle:lifecycle-*:2.1.0 is released. The commits included in this version can be found here.
July 2, 2019
androidx.lifecycle:*:2.1.0-rc01 is released with no changes from androidx.lifecycle:*:2.1.0-beta01. The commits included in this version can be found here.
May 7, 2019
androidx.lifecycle:*:2.1.0-beta01 is released. The commits included in this version can be found here.
New features
liveData extension functions for transformations and observations, ViewModel initialisation with property delegation and others are stabilised and not going to change.April 3, 2019
androidx.lifecycle:*:2.1.0-alpha04 is released. The commits included in this version can be found here.
API changes
by viewModels() and by activityViewModels() has been changed to support a ViewModelStore directly, rather than only a ViewModelStoreOwner. (aosp/932932)March 13, 2019
androidx.lifecycle:*:2.1.0-alpha03 is released. The full list of commits included in this version can be found here.
API changes
ViewModelProvider.KeyedFactory was removed. Second interface in addition to ViewModelProvider.Factory didn’t compose well with new features as property delegation in Kotlin by viewmodels {}. (aosp/914133)January 30, 2019
androidx.lifecycle 2.1.0-alpha02 is released.
API changes
LifecycleRegistry now contains a setCurrentState() method that replaces the now deprecated setState() method. (aosp/880715)Bug fixes
ViewModel instances would crash when the containing ViewModelStore was cleared. b/122273087December 17, 2018
androidx.lifecycle 2.1.0-alpha01 is released.
New features
LifecycleEventObserver for the cases when a stream of lifecycle events is needed. It is a public api instead of a hidden GenericLifecycleObserver class.LiveData.observe methods and Transformations.* methods.Transformations.distinctUntilChanged was added. It creates a new LiveData object that does not emit a value until the source LiveData value has been changed.ViewModel.viewModelScope was added.ViewModelProvider.KeyedFactory, a factory for ViewModels that receives key and Class in create method.September 21, 2018
Lifecycle 2.0.0 is released with one bugfix from 2.0.0-rc01 in ViewModel.
Bug Fixes
July 2, 2018
Bug Fixes
For the pre-AndroidX versions of Lifecycle that follow, include these dependencies:
dependencies {
def lifecycle_version = "1.1.1"
// ViewModel and LiveData
implementation "android.arch.lifecycle:extensions:$lifecycle_version"
// alternatively - just ViewModel
implementation "android.arch.lifecycle:viewmodel:$lifecycle_version" // For Kotlin use viewmodel-ktx
// alternatively - just LiveData
implementation "android.arch.lifecycle:livedata:$lifecycle_version"
// alternatively - Lifecycles only (no ViewModel or LiveData).
// Support library depends on this lightweight import
implementation "android.arch.lifecycle:runtime:$lifecycle_version"
annotationProcessor "android.arch.lifecycle:compiler:$lifecycle_version" // For Kotlin use kapt instead of annotationProcessor
// alternately - if using Java8, use the following instead of compiler
implementation "android.arch.lifecycle:common-java8:$lifecycle_version"
// optional - ReactiveStreams support for LiveData
implementation "android.arch.lifecycle:reactivestreams:$lifecycle_version"
// optional - Test helpers for LiveData
testImplementation "android.arch.core:core-testing:$lifecycle_version"
}
March 21, 2018
Only one small change: android.arch.core.util.Function is moved from arch:runtime to arch:common. This allows it to be used without the runtime dependency, e.g. in paging:common below.
lifecycle:common is a dependency of lifecycle:runtime, so this change doesn’t affect lifecycle:runtime directly, only modules that depend directly on lifecycle:common, as Paging does.
January 22, 2018
Packaging Changes
New, much smaller dependencies are now available:
android.arch.lifecycle:livedata:1.1.0android.arch.lifecycle:viewmodel:1.1.0API Changes
LifecycleActivity and LifecycleFragment have now been removed - please use
FragmentActivity, AppCompatActivity or support Fragment.@NonNull annotations have been added to ViewModelProviders and ViewModelStoresViewModelProviders constructor has been deprecated - please use its static methods directlyViewModelProviders.DefaultFactory has been deprecated - please use ViewModelProvider.AndroidViewModelFactoryViewModelProvider.AndroidViewModelFactory.getInstance(Application) method has been added to retrieve
a static Factory suitable for creating ViewModel and AndroidViewModel instances.Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2026-06-03 UTC.