Lifecycle
This table lists all the artifacts in the androidx.lifecycle
group.
Artifact | Current Stable Release | Next Release Candidate | Beta Release | Alpha Release |
---|---|---|---|---|
lifecycle-* | 2.3.0 | - | - | - |
lifecycle-viewmodel-compose | - | - | - | 1.0.0-alpha02 |
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.3.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" // Jetpack Compose Integration for ViewModel implementation "androidx.lifecycle:lifecycle-viewmodel-compose:1.0.0-alpha02" // 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.3.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.
Feedback
Your feedback helps make Jetpack better. Let us know if you discover new issues or have ideas for improving this library. Please take a look at the existing issues in this library before you create a new one. You can add your vote to an existing issue by clicking the star button.
See the Issue Tracker documentation for more information.
Lifecycle Viewmodel Compose
Lifecycle-Viewmodel-Compose Version 1.0.0-alpha02
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 aprovides
functions that can be used withCompositionLocalProvider
, replacing theasProvidableCompositionLocal()
API. (I45d24)
Lifecycle-Viewmodel-Compose Version 1.0.0-alpha01
February 10, 2021
androidx.lifecycle:lifecycle-viewmodel-compose:1.0.0-alpha01
is released. Version 1.0.0-alpha01 contains these commits.
New Features
- The
viewModel()
composable andLocalViewModelStoreOwner
were moved fromandroidx.compose.ui.viewinterop
to this artifact in theandroidx.lifecycle.viewmodel.compose
package. (I7a374)
Version 2.3.0
Version 2.3.0
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 callsetSavedStateProvider()
for a given key, providing aSavedStateProvider
that will get a callback tosaveState()
when theSavedStateHandle
is asked to save its state. See Saving non-parcelable classes.- Lifecycle Behavior Enforcement:
- LifecycleRegistry now enforces
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. ForLifecycleRegistry
objects that owned by your own components, you can explicitly opt out from checks by usingLifecycleRegistry.createUnsafe(...)
, but then you have to ensure that a proper synchronization is in place when thisLifecycleRegistry
is accessed from different threads.
- LifecycleRegistry now enforces
- Lifecycle State and Event Helpers: Added static helper methods of
downFrom(State)
,downTo(State)
,upFrom(State)
,upTo(State)
toLifecycle.Event
for generating theEvent
given aState
and transition direction. Added thegetTargetState()
method that provides theState
that the Lifecycle will transition to directly following theEvent
. withStateAtLeast
: AddedLifecycle.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 existingwhen*
methods as they do not permit running suspending code and do not employ a custom dispatcher. (aosp/1326081)ViewTree
APIs: A newViewTreeLifecycleOwner.get(View)
andViewTreeViewModelStoreOwner.get(View)
API allows you to retrieve the containingLifecycleOwner
andViewModelStoreOwner
, respectively, given aView
instance. You must upgrade to Activity1.2.0
and Fragment1.3.0
, and AppCompat 1.3.0-alpha01 or higher to populate this correctly. ThefindViewTreeLifecycleOwner
andfindViewTreeViewModelStoreOwner
Kotlin extensions are available inlifecycle-runtime-ktx
andlifecycle-viewmodel-ktx
, respectively.LiveData.observe()
Kotlin extension deprecation: TheLiveData.observe()
Kotlin extension necessary to use lambda syntax is now deprecated as it is not necessary when using Kotlin 1.4.
Version 2.3.0-rc01
December 16, 2020
androidx.lifecycle:lifecycle-*:2.3.0-rc01
is released. Version 2.3.0-rc01 contains these commits.
Bug Fixes
- The
keys()
method ofSavedStateHandle
is now consistent before and after the state is saved - it now includes keys previously used withsetSavedStateProvider()
in addition to the keys used withset()
andgetLiveData()
. (aosp/1517919, b/174713653)
External Contribution
- The APIs to suspend Lifecycle-aware coroutines now better handle calls to
yield()
. Thanks Nicklas Ansman Giertz! (aosp/1430830, b/168777346)
Version 2.3.0-beta01
October 1, 2020
androidx.lifecycle:lifecycle-*:2.3.0-beta01
is released. Version 2.3.0-beta01 contains these commits.
API Changes
- The
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
- Upgrade androidx to use Kotlin 1.4 (Id6471, b/165307851, b/165300826)
Documentation Changes
- The
liveData
builder andasLiveData()
docs have been updated to include details about changing the given timeout values. (aosp/1122324)
Version 2.3.0-alpha07
August 19, 2020
androidx.lifecycle:lifecycle-*:2.3.0-alpha07
is released. Version 2.3.0-alpha07 contains these commits.
Bug Fixes
- Fixed a crash issue in the
NullSafeMutableLiveData
Lint check. (aosp/1395367)
Version 2.3.0-alpha06
July 22, 2020
androidx.lifecycle:lifecycle-*:2.3.0-alpha06
is released. Version 2.3.0-alpha06 contains these commits.
New Features
- Added static helper methods of
downFrom(State)
,downTo(State)
,upFrom(State)
,upTo(State)
toLifecycle.Event
for generating theEvent
given aState
and transition direction. Added thegetTargetState()
method that provides theState
that the Lifecycle will transition to directly following theEvent
. (I00887) - 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 existingwhen*
methods as they do not permit running suspending code and do not employ a custom dispatcher. (aosp/1326081)
Behavior Changes
- LifecycleRegistry now enforces
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. ForLifecycleRegistry
objects that owned by your own components, you can explicitly opt out from checks by usingLifecycleRegistry.createUnsafe(...)
, but then you have to ensure that a proper synchronization is in place when thisLifecycleRegistry
is accessed from different threads (Ie7280, b/137392809)
Bug Fixes
- Fixed a crash in
NullSafeMutableLiveData
. (b/159987480) - Fixed an
ObsoleteLintCustomCheck
for Lint checks bundled withlifecycle-livedata-core-ktx
(and specificallyNullSafeMutableLiveData
). (b/158699265)
Version 2.3.0-alpha05
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 toonActive()
oronInactive()
. (b/157840298)- Fixed an issue where Lint checks would not run when using Android Studio 4.1 Canary 6 or higher. (aosp/1331903)
Version 2.3.0-alpha04
June 10, 2020
androidx.lifecycle:lifecycle-*:2.3.0-alpha04
is released. Version 2.3.0-alpha04 contains these commits.
Bug Fixes
- Fixed a crash in the
NonNullableMutableLiveData
Lint check. (b/157294666) - The
NonNullableMutableLiveData
Lint check now covers significantly more cases where anull
value was set on aMutableLiveData
with a non-null type parameter. (b/156002218)
Version 2.3.0-alpha03
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 callsetSavedStateProvider()
for a given key, providing aSavedStateProvider
that will get a callback tosaveState()
when theSavedStateHandle
is asked to save its state. (b/155106862)- A new
ViewTreeViewModelStoreOwner.get(View)
API allows you to retrieve the containingViewModelStoreOwner
given aView
instance. You must upgrade to Activity1.2.0-alpha05
, Fragment1.3.0-alpha05
, and AppCompat1.3.0-alpha01
to populate this correctly. AfindViewModelStoreOwner()
Kotlin extension has been added tolifecycle-viewmodel-ktx
. (aosp/1295522)
Bug Fixes
- Fixed an issue that caused the
MutableLiveData
Lint checks released in Lifecycle2.3.0-alpha01
from being published alongside thelifecycle-livedata-core-ktx
artifact. (b/155323109)
Version 2.3.0-alpha02
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 nullApplication
to its constructor to better support cases where one is not readily available and support forAndroidViewModel
is not needed. (aosp/1285740)
Bug Fixes
- Improved cold start performance by avoiding class verification failure on API 28 and lower devices. (aosp/1282118)
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 containingLifecycleOwner
given aView
instance. You must upgrade to Activity1.2.0-alpha01
and Fragment1.3.0-alpha01
to populate this correctly. AfindViewTreeLifecycleOwner
Kotlin extension is available inlifecycle-runtime-ktx
. (aosp/1182361, aosp/1182956) - Added a new Lint check that warns you when setting a
null
value on aMutableLiveData
that has been defined in Kotlin as non-null. This is available when using thelivedata-core-ktx
orlivedata-ktx
artifacts. (aosp/1154723, aosp/1159092) - A new
lifecycle-runtime-testing
artifact is available that provides aTestLifecycleOwner
that implementsLifecycleOwner
and provides a thread safe mutableLifecycle
. (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. Thelifecycle-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 aFragment
orFragmentActivity
to the newViewModelProvider(ViewModelStoreOwner)
constructor to achieve the same functionality when using Fragment1.2.0
.lifecycle-extensions
Artifact Deprecation: With the above deprecation ofViewModelProviders.of()
, this release marks the deprecation of the last API inlifecycle-extensions
and this artifact should now be considered deprecated in its entirety. We strongly recommend depending on the specific Lifecycle artifacts you need (such aslifecycle-service
if you’re usingLifecycleService
andlifecycle-process
if you’re usingProcessLifecycleOwner
) rather thanlifecycle-extensions
as there will not be a future2.3.0
release oflifecycle-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 useLifecycleEventObserver
.
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
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 ofViewModel
class and factories provided by default by Fragments and AppCompatActivity will injectSavedStateHandle
automatically. - AbstractSavedStateViewModelFactory was added. It allows you to create custom factories for your
ViewModel
and provide them access toSavedStateHandle
.
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.