A fragment observes data for its view with itself as the lifecycle owner, which outlives the view
What it finds
A fragment's view is destroyed and recreated while the fragment lives on. Observing LiveData with this in onCreateView or onViewCreated keeps the old observer when a new view is made, so updates arrive twice or touch a destroyed view. The fragment documentation uses viewLifecycleOwner for observers of the view.
From the rule's source, fragment_lifecycle_owner.rs.
class ProfileFragment : Fragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { model.name.observe(this) { show(it) }tx0 reports: observing with the fragment as owner outlives its view model.name.observe(viewLifecycleOwner) { show(it) } } override fun onCreate(savedInstanceState: Bundle?) { model.name.observe(this) { show(it) } }} Turn it on, off or suppress it
The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/kotlin:fragment-view-observed-with-fragment" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/kotlin:fragment-view-observed-with-fragment] reason="..."tx0 explain tx0/kotlin:fragment-view-observed-with-fragment shows the same in the terminal. Found a false positive? Report it.