Debugging Android process death
Learn this now or spend š¤·āāļø in Crashlytics later
In this series, we spoke at length about how Android process death can trigger crashing on apps that do not follow a Clean MVVM Application lifecycle pattern. In order to debug Android process death, you need to first trigger a process death, and then you need to restore your application to see how it behaves.
Step 1: Trigger process death
Method A: Use adb
Put your app into the background by pressing the home button
Kill the app using adb
:
adb shell am kill <YOUR APPLICATION ID>
ā DONāT use the āStopā button of Android Studio to kill the process. This will not simulate a process death and it will falsely make you believe that your app is protected. This will indeed kill the app, but next time you launch it, it will go through the normal flow of the LAUNCHER Activity, instead of re-creating the Activity you were in before moving the app to the background.
Method B: Do it naturally
You can also use the following technique if you donāt have adb
access for some reason:
From Developer Options, set the Background process limit to No background processes
Move the app to the background by pressing the home button
Launch a few other apps, like Phone or Messages or Chrome.
Tip: Use the Android Studioās āAttach Debugger to Android Processā window to know when your app has actually been killed.
Step 2: Restore your app and see how it behaves
When the process has been killed, use the square āapp switcherā aka Recents screen to navigate back into your app. This will cause the OS to try to resume your app directly from the last screen it was on.
Additional tips
If there is a crash with a small amount of logs in Crashlytics or if you see logs for an Activity getting created without seeing the launch activity of your app being created, then if itās not a deep link or a notification, itās almost certainly a process death scenario that you can reproduce by following the steps above.
Debugging the back stack chain
Thereās an additional tricky part to debugging android process death:
Only the Activity that you were in will be recreated once you foreground the app.
That means that every time you tap the Back arrow, the parent activity will be picked up from the back stack and get recreated as well. So essentially, all Activities in the back stack will experience restoration from a process death and the state of the Application could vary, depending on the operations that the recreated activities have performed (i.e. whether they have modified any application-scoped data).
When debugging process death for Activity A, try doing so both by backgrounding Activity A itself, and from child Activity B + pressing Back to return to Activity A
A note about fragments
When an Activity creates a Fragment, and you background the app while the Fragment is visible, then when the Activity is restored after a process death, it is not guaranteed that the onCreate()
of the Activity will have completed before the onCreate()
of the Fragment. So if you have shared data between the Activity and the Fragment, you might crash if the Fragment tries to access them before the Activity initializes them.
Further reading
- Android Activity Lifecycle considered harmful
- Android process death
- Clean MVVM Activity Lifecycle
- Repository Initialization
- Activity Lifecycle Cheat Sheet
- Debugging Android process death ā you are here
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.