Wex
Wex

Reputation: 4686

How do I test the "App Process Killed" route on Android?

In the flow diagram for an Android app activity lifecycle (shown below) there is a route by which the 'App process' is killed and onDestroy() is not called. It seems this is most commonly done to free up memory resources for a different activity.

All that is fine, but how do I test this scenario? Either on device or in the simulator.

enter image description here

Upvotes: 4

Views: 1741

Answers (3)

Bö macht Blau
Bö macht Blau

Reputation: 13009

If you force stop your app, all BroadcastReceivers and also app widgets, which extend BroadcastReceiver, will stop working. See also this SO post by Commonsware

So force stopping the app is not ideal for testing app behavior under low memory conditions. What else can you do?

  • One option: write your own task killer app and use ActivityManager.killBackgroundProcesses(). As the documentation says:

This is the same as the kernel killing those processes to reclaim memory

Upvotes: 2

vilpe89
vilpe89

Reputation: 4702

Steps:

  1. Navigate to your app
  2. Press home button so the app will not get the onDestroy call
  3. Go to system settings and find the right place to "force close" your app
  4. Navigate back to your app

Important note: DO NOT use back button when leaving your app when you are going to system settings, use home button instead so the app does not get killed.

Upvotes: 0

WarrenFaith
WarrenFaith

Reputation: 57672

The easiest and most clean solution to test those life cycle is to enable the "Don't Keep Activities" settings in the developer settings on your device.

This way you even get your activity killed as soon as you start a new activity. So if you press back than the old activity will be recreated.

Upvotes: -1

Related Questions