Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot resolve symbol repeatOnLifecycle in Android

I'm following this article to collect flows in UI. But I couldn't resolve repeatOnLifeCycle API in my code. I have added the below dependency, though.

lifecycle:lifecycle-runtime-ktx:2.4.0-alpha03

Please helpenter image description here

like image 314
Rakesh Avatar asked Aug 04 '21 18:08

Rakesh


People also ask

What is repeatOnLifecycle?

repeatOnLifecycle is a suspend function that takes a Lifecycle. State as a parameter which is used to automatically create and launch a new coroutine with the block passed to it when the lifecycle reaches that state , and cancel the ongoing coroutine that's executing the block when the lifecycle falls below the state .

Why use repeatOnLifecycle?

The Lifecycle. repeatOnLifecycle API was primarily born to allow safer Flow collection from the UI layer in Android. Its restartable behavior, that takes into consideration the UI lifecycle, makes it the perfect default API to process items only when the UI is visible on the screen.

What is Android LifecycleScope?

A LifecycleScope is defined for each Lifecycle object. Any coroutine launched in this scope is canceled when the Lifecycle is destroyed. You can access the CoroutineScope of the Lifecycle either via lifecycle. coroutineScope or lifecycleOwner. lifecycleScope properties.


4 Answers

It's not
lifecycle:lifecycle-runtime-ktx:2.4.0-alpha03
but
androidx.lifecycle:lifecycle-runtime-ktx:2.4.0-alpha03

like image 153
amirhossein p Avatar answered Oct 07 '22 14:10

amirhossein p


I hope this gradle config can help you.

 def lifecycle_version = "2.4.0"
implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version")
implementation("androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version")
implementation("androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version")
implementation("androidx.lifecycle:lifecycle-viewmodel-savedstate:$lifecycle_version")
implementation("androidx.lifecycle:lifecycle-common-java8:$lifecycle_version")

With this set, i resolve "repeatOnLifecycle" with this import: import androidx.lifecycle.repeatOnLifecycle

like image 37
K. Donon Avatar answered Oct 07 '22 14:10

K. Donon


I also had a similar issue. For me, adding the following in the dependencies section of the build.gradle file (the App Module one and not the Project one) helped to resolve the issue:

implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.4.0-alpha03"

I also removed the import androidx.lifecycle.Lifecycle that was already there in the problematic file and got Android Studio to import the right one just in case the old one was wrong.

Found the latest version number to use(i.e. the "2.4.0-alpha03" part) from the following link but I think Android Studio would have later given a hint on the latest version to upgrade to anyway even if I hadn't done this: https://mvnrepository.com/artifact/androidx.lifecycle/lifecycle-runtime-ktx/2.2.0-alpha01

like image 5
DeveloperrepoleveD Avatar answered Oct 07 '22 14:10

DeveloperrepoleveD


Note: These APIs are available in the lifecycle:lifecycle-runtime-ktx:2.4.0-alpha01 library or later. lifecycle-runtime-ktx

like image 4
Freddy Avatar answered Oct 07 '22 15:10

Freddy