When users switch away from an Android application to answer a phone call, take a photo, or browse another app, the Android low-memory killer daemon (lmkd) may destroy background activities to reclaim physical RAM. If an application fails to save and restore its state correctly via onSaveInstanceState or ViewModel SavedStateHandle, returning to the application causes crashes, lost form data, or blank screens. The Don’t keep activities toggle in Android Developer Options provides developers and testers with an immediate way to simulate aggressive memory destruction without needing a physical low-memory environment.
How Don’t Keep Activities Operates
Under standard operating conditions, Android keeps paused activities cached in memory in the back stack so users can resume them instantly. When “Don’t keep activities” is enabled, the Android framework deliberately calls onDestroy() on every activity the moment the user navigates away or moves another activity to the foreground. When the user returns via the Recent Apps screen or Back button, the framework forces the activity to recreate from scratch, validating your saved bundle persistence logic.
Step-by-Step Practical Configuration
Step 1: Ensure Developer Options Is Enabled
- Open the Settings app on your Android device.
- Scroll to the bottom and tap About phone (or About tablet).
- Locate Build number (under Software information on Samsung Galaxy devices).
- Tap Build number seven consecutive times.
- Enter your lock screen PIN or pattern. A confirmation toast will display: “You are now a developer!”
Step 2: Access the Apps Category in Developer Options
- Navigate back to Settings > System > Developer options.
- Scroll down to the Apps header.
- Locate the Don’t keep activities toggle.
Step 3: Enable the Toggle and Test Your Applications
- Switch the Don’t keep activities toggle to On.
- Open the application you are auditing or developing.
- Fill in a multi-step form, apply list filters, or navigate deep into a detail screen.
- Press the Home button to send the app to the background.
- Open another app (e.g. Chrome or Calculator) to ensure the system context switches.
- Reopen your target application from the Recent Apps screen.
- Verify Behavior: Check whether input fields, scroll offsets, and active state were restored cleanly, or if the application crashed with a
NullPointerException.
Comparison of Android Memory Testing Methods
| Testing Method | Configuration | Destruction Target | Process Impact | Primary Diagnostic Focus |
|---|---|---|---|---|
| Don’t Keep Activities | Developer Options > Apps | Every activity on transition | Process remains alive; Activity destroyed | Testing onSaveInstanceState and Bundle serialization |
| ADB Process Kill | adb shell am kill <package> |
Entire Application Process | Complete process termination | Testing full cold-start state reconstruction from SavedState |
| Background Process Limit | Developer Options > Apps | Cached background processes | Restricts process count to 1-4 | Testing system-wide multitasking degradation under load |
| Fill RAM Scripts | Third-party memory stress tools | Free Physical RAM | Triggers native lmkd pressure events |
Validating low-memory trim callbacks (onTrimMemory) |
Best Practices and Operational Notes
- Process vs Activity Destruction: Note that “Don’t keep activities” destroys the
Activityinstance while leaving the application process and its static singleton memory intact. To test full process death where static variables are also wiped, useadb shell am kill <package_name>while the app is in the background. - Remember to Disable After Testing: Leaving “Don’t keep activities” enabled during everyday smartphone usage significantly degrades battery life and performance, as every single app (including Settings, Phone, and Browsers) will be forced to recreate views upon multitasking. Always switch it back to Off after testing.
- Orientation Changes: Activity recreation also occurs naturally during screen orientation rotations. However, testing with “Don’t keep activities” ensures state survives navigation transitions between separate app activities as well.