Skip to content

Instantly share code, notes, and snippets.

@manuelvicnt
Created May 21, 2021 15:19
Show Gist options
  • Save manuelvicnt/a26e26179b80d8a278c634cc5f7a9a78 to your computer and use it in GitHub Desktop.
Save manuelvicnt/a26e26179b80d8a278c634cc5f7a9a78 to your computer and use it in GitHub Desktop.
class LocationFragment: Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
// ...
viewLifecycleOwner.lifecycleScope.launch {
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
locationProvider.locationFlow().collect {
// New location! Update the map
}
}
}
}
}
@scaleflake
Copy link

scaleflake commented Oct 1, 2021

Facing same issue even after adding dependency: implementation "lifecycle:lifecycle-runtime-ktx:2.3.1"

Showing unresolved reference repeatOnLifecycle

@manuelvicnt can you suggest what is wrong ?

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

From here:
https://medium.com/androiddevelopers/a-safer-way-to-collect-flows-from-android-uis-23080b1f8bda

PS: you can use 2.4.0-beta01 today
PSS: some build.gradle.kts example that worked for me: implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.4.0-beta01")

@bosankus
Copy link

bosankus commented Oct 3, 2021

I updated the dependency but still the same error is showing, am I missing something ? Here is my commit The part is commented in FragmentNews.kt
@scaleflake

@VictorKabata
Copy link

implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.4.0-rc01" . This worked for me

@khaboshama
Copy link

You can add this dependence in the Gradle file
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.5.1"

@belinwu
Copy link

belinwu commented Jan 3, 2023

Should I use repeatOnLifecycle with Lifecycle.State.CREATED in Fragment for collecting UI State provided by ViewModel?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment