Register now for Android Dev Summit 2019!

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.

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:

dependencies {
    def lifecycle_version = "2.0.0"

    // ViewModel and LiveData
    implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
    // alternatively - just ViewModel
    implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version" // For Kotlin use lifecycle-viewmodel-ktx
    // alternatively - just LiveData
    implementation "androidx.lifecycle:lifecycle-livedata:$lifecycle_version"
    // alternatively - Lifecycles only (no ViewModel or LiveData). Some UI
    //     AndroidX libraries use this lightweight import for Lifecycle
    implementation "androidx.lifecycle:lifecycle-runtime:$lifecycle_version"

    annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycle_version" // For Kotlin use kapt instead of annotationProcessor
    // alternately - if using Java8, use the following instead of lifecycle-compiler
    implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"

    // optional - ReactiveStreams support for LiveData
    implementation "androidx.lifecycle:lifecycle-reactivestreams:$lifecycle_version" // For Kotlin use lifecycle-reactivestreams-ktx

    // optional - Test helpers for LiveData
    testImplementation "androidx.arch.core:core-testing:$lifecycle_version"
}

For more information about dependencies, see Add Build Dependencies.

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

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.

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

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)

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-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 ViewModelPrioder.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

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 Dependencies

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.