Android View LifecycleOwner Extensions = Lifecycle + LifecycleScope on any View

Thomas Gorisse
4 min readOct 19, 2021

--

View Lifecycle has always been a big subject on Android. Comparing to Activity and Fragment which have well known lifecycle states (onCreate, onResume, …, onDestroy) the view states had never been very clear.

Even if you could say that we can deal with the top view container (Activity or Fragment) to manage child views states, they are a lot of cases where we need to know the view state independently from the Activity/Fragment like recycler items, custom views or just because some actions are linked to a view and should not be exposed at the Activity level (View animation start/stop, view countdown timer,…)

I know that you are maybe thinking

Why not just using onAttachedToWindow() and onDetachedFromWindow()?

First, because those two functions, just like their naming (what is a Window on Android?) are not clear and secondly because in 12 years of Android development using it, I never really understood when they are called and just used it just because there were no other callback on view states. Even if we can also add an OnAttachStateChangeListener to every view without having to create a custom view, since the listener interface has two java functions, we have to create boilerplate code with an object:OnAttachStateChangeListener instead of just having a lambda callback.

So, what can we do to have a clear reusable LifecycleOwner just like we have on Activity and Fragment and without having to extend every Android View we use?

Thanks to Kotlin Extensions, we will be able to create some View extension functions in a separate file and use it everywhere in our project.

Kotlin extension functions are not perfect and should be used with caution but are exactly what we need here.

How to retrieve the View LifecycleOwner and keep the reference to it on an extension function?

  • We will locate the LifecycleOwner responsible for managing the View once the View is attach and apply the Fragment/Activity state to it.
  • To keep the Lifecycle reference without having access to a variable we put it on a view tag (Extension function do not have instances).

And that’s it!!!

This function is calling the newly introduced findViewTreeLifecycleOwner() which goes up in the view tree to retrieve the first parent implementing LifecycleOwner = First parent Fragment or parent Activity

Take care that the call to findViewTreeLifecycleOwner() is done after the view attachment else it will return null. But our function has it own lifecycle (LifecycleRegistry implementation of the abstract Lifecycle class) which is in state INITIALISE until attached.

You can also add more functions around this one in order to make a cleaner code reusing like:

The complete View extension class containing all you need is here:

GitHub/ThomasGorisse/AndroidViewLifecycle/View.kt

Fill free to copy/paste it and visit the repository for samples.

Or Just add the dependency if you use it in multiple projects:

Now let’s talk about what a Lifecycle and LifecycleScope can help you doing:

Remember, no need for custom view since all properties and functions are defined as View extensions.

Samples

View Lifecycle

Listen or execute when a View lifecycle state occurred

  • Invoke an action every time the view becomes resumed
  • Invoke actions once, when the view is at least at a state

= If the view is already resumed the action will be performed immediately, otherwise the action will be performed after the view is next resumed in all the cases, your action will be invoked only one time.

  • Perform an action when the view is destroyed:

View LifecycleScope

Launch a coroutine and auto cancel it when the view sate is destroyed

  • Launch and run a given block when the view Lifecycle state is at least resumed. The returned Job will be cancelled when the view is destroyed

= When the view is detached or the Fragment or FragmentActivity container is destroyed, if loadNetworkData() is not finished, the network call is cancelled.

  • Launch a repeating call and auto cancel it when the view is destroyed (TimerTask Coroutine equivalent). The returned Job will be cancelled when the view is destroyed.

= When the view is detached or the Fragment or FragmentActivity container is destroyed, the while loop stops.

--

--

Thomas Gorisse
Thomas Gorisse

Written by Thomas Gorisse

Senior Android/XR Engineer @ DigitalMate

Responses (2)