{"id":13611624,"url":"https://github.com/skydoves/viewmodel-lifecycle","last_synced_at":"2025-09-04T20:35:42.146Z","repository":{"id":42204552,"uuid":"454704051","full_name":"skydoves/viewmodel-lifecycle","owner":"skydoves","description":"🌳 ViewModel Lifecycle allows you to track and observe Jetpack's ViewModel lifecycle changes.","archived":false,"fork":false,"pushed_at":"2022-04-10T15:12:51.000Z","size":740,"stargazers_count":106,"open_issues_count":2,"forks_count":10,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-14T19:11:31.436Z","etag":null,"topics":["android","coroutines","jetpack-viewmodel","lifecycle","observe","viewmodel"],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/skydoves.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null},"funding":{"github":"skydoves","custom":["https://www.paypal.me/skydoves","https://www.buymeacoffee.com/skydoves"]}},"created_at":"2022-02-02T09:00:38.000Z","updated_at":"2025-01-22T00:52:56.000Z","dependencies_parsed_at":"2022-09-26T21:50:15.211Z","dependency_job_id":null,"html_url":"https://github.com/skydoves/viewmodel-lifecycle","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/skydoves/viewmodel-lifecycle","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skydoves%2Fviewmodel-lifecycle","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skydoves%2Fviewmodel-lifecycle/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skydoves%2Fviewmodel-lifecycle/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skydoves%2Fviewmodel-lifecycle/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/skydoves","download_url":"https://codeload.github.com/skydoves/viewmodel-lifecycle/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skydoves%2Fviewmodel-lifecycle/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273670032,"owners_count":25147069,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-04T02:00:08.968Z","response_time":61,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["android","coroutines","jetpack-viewmodel","lifecycle","observe","viewmodel"],"created_at":"2024-08-01T19:01:59.160Z","updated_at":"2025-09-04T20:35:42.092Z","avatar_url":"https://github.com/skydoves.png","language":"Kotlin","readme":"\u003ch1 align=\"center\"\u003eViewModel Lifecycle\u003c/h1\u003e\u003c/br\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://opensource.org/licenses/Apache-2.0\"\u003e\u003cimg alt=\"License\" src=\"https://img.shields.io/badge/License-Apache%202.0-blue.svg\"/\u003e\u003c/a\u003e\n  \u003ca href=\"https://android-arsenal.com/api?level=21\"\u003e\u003cimg alt=\"API\" src=\"https://img.shields.io/badge/API-21%2B-brightgreen.svg?style=flat\"/\u003e\u003c/a\u003e\n  \u003ca href=\"https://github.com/skydoves/ViewModel-Lifecycle/actions/workflows/android.yml\"\u003e\u003cimg alt=\"API\" src=\"https://github.com/skydoves/ViewModel-Lifecycle/actions/workflows/android.yml/badge.svg\"/\u003e\u003c/a\u003e\n  \u003ca href=\"https://github.com/skydoves\"\u003e\u003cimg alt=\"Profile\" src=\"https://skydoves.github.io/badges/skydoves.svg\"/\u003e\u003c/a\u003e\n  \u003ca href=\"https://androidweekly.net/issues/issue-506\"\u003e\u003cimg alt=\"Android Weekly\" src=\"https://skydoves.github.io/badges/android-weekly.svg\"/\u003e\u003c/a\u003e\n  \u003ca href=\"https://skydoves.github.io/libraries/viewmodel-lifecycle/html/viewmodel-lifecycle/com.skydoves.viewmodel.lifecycle/index.html\"\u003e\u003cimg alt=\"Dokka\" src=\"https://skydoves.github.io/badges/dokka-viewmodel-lifecycle.svg\"/\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n🌳 ViewModel Lifecycle allows you to track and observe Jetpack's ViewModel lifecycle changes.\u003cbr\u003e\nAlso, it supports useful extensions for RxKotlin/RxJava and Coroutines.\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n\u003cimg src=\"preview/preview.png\" /\u003e\n\u003c/p\u003e\n\n## Including in your project\n\n[![Maven Central](https://img.shields.io/maven-central/v/com.github.skydoves/viewmodel-lifecycle.svg?label=Maven%20Central)](https://search.maven.org/search?q=g:%22com.github.skydoves%22%20AND%20a:%22viewmodel-lifecycle%22)\n\n### Gradle \nAdd the code below to your **root** `build.gradle` file (not your module build.gradle file):\n```gradle\nallprojects {\n    repositories {\n        mavenCentral()\n    }\n}\n```\n\nNext, add the dependency below to your **module**'s `build.gradle` file:\n\n```gradle\ndependencies {\n    implementation \"com.github.skydoves:viewmodel-lifecycle:1.1.0\"\n}\n```\n## SNAPSHOT \n[![viewmodel-lifecycle](https://img.shields.io/static/v1?label=snapshot\u0026message=viewmodel-lifecycle\u0026logo=apache%20maven\u0026color=C71A36)](https://oss.sonatype.org/content/repositories/snapshots/com/github/skydoves/viewmodel-lifecycle/) \u003cbr\u003e\nSnapshots of the current development version of ViewModel-Lifecycle are available, which track [the latest versions](https://oss.sonatype.org/content/repositories/snapshots/com/github/skydoves/viewmodel-lifecycle/).\n\n```gradle\nrepositories {\n   maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }\n}\n```\n\n## Usage\n\n`ViewModel-Lifecycle` allows you to observe two lifecycle changes:  **initialized** and **cleared**.\n\n### ViewModelLifecycleOwner\n\n`ViewModelLifecycleOwner` is a lifecycle owner for the Jetpack's ViewModel, which extends [LifecycleOwner](https://developer.android.com/reference/androidx/lifecycle/LifecycleOwner). It traces and provides lifecycle states for ViewModels. You can get the `ViewModelLifecycleOwner` from your ViewModel as the following:\n\n```kotlin\nclass MyActivity : AppCompatActivity() {\n\n  private val viewModel by viewModels\u003cMyViewModel\u003e()\n\n  override fun onCreate(savedInstanceState: Bundle?) {\n    super.onCreate(savedInstanceState)\n    \n    val viewModelLifecycleOwner = viewModel.viewModelLifecycleOwner\n...\n```\n\nAlso, you can get it directly from your ViewModel as the following:\n\n```kotlin\nclass MyViewModel : ViewModel() {\n\n  val lifecycleOwner = viewModelLifecycleOwner\n}\n```\n\n#### ViewModelLifecycleOwner for LiveData\n\nYou can also use it to observe your [LiveData](https://developer.android.com/topic/libraries/architecture/livedata) with the `ViewModelLifecycleOwner` depending on ViewModel's lifecycle. If the lifecycle moves to the cleared state, the observer will automatically be removed.\n\n```kotlin\nclass MyViewModel : ViewModel() {\n\n  private val liveData = MutableLiveData\u003cString\u003e()\n\n  init {\n    val lifecycleOwner = liveData.observe(viewModelLifecycleOwner) {\n      // sometihng\n    }\n  }\n}\n```\n\n\u003e Note: If you use `ViewModelLifecycleOwner` to observe your LiveData, observers will receive every event before the lifecycle moves to the cleared state. But you'll not receive further events by Activity recreations such as screen rotation. So make sure which `lifecycleOwner` is the best solution.\n\n### ViewModelLifecycle\n\n`ViewModelLifecycle` is an implementation of the [Lifecycle](https://developer.android.com/jetpack/androidx/releases/lifecycle), which follows the ViewModel's lifecycle. `ViewModelLifecycle` handles multiple `LifecycleObserver` such as `ViewModelLifecycleObserver` to track ViewModel's lifecycle. `ViewModelLifecycle` belongs to `ViewModelLifecycleOwner`, and you can get it directly from the [ViewModelLifecycleOwner] as the following:\n\n```kotlin\nval viewModelLifecycle = viewModelLifecycleOwner.viewModelLifecycle\n```\n\n### ViewModelLifecycle Observers\n\nYou can observe the lifecycle changes of the `ViewModelLifecycle` with `addViewModelLifecycleObserver` extension as the following:\n\n```kotlin\nviewModel.viewModelLifecycleOwner.addViewModelLifecycleObserver { viewModelState -\u003e\n  when (viewModelState) {\n    ViewModelState.INITIALIZED -\u003e // viewModel was initialized\n    ViewModelState.CLEARED -\u003e // viewModel was cleraed\n  }\n}\n```\n\nYou can also observe the lifecycle changes of the `ViewModelLifecycle` with `addObserver` as the following:\n\n```kotlin\nviewModelLifecycleOwner.lifecycle.addObserver(\n  object : DefaultViewModelLifecycleObserver {\n    override fun onInitialized(viewModelLifecycleOwner: ViewModelLifecycleOwner) {\n        // viewModel was initialized\n    }\n\n    override fun onCleared(viewModelLifecycleOwner: ViewModelLifecycleOwner) {\n        // viewModel was cleraed\n    }\n  }\n)\n```\n\nYou can also implement your own custom lifecycle observer classes with `DefaultViewModelLifecycleObserver` and `FullViewModelLifecycleObserver` interfaces.\n\n\n\u003cimg src=\"https://user-images.githubusercontent.com/24237865/152351055-40f6e478-01dc-4ef4-be90-064cd047d789.png\" width=\"32%\" align=\"right\" /\u003e  \n\n## ViewModel Lifecycle for RxKotlin (RxJava)\n\n[![Maven Central](https://img.shields.io/maven-central/v/com.github.skydoves/viewmodel-lifecycle.svg?label=Maven%20Central)](https://search.maven.org/search?q=g:%22com.github.skydoves%22%20AND%20a:%22viewmodel-lifecycle%22)\n\nViewModel Lifecycle provides useful extensions for RxKotlin (RxJava).\n\n### Gradle\n\nAdd the dependency below to your **module's** `build.gradle` file:\n\n```gradle\ndependencies {\n    // RxKotlin3 (RxJava3)\n    implementation \"com.github.skydoves:viewmodel-lifecycle-rxkotlin3:$version\"\n\n    // RxKotlin2 (RxJava2)\n    implementation \"com.github.skydoves:viewmodel-lifecycle-rxkotlin2:$version\"\n}\n```\n\n### AutoDisposable\n\nWith `autoDisposable` extension, you can create a `Disposable` delegate property which will call the `dispose()` function automatically when ViewModel will be cleared as the following:\n\n```kotlin\nclass MyViewModel : ViewModel() {\n\n  // dispose CompositeDisposable automatically when viewModel is getting cleared\n  val compositeDisposable by autoDisposable(CompositeDisposable())\n}\n```\n\nThe `autoDisposable` extension creates a read-only property, which receives the `Disposable` interface as an inital value.\n\n\u003cimg src=\"https://user-images.githubusercontent.com/24237865/152125637-5f7830e9-c147-40f0-9d1c-951c9333179c.png\" width=\"32%\" align=\"right\" /\u003e  \n\n## ViewModel Lifecycle for Coroutines\n\n[![Maven Central](https://img.shields.io/maven-central/v/com.github.skydoves/viewmodel-lifecycle.svg?label=Maven%20Central)](https://search.maven.org/search?q=g:%22com.github.skydoves%22%20AND%20a:%22viewmodel-lifecycle%22)\n\n`ViewModel-Lifecycle` also supports Coroutines to track and observe ViewModel's lifecycle changes.\n\n### Gradle\n\nAdd the dependency below to your **module's** `build.gradle` file:\n\n```gradle\ndependencies {\n    implementation \"com.github.skydoves:viewmodel-lifecycle-coroutines:$version\"\n    implementation \"org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2\"\n}\n```\n\n### ViewModelLifecycle Flow\n\nYou can observe lifecycle changes as a Flow with `viewModelLifecycleFlow` extension as the following:\n\n```kotlin\nclass MyInteractor(\n  private val viewModelLifecycleOwner: ViewModelLifecycleOwner\n) : CoroutineScope {\n\n  override val coroutineContext: CoroutineContext = SupervisorJob() + Dispatchers.Main\n\n  init {\n    launch(coroutineContext) {\n      viewModelLifecycleOwner.viewModelLifecycleFlow().collect { viewModelState -\u003e\n        when (viewModelState) {\n          ViewModelState.INITIALIZED -\u003e // ViewModel was initialized.\n          ViewModelState.CLEARED -\u003e {\n            // ViewModel was cleared.\n            coroutineContext.cancel() // cancel the custom scope.\n          }\n        }\n      }\n    }\n  }\n}\n```\n\nMake sure you cancel your custom `CoroutineScope` after observing the `ViewModelState.CLEARED`, and the `viewModelLifecycleFlow` extension must be launched on **main** thread.\n\n## Find this library useful? :heart:\nSupport it by joining __[stargazers](https://github.com/skydoves/Lazybones/stargazers)__ for this repository. :star:\u003cbr\u003e\nAnd __[follow](https://github.com/skydoves)__ me for my next creations! 🤩\n\n# License\n```xml\nCopyright 2022 skydoves (Jaewoong Eum)\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n\n   http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the L\n","funding_links":["https://github.com/sponsors/skydoves","https://www.paypal.me/skydoves","https://www.buymeacoffee.com/skydoves"],"categories":["Kotlin"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fskydoves%2Fviewmodel-lifecycle","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fskydoves%2Fviewmodel-lifecycle","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fskydoves%2Fviewmodel-lifecycle/lists"}