Android-components: Fragment.consumeFrom(): Lifecycle issue

Created on 14 Aug 2019  路  3Comments  路  Source: mozilla-mobile/android-components

Fenix issue: https://github.com/mozilla-mobile/fenix/issues/4725

We have a lifecycle issue in Fragment.consumeFrom().

We are using a scope that is bound to the View being attached/detached. However that happens briefly after the fragment got detached:

onAttach()
onCreateView()
onViewCreated()  <-- scope created
  consumeFrom()
  consumeFrom()
  consumeFrom()
  consumeFrom()
onDestroyView()
onDetach()
onViewDetachedFromWindow() <-- scope cancelled

If a consumeFrom lambda manages to run exactly between the fragment being detached and the view not yet then this causes us to run on a detached fragment:

onAttach()
onCreateView()
onViewCreated()  <-- scope created
  consumeFrom()
  consumeFrom()
  consumeFrom()
  consumeFrom()
onDestroyView()
onDetach()
  consumeFrom() <-- should explode since fragment is detached and context etc. not available
onViewDetachedFromWindow() <-- scope cancelled

Unfortunately that's super hard to reproduce. That's why we do not see it that often on Sentry. However with many users unlikely events still happen quite often.

<state> 馃悶 bug

All 3 comments

Interestingly sometimes it happens in the expected order:

[..]
  consumeFrom()
onViewDetachedFromWindow()
onDestroyView()
onDetach()

For some reason I can't reproduce it anymore. Now it's always in order:

onViewDetachedFromWindow()
onDestroyView()
onDetach()

馃

Okay, lol. Coming from the home screen it's always:

                         W  onDestroyView()
                         W  onDetach()
                         W  onViewDetachedFromWindow()

But from the browser it is:

                         W  onViewDetachedFromWindow()
                         W  onDestroyView()
                         W  onDetach()

馃し鈥嶁檪

Was this page helpful?
0 / 5 - 0 ratings