The second Android 11 Developer Preview is now available, test it out and share your feedback.

Lifecycle

Lifecycle-aware components perform actions in response to a change in the lifecycle status of another component, such as activities and fragments. These components help you produce better-organized, and often lighter-weight code, that is easier to maintain. See the reference docs for more information.

Latest Update Current Stable Release Next Release Candidate Beta Release Alpha Release
March 4, 2020 2.2.0 - - 2.3.0-alpha01

Declaring dependencies

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:

Kotlin

dependencies {
    def lifecycle_version = "2.2.0"
    def arch_version = "2.1.0"

    // ViewModel
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$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"

    // Saved state module for ViewModel
    implementation "androidx.lifecycle:lifecycle-viewmodel-savedstate:$lifecycle_version"

    // 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"
}

Java

dependencies {
    def lifecycle_version = "2.2.0"
    def arch_version = "2.1.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"
}

For more information about dependencies, see Add Build Dependencies.

Version 2.3.0

Version 2.3.0-alpha01

March 4, 2020

androidx.lifecycle:lifecycle-*:2.3.0-alpha01 is released. Version 2.3.0-alpha01 contains these commits.

New Features

  • A new 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)
  • Added a new Lint check that warns you when setting a 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)
  • A new lifecycle-runtime-testing artifact is available that provides a TestLifecycleOwner that implements LifecycleOwner and provides a thread safe mutable Lifecycle. (aosp/1242438)

Bug fixes

  • The lifecycle-runtime artifact now has a unique package name. (aosp/1187196)

Version 2.2.0

ViewModel-Savedstate Version 2.2.0

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.

Version 2.2.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 Coroutine Integration: The new 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.
  • Gradle Incremental Annotation Processor: Lifecycle's annotation processor is incremental by default. If your app is written in the Java 8 programming language you can use DefautLifecycleObserver instead; and if it's written in the Java 7 programming language you can use LifecycleEventObserver.

Version 2.2.0-rc03

December 4, 2019

androidx.lifecycle:lifecycle-*:2.2.0-rc03 is released. Version 2.2.0-rc03 contains these commits.

Bug fixes

  • Fixed a failure occurring when a mocked ViewModel was stored in ViewModelStore and queried later with default factory.
  • Fix a usage of Dispatchers.Main.immediate in launchWhenCreated and similar methods to be called synchronously during corresponding lifecycle event. (aosp/1156203)

External contributions

  • Thanks to Anders Järleberg for contributing the fix! (aosp/1156203)
  • Thanks to Vsevolod Tolstopyatov from Jetbrains for reviewing an implementation of inlined execution.

Dependency changes

  • Lifecycle Extensions now depends on Fragment 1.2.0-rc03.

Version 2.2.0-rc02

November 7, 2019

androidx.lifecycle:lifecycle-*:2.2.0-rc02 is released. Version 2.2.0-rc02 contains these commits.

Bug fixes

  • Fixed a bug in the proguard setup of the library that affected devices running API 28+ if the target API is below 29. (b/142778206)

Version 2.2.0-rc01

October 23, 2019

androidx.lifecycle:lifecycle-*:2.2.0-rc01 is released. Version 2.2.0-rc01 contains these commits.

Bug fixes

  • Fixed an issue where 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

  • Thanks to Nicklas Ansman for contributing the fix! (aosp/1145596)

Version 2.2.0-beta01

October 9, 2019

androidx.lifecycle:lifecycle-*:2.2.0-beta01 is released. Version 2.2.0-beta01 contains these commits.

Bug fixes

  • Fixed a regression introduced in Lifecycle 2.2.0-alpha05 in the ordering of ProcessLifecycleOwner and the activity’s LifecycleOwner moving to started and resumed on Android 10 devices. (aosp/1128132)
  • Fixed a regression introduced in Lifecycle 2.2.0-alpha05 which would cause a NullPointerException when using version 2.0.0 or 2.1.0 of lifecycle-process. (b/141536990)

Version 2.2.0-alpha05

September 18, 2019

androidx.lifecycle:lifecycle-*:2.2.0-alpha05 is released. Version 2.2.0-alpha05 contains these commits.

Bug fixes

  • Fixed a race condition in coroutine livedata builder. b/140249349

Version 2.2.0-alpha04

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

  • Thanks to Nicklas Ansman for contributing the move to Dispatchers.Main.immediate! (aosp/1106073)

Version 2.2.0-alpha03

August 7, 2019

androidx.lifecycle:lifecycle-*:2.2.0-alpha03 is released. The commits included in this version can be found here.

New features

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)

Version 2.2.0-alpha02

July 2, 2019

androidx.lifecycle:*:2.2.0-alpha02 is released. The commits included in this version can be found here.

API changes

  • Replaced LiveDataScope.initialValue with LiveDataScope.latestValue which will track the current emitted value of the liveData block.
  • Added a new overload to the liveData builder that receives timeout parameter as type Duration

Version 2.2.0-alpha01

May 7, 2019

androidx.lifecycle:*:2.2.0-alpha01 is released. The commits included in this version can be found here.

New features

  • This release adds new features that adds support for Kotlin coroutines for Lifecycle and LiveData. Detailed documentation on them can be found here.

ViewModel-SavedState Version 1.0.0

Version 1.0.0

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

  • New SavedStateHandle class was added. It enables your 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.
  • AbstractSavedStateViewModelFactory was added. It allows you to create custom factories for your ViewModel and provide them access to SavedStateHandle.

ViewModel-Savedstate Version 1.0.0-rc03

December 4, 2019

androidx.lifecycle:lifecycle-viewmodel-savedstate:1.0.0-rc03 is released. Version 1.0.0-rc03 contains these commits.

Dependency changes

  • Lifecycle ViewModel SavedState now depends on Lifecycle 2.2.0-rc03.

Viewmodel-Savedstate Version 1.0.0-rc02

November 7, 2019

androidx.lifecycle:lifecycle-viewmodel-savedstate:1.0.0-rc02 is released. Version 1.0.0-rc02 contains these commits.

Dependency changes

  • Now depends on lifecycle 2.2.0-rc02.

ViewModel-SavedState Version 1.0.0-rc01

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.

ViewModel-Savedstate Version 1.0.0-beta01

October 9, 2019

androidx.lifecycle:lifecycle-viewmodel-savedstate:1.0.0-beta01 is released. Version 1.0.0-beta01 contains these commits.

Bug fixes

  • Fixed an issue where accessing a SavedState ViewModel for the first time in Activity.onActivityResult() would result in an IllegalStateException. (b/139093676)
  • Fixed an IllegalStateException when using AbstractSavedStateViewModelFactory. (b/141225984)

ViewModel-SavedState Version 1.0.0-alpha05

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)

ViewModel-SavedState Version 1.0.0-alpha03

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

ViewModel-SavedState Version 1.0.0-alpha02

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

  • Added SavedStateHandle.getLiveData() overload which accepts a default value.

API Changes

  • SavedStateVMFactory is renamed to SavedStateViewModelFactory.
  • AbstractSavedStateVMFactory is renamed to AbstractSavedStateViewModelFactory.

ViewModel-Savedstate Version 1.0.0-alpha01

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

  • Now 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.

Version 2.1.0

Important changes since 2.0.0

  • Added LifecycleEventObserver for the cases when a stream of lifecycle events is needed. It is a public API instead of a hidden GenericLifecycleObserver class.
  • Added ktx extensions for LiveData.observe methods and Transformations.* methods.
  • Added Transformations.distinctUntilChanged, which creates a new LiveData object that does not emit a value until the source LiveData value has been changed.
  • Added coroutine support in ViewModels by adding the extension property ViewModel.viewModelScope.

Version 2.1.0

September 5, 2019

androidx.lifecycle:lifecycle-*:2.1.0 is released. The commits included in this version can be found here.

Version 2.1.0-rc01

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.

Version 2.1.0-beta01

May 7, 2019

androidx.lifecycle:*:2.1.0-beta01 is released. The commits included in this version can be found here.

New features

  • Lifecycles are graduated to beta: api introduced in previous alphas such as liveData extension functions for transformations and observations, ViewModel initialisation with property delegation and others are stabilised and not going to change.

Version 2.1.0-alpha04

April 3, 2019

androidx.lifecycle:*:2.1.0-alpha04 is released. The commits included in this version can be found here.

API changes

  • Breaking change: the underlying API behind by viewModels() and by activityViewModels() has been changed to support a ViewModelStore directly, rather than only a ViewModelStoreOwner. (aosp/932932)

Version 2.1.0-alpha03

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)

Version 2.1.0-alpha02

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

  • Fixed an issue where mock ViewModel instances would crash when the containing ViewModelStore was cleared. b/122273087

Version 2.1.0-alpha01

December 17, 2018

androidx.lifecycle 2.1.0-alpha01 is released.

New features

  • Added LifecycleEventObserver for the cases when a stream of lifecycle events is needed. It is a public api instead of a hidden GenericLifecycleObserver class.
  • Added ktx extensions for LiveData.observe methods and Transformations.* methods.
  • Method Transformations.distinctUntilChanged was added. It creates a new LiveData object that does not emit a value until the source LiveData value has been changed.
  • Coroutine support in ViewModels: extension property ViewModel.viewModelScope was added.
  • Added ViewModelProvider.KeyedFactory, a factory for ViewModels that receives key and Class in create method.

Version 2.0.0

Version 2.0.0

September 21, 2018

Lifecycle 2.0.0 is released with one bugfix from 2.0.0-rc01 in ViewModel.

Bug Fixes

  • Fixed a ViewModel proguard rule that incorrectly removed constructors b/112230489

Version 2.0.0-beta01

July 2, 2018

Bug Fixes

  • Fixed LifecycleObserver proguard rule to keep only implementations, not subinterfaces b/71389427
  • Fixed ViewModel proguard rules to allow obfuscation and shrinking

Pre-AndroidX Versions

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"
}

Version 1.1.1

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.

Version 1.1.0

January 22, 2018

Packaging Changes

New, much smaller dependencies are now available:

  • android.arch.lifecycle:livedata:1.1.0
  • android.arch.lifecycle:viewmodel:1.1.0

API Changes

  • The deprecated LifecycleActivity and LifecycleFragment have now been removed - please use FragmentActivity, AppCompatActivity or support Fragment.
  • @NonNull annotations have been added to ViewModelProviders and ViewModelStores
  • ViewModelProviders constructor has been deprecated - please use its static methods directly
  • ViewModelProviders.DefaultFactory has been deprecated - please use ViewModelProvider.AndroidViewModelFactory
  • The static ViewModelProvider.AndroidViewModelFactory.getInstance(Application) method has been added to retrieve a static Factory suitable for creating ViewModel and AndroidViewModel instances.