Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio doesn't let me use repeatOnLifecycle

I want to observe data inside my fragment from viewModel, but Android Studio keeps triggering this warning. Can someone help with this problem? Can this problem somehow be related to Android Studio Bumbleblee's update? enter image description here

like image 336
Junior Mourao Avatar asked Sep 02 '25 15:09

Junior Mourao


1 Answers

When you write

viewLifecycleOwner.lifecycleScope.launch {
  repeatOnLifecycle(Lifecycle.State.STARTED) {
    // {code to collect from viewModel}
  }
}

The repeatOnLifecycle is an extension on a LifecycleOwner - here, you are implicitly using this - i.e., the Fragment's Lifeycle and most important not the Fragment View Lifecycle.

As seen in the documentation, you should explicitly be using viewLifecycleOwner.repeatOnLifecycle, which is exactly what the Lint check is telling you to use:

viewLifecycleOwner.lifecycleScope.launch {
  viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
    // {code to collect from viewModel}
  }
}
like image 121
ianhanniballake Avatar answered Sep 05 '25 05:09

ianhanniballake