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.

Leave a rating/review
Download materials
Save for later
Share
You are currently viewing page 3 of 3 of this article. Click here to view the first page.

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.

  1. 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.
  2. Use launchWhenResumed() to ensure the dialog isn’t displayed until ProfileFragment is at least in the resumed state. You also could use launchWhenCreated() or launchWhenStarted(), 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.

LifecycleScope

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.