42

Android doc say:

"When the system, rather than the user, shuts down an activity to conserve memory, ... "

But how to simulate this situation?I want to debug the onRestoreInstanceState(Bundle) method,but don't know how to.

God
  • 1,238
  • 2
  • 18
  • 45
L.J.W
  • 1,575
  • 5
  • 18
  • 24

8 Answers8

35

You can't do it in an automated way b/c its completely non deterministic.

See my answer here: https://stackoverflow.com/a/15048112/909956 for details.

But good news is that all you need to do is just simulate calling onSaveInstanceState and you are indirectly testing this low memory situation.

onSaveInstanceState can be triggered by:

  1. losing focus (by pressing home which in essence is like switching from your app to launcher app), launching another activity, pressing recents
  2. changing orientation. this is the easier way if you are using an emulator
  3. changing developer setting: goto developer options --> Apps --> Don't keep activities. This is best option if you are testing temporarily on an actual device.
Community
  • 1
  • 1
numan salati
  • 19,394
  • 9
  • 63
  • 66
26

I've used the "Don't keep activities" developer option to reproduce a crash that happened when an activity was killed due to memory pressure. You can find it in the Apps section of Settings->Developer Options.

It destroys every activity as soon as you leave it. E.g. if you press home to put your app in the background the current activity is destroyed. See https://stackoverflow.com/a/22402360/2833126 for more information.

Community
  • 1
  • 1
aschmied
  • 908
  • 2
  • 10
  • 26
  • 10
    Kills the activity. Good. Does not restart app and restart to the last opened Activity - what we really need to test to test the actual scenario. – maxweber Jun 02 '15 at 00:23
13

There's two way to simulate the android killing process: using the setting "Don't keep activities" in developer settings or killing the app process by yourself.

To kill the process, open the activity you want to test, then press home button to send your app to background, and then, using the DDMS in Android Studio (Android Device Monitor), select the process and then stop the process (as seen in the image below). Your app was killed. Now, open your app again (accessing the list of open apps). Now you can test the killed state.

enter image description here

heloisasim
  • 4,956
  • 2
  • 27
  • 36
  • 1
    Thanks! Can also be done in Android Monitor window with the red Terminate Application button following the same process – Alexandre G Jan 25 '17 at 08:30
  • Wow you just helped me reproduce a bunch of weird bugs that always happened on the clients' devices and never on mine! Setting "Don't keep activities" alone seems to be different from killing the process. It does not re-create the Application object, so activities that assume stuff about the "global" state stored in the Application object are very likely to crash. – jadkik94 Feb 15 '17 at 15:58
10

For the purposes of debugging onRestoreInstanceState(), just change the screen orientation ([Ctrl]-[F11] in the emulator). Your activity will be destroyed and recreated, and the onSaveInstanceState()/onRestoreInstanceState() pair will be invoked.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 3
    This won't work for apps that control orientation on their own or have fixed orientation. – gotube Oct 13 '14 at 06:01
  • And how to test on device. Currently there is no usable emulator so testing must be don on an actual device. Also, changing orientaton is not an option if an activity is declare portrait only. – f470071 Apr 29 '17 at 19:13
  • @f470071: "And how to test on device" -- rotate the device. Or change the locale. Or change the font scale on Android 4.0+ devices. "Currently there is no usable emulator so testing must be don on an actual device" -- the Android SDK emulator works for many developers; others use Genymotion. – CommonsWare Apr 29 '17 at 19:27
  • Android SDK emulator is way to slow. On Genymotion Google Play Services cannot be installed. This is why testing is such a frustrating ordeal. Rotating device is not an option because Acitivities are set to fixed orientation. – f470071 Apr 29 '17 at 19:32
4

To debug onRestoreInstanceState you could do the following:

  • make sure you can debug application right after its start (calling android.os.Debug.waitForDebugger() from your constructor helps, it hangs your application until debugger is connected),

  • put you application in some state,

  • causally kill it from Settings->Apps,

  • causally switch back to it through Recent Apps button (it will still be in the list),

  • at this moment your application will be started anew and onRestoreInstanceState will be immediately called on the top activity.

beefeather
  • 1,040
  • 5
  • 8
4

Use the SetAlwaysFinish app (works on a real device and in the emulator) or use the Google DevTools app (works in the emulator only).

These apps use the hidden AlwaysFinish setting of the ActivityManagerNative class to change the behavior of the OS and cause it to immediate unload every activity as soon as it's no longer in the foreground. This will reliably trigger the onSaveInstanceState and onRestoreInstanceState events.

See link below for more details: http://bricolsoftconsulting.com/how-to-test-onsaveinstancestate-and-onrestoreinstancestate-on-a-real-device/

Theo
  • 5,963
  • 3
  • 38
  • 56
1

Good answers here.

Now, residing in the distant future, using Instant Run in Android Studio will also trigger a save and restore when activities are restarted with code changes.

Johnny C
  • 1,799
  • 1
  • 16
  • 27
0

There's a decent solution for this in Android 6 and newer. See my answer here: Simulate killing of activity in emulator

Wookie
  • 782
  • 8
  • 12