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?
Asked
Active
Viewed 6,644 times
24

Junior Mourao
- 273
- 2
- 7
-
There is a good chance that this is a buggy Lint rule. You might consider creating a project that demonstrates the problem and [file an issue](https://issuetracker.google.com/). – CommonsWare Feb 17 '22 at 19:36
-
4@CommonsWare - nope, that's a legit warning - you need to repeat the `viewLifecycleOwner` a second time inside the `launch` as per [the docs](https://developer.android.com/topic/libraries/architecture/coroutines#restart). – ianhanniballake Feb 17 '22 at 20:35
-
I found this about the issue: https://issuetracker.google.com/issues/209658963 It's probably a false positive. Is there any way to fix it? – Junior Mourao Feb 17 '22 at 20:38
-
Just so this issue is found easier: The error is "The repeatOnLifecycle API should be used with viewLifecycleOwner" and the lint is supressed via @SuppressLint("UnsafeRepeatOnLifecycleDetector") – m.reiter Feb 23 '22 at 13:52
-
I had this come up in one file and not in another. Pretty inexplicable :( – Daniel Wilson Oct 07 '22 at 19:41
-
@ianhanniballake Even after reading the docs, I'm unclear why viewLifecycleOwner needs to be used for fragments? – Muhammad Babar Jan 18 '23 at 06:20
1 Answers
60
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}
}
}

ianhanniballake
- 191,609
- 30
- 470
- 443
-
Thanks for the clarification. The JavaDoc of `repeatOnLifecycle()` should be updated since it still mentions `lifecycleScope.launch { repeatOnLifecycle(...) }`. Version 2.4.1 of `lifecycle-runtime-ktx`. – Sven Jacobs May 04 '22 at 09:22
-
@SvenJacobs - the Javadoc clearly shows it is being used in an Activity, where that code is absolutely correct. Just because it is correct in an Activity, does not make it correct in a Fragment, hence this explicit Lint warning and the documentation I linked to. – ianhanniballake May 04 '22 at 13:30
-
Sorry I missed the part about Activity. But maybe the documentation could be extended with a Fragment example, too? ;) – Sven Jacobs May 04 '22 at 14:16
-
1Not sure why it is not implicitly using viewLifecycleOwner, we are calling repeatOnLifecycle from coroutine which is being launched with viewLifecycleOwner, so automatically calling repeatOnLifecycle inside the coroutine is done with viewLifecycleOwner – Roshaan Farrukh May 11 '22 at 07:38