Android may terminate an app’s process while it is in the background, causing unexpected crashes when the user returns. This phenomenon is known as process death, triggered by OS memory pressure.
This article explains how to reproduce process death reliably using ADB and how to guard your app against fatal crashes.
1. Reproduce Process Death with ADB
Rare background‑return crashes can be reproduced instantly using:
コード
adb shell am kill <your.package.name>
Steps
- Launch the app and navigate to a deeper screen.
- Press Home to send the app to background.
- Run the ADB command above.
- Select the app from Recents to resume.
If the app crashes, your restore logic is incomplete.
2. Why Guard Logic Is Better Than Full Restoration
Ideally, onSaveInstanceState() should save all data, but large parameter sets increase complexity and bug risk.
Instead, assume data may be missing after process death and guard every critical access.
「データが消失していることを前提としたエラー処理」 (PDFより引用)
3. Example: Index Range Validation
When the Application‑level data becomes empty after process death, lookup functions may return -1. Using this invalid index directly causes ArrayIndexOutOfBoundsException.
Guard Example
java
mPosition = adapter.getFileNamePosition(common.getSelectName());
if (mPosition < 0 || mPosition >= adapter.getItemCount()) {
Log.e(TAG, "Data missing. Returning to previous screen.");
getParentFragmentManager().popBackStack();
return;
}
This prevents fatal crashes and safely returns the user to a stable screen.
4. Why This Approach Works
✔ Simple
No need to serialize large bundles.
✔ Prevents Fatal Exceptions
Users avoid the “app suddenly closed” experience.
✔ Maintainable
Every screen follows the rule: If data is missing, exit safely.
5. Summary
Use ADB to simulate process death and verify your app’s recovery behavior. By assuming data loss and adding guard logic, you can build a stable, crash‑resistant Android application with minimal cost.



コメント