Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is a one-shot callback for work that needs to be done in a specific lifecycle state. Use case I had for this was waiting for an app the be in a started state before performing a Fragment transaction. For example: ``` // In Activity.onCreate() viewModel.events() .onEach { event -> if (event is Event.FragmentNavigate) { // User might have backgrounded the app at this point, so // wait til it's safe to navigate. lifecycle.doOnEvent(Lifecycle.Event.ON_START) { supportFragmentManager.commit { replace(android.R.id.content, MyFragment() } } } } .launchIn(lifecycleScope) ``` There exists Lifecycle.whenStarted which can achieve the same as the above, but it needlessly launches a coroutine, and the API is documented that it'll be removed entirely in a future update.
- Loading branch information