Coroutines with Lifecycle and LiveData
In this tutorial, you’ll build an Android app that uses coroutines with LiveData objects and lifecycle-aware CoroutineScopes. By Husayn Hakeem.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Coroutines with Lifecycle and LiveData
20 mins
- Getting Started
- Using Coroutines With LiveData
- Adding the LiveData Builder Dependency
- Emitting Single Values Through LiveData With Coroutines
- Displaying User Information
- Emitting Values From a LiveData Source With Coroutines
- Displaying the User’s Total Investment Amount
- Transforming LiveData With Coroutines
- Displaying the User’s Investments
- Using ViewModelScope
- Adding the ViewModelScope Dependency
- Displaying a Stock Recommendation
- Refreshing the Stock Recommendation
- Using Lifecycle-Aware Coroutine Scopes
- Adding the LifecycleScope Dependency
- Displaying a Dialog on the Screen’s First Launch
- Where to Go From Here?
Using Lifecycle-Aware Coroutine Scopes
Every Lifecycle comes with a LifecycleScope, which lets you launch coroutines that are automatically cancelled once the Lifecycle reaches the DESTROYED
state.
You can access the LifecycleScope from a Lifecycle using the coroutineScope
attribute. You could also access it from a LifecycleOwner, like an Activity or Fragment, using the lifecycleScope
attribute.
The LifecycleScope provides convenient methods that are lifecycle-aware and run a block of code. Each method only runs the block once the lifecycle reaches a certain state, based on the name of the method.
launchWhenCreated(block)
launchWhenStarted(block)
launchWhenResumed(block)
Adding the LifecycleScope Dependency
To use LifecycleScope, add the following dependency to the project’s app/build.gradle:
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.2.0"
Displaying a Dialog on the Screen’s First Launch
You’re proud of the stock recommendation feature your app provides, and you want to encourage the user to use it. So, display an informative dialog the first time the profile screen launches.
Open ProfileFragment.kt and add the following block, which launches a Dialog the first time the fragment’s launched:
// 1
init {
// 2
lifecycleScope.launchWhenResumed {
if (isFirstProfileLaunch()) {
displayTipDialog()
}
}
}
In the code above, you’re attempting to display the Dialog. This logic runs on the Fragment’s construction.
- You use the
init()
block of the Fragment even though it shouldn’t launch the dialog before it starts and its UI displays. It’s safe because you’re using the ProfileFragment’s LifecycleScope. - Use
launchWhenResumed()
to ensure the dialog isn’t displayed until ProfileFragment is at least in the resumed state. You also could uselaunchWhenCreated()
orlaunchWhenStarted()
, since displaying the Dialog only requires a Context.
Build and run the app. You’ll see a dialog pop up once the screen displays.
If you close the dialog by clicking the Ok button, it won’t show when you launch the app again. However, if you dismiss it by clicking the back button or elsewhere on the screen, it’ll show the next time you open the app.
And there you have it, your awesome trading app’s profile screen!
Where to Go From Here?
Download the final project using the Download Materials button at the top or bottom of the tutorial.
In this tutorial, you learned how to use coroutines with LiveData with the help of the LiveData builder. You also learned how to use lifecycle-aware CoroutineScopes.
This project is a beginning but there’s so much more you can add. For instance, you could replace the dummy data with real data from a server or local database. Or you could make the user information fields editable.
I hope you enjoyed this tutorial! If you have any questions or comments, please join the forum discussion below.