Android Activity Lifecycle Cheat Sheet

Eric Silverberg
Perry Street Software Engineering
3 min readMar 9, 2021
The red bar is the legacy Activity Lifecycle

Every developer will ask herself these questions when writing a new activity — here are quick answers when doing so using our Clean MVVM Activity Lifecycle:

Where do I initialize my ViewModel?

→ Lazily as a member variable:

private val viewModelFactory: FeatureViewModelFactory by inject()
private val viewModel: FeatureViewModel by viewModels { viewModelFactory }

Where do I set up the views and the click listeners?

onSetupViews(), one of our custom Clean MVVM Lifecycle methods

Where do I set up the state change subscription? (LiveData)

onSetupLiveDataEventSubscriptions(), one of our custom Clean MVVM Lifecycle methods

Where do I set up the events subscription? (RxJava)

onSetupAliveActivityRxJavaEventSubscriptions() for subscriptions that we want to retain even if the app is backgrounded (most of the cases)

onSetupVisibleActivityRxJavaEventSubscriptions() for subscriptions that we want to cancel when the app is backgrounded

Where do I handle deep links?

onInitializationComplete(), one of our custom Clean MVVM Lifecycle methods

Where do I parse notifications intents?

→ Also in onInitializationComplete()

Activity template

Here is an example of our template that we use for Activities (inheriting from a custom AppCompatActivity; adjust to inherit from your own)

Fragment template

Here is an example of our template that we use for Fragments (inheriting from a custom Fragment; adjust to inherit from your own)

Who implements the Clean MVVM Lifecycle methods?

A keen observer should now point out that these special new lifecycle methods are all well and good, but who actually defines those? The answer, of course, is a custom parent Activity. Below we provide an abridged version of our PSSAppCompatActivity:

Clean MVVM Activity Lifecycle summarized

In this series we first pointed out the incompleteness of the common Android Activity lifecycle architectural pattern, specifically when it relates to how and when to access application-scoped content.

We next dived into the challenges related to Android process death.

We proposed a new lifecycle, the Clean MVVM Activity Lifecycle, using standard Android Activity lifecycle methods as entry points into our own, asynchronous and Rx/LiveData-friendly Activity lifecycle.

We explored how repository initialization works, given the requirement that initialization can (and will!) happen anytime any activity is started because of Android process death.

We have shared with you concrete examples and tips on how and when to use the different methods in our proposed Clean MVVM Activity lifecycle.

Follow these patterns and you will prevent Android process death related bugs, reduce crashes, make your users happier and make your life as a developer much easier! 🤖🎉

Finally, if you would like additional information about practical ways to simulate and debug an Android process death scenario, see the link below in “Further Reading”.

Further reading

One more thing…

If you’re a mobile developer who enjoys app architecture, are interested in working for a values-first app company serving the queer community, talk to us! Visit https://www.scruff.com/careers for info about jobs at Perry Street Software

Other series you might like

Clean API Architecture (2021)
Classes, execution patterns, and abstractions when building a modern API endpoint.

Kotlin in Xcode? Swift in Android Studio? (2020)
A series on using Clean + MVVM for consistent architecture on iOS & Android

About the authors

Eric Silverberg and Stelios Frantzeskakis are developers for Perry Street Software, publishers of the LGBTQ+ dating apps SCRUFF and Jack’d, with more than 20M members worldwide.

--

--