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 |
---|---|---|---|---|
December 4, 2019 | 2.1.0 | 2.2.0-rc03 | - | - |
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.1.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
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 inViewModelStore
and queried later with default factory. - Fix a usage of
Dispatchers.Main.immediate
inlaunchWhenCreated
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 ofDispatchers.Main
instead ofDispatchers.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’sLifecycleOwner
moving to started and resumed on Android 10 devices. (aosp/1128132) - Fixed a regression introduced in Lifecycle
2.2.0-alpha05
which would cause aNullPointerException
when using version2.0.0
or2.1.0
oflifecycle-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 ofliveData
now useDispatchers.Main.immediate
instead ofDispatchers.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
- Implementations of
ViewModelStoreOwner
can now optionally implementHasDefaultViewModelProviderFactory
to provide a defaultViewModelProvider.Factory
. This has been done for Activity1.1.0-alpha02
, Fragment1.2.0-alpha02
, and Navigation2.2.0-alpha01
. (aosp/1092370, b/135716331)
API changes
ViewModelProviders.of()
has been deprecated. You can pass aFragment
orFragmentActivity
to the newViewModelProvider(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
withLiveDataScope.latestValue
which will track the current emitted value of theliveData
block. - Added a new overload to the
liveData
builder that receivestimeout
parameter as typeDuration
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
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 anIllegalStateException
. (b/139093676) - Fixed an
IllegalStateException
when usingAbstractSavedStateViewModelFactory
. (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 extendsAbstractSavedStateViewModelFactory
andSavedStateHandle
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
lifecycle-viewmodel-savedstate
no longer depends onfragment
and the relatedSavedStateViewModelFactory(Fragment)
andSavedStateViewModelFactory(FragmentActivity)
constructors have been removed. Instead,SavedStateViewModelFactory
is now the default factory for Activity1.1.0-alpha02
, Fragment1.2.0-alpha02
, and Navigation2.2.0-alpha01
. (b/135716331)
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 toSavedStateViewModelFactory
.AbstractSavedStateVMFactory
is renamed toAbstractSavedStateViewModelFactory
.
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 factorySavedStateVMFactory
and your ViewModel should have a constructor that receivesSavedStateHandle
object 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 hiddenGenericLifecycleObserver
class. - Added ktx extensions for
LiveData.observe
methods andTransformations.*
methods. - Added
Transformations.distinctUntilChanged
, which creates a new LiveData object that does not emit a value until the sourceLiveData
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()
andby activityViewModels()
has been changed to support aViewModelStore
directly, rather than only aViewModelStoreOwner
. (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 toViewModelProvider.Factory
didn’t compose well with new features as property delegation in Kotlinby viewmodels {}
. (aosp/914133)
Version 2.1.0-alpha02
January 30, 2019
androidx.lifecycle 2.1.0-alpha02
is released.
API changes
LifecycleRegistry
now contains asetCurrentState()
method that replaces the now deprecatedsetState()
method. (aosp/880715)
Bug fixes
- Fixed an issue where mock
ViewModel
instances would crash when the containingViewModelStore
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 hiddenGenericLifecycleObserver
class. - Added ktx extensions for
LiveData.observe
methods andTransformations.*
methods. - Method
Transformations.distinctUntilChanged
was added. It creates a newLiveData
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 receiveskey
andClass
increate
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
andLifecycleFragment
have now been removed - please useFragmentActivity
,AppCompatActivity
or supportFragment
. @NonNull
annotations have been added toViewModelProviders
andViewModelStores
ViewModelProviders
constructor has been deprecated - please use its static methods directlyViewModelProviders.DefaultFactory
has been deprecated - please useViewModelProvider.AndroidViewModelFactory
- The static
ViewModelProvider.AndroidViewModelFactory.getInstance(Application)
method has been added to retrieve a staticFactory
suitable for creatingViewModel
andAndroidViewModel
instances.