55

How can I test all of the following methods code? I want to play scenarios when all of them are happening to see if my code works for save/restore process of an activity. So what should I do in the Emulator to get all methods tested?

public class Activity extends ApplicationContext {
     protected void onCreate(Bundle savedInstanceState);

     protected void onStart();

     protected void onRestoreInstanceState(Bundle savedInstanceState);

     protected void onSaveInstanceState(Bundle savedInstanceState);

     protected void onRestart();

     protected void onResume();

     protected void onPause();

     protected void onStop();

     protected void onDestroy();
 }
Pentium10
  • 204,586
  • 122
  • 423
  • 502

5 Answers5

172

If you have enabled Developer Options on your device, there is an option Do not keep activities that will help test onRestoreInstanceState().

Android Settings panel

Joshua Pinter
  • 45,245
  • 23
  • 243
  • 245
user338519
  • 2,189
  • 1
  • 15
  • 8
  • 16
    This should be the accepted answer, because it devlivers predicatble results (you do not have to open a random number of random apps) and you do not need to install a third-party-app with root access. – Habizzle Feb 04 '14 at 12:33
  • @max.mustermann Not only it delivers predictable results but that is the intended method for testing state saving&restoration. – Pijusn Aug 23 '14 at 21:12
  • On my device there was no such checkbox; instead there was "Background processes limit". But for some reason options "No background processes" and "At most 1 process" didn't work out; apps were just destroyed with no instanceState saved. Howewer, "At most 2 processes" worked fine - you just have to switch through at least three apps to force system to destory your app. Maybe it will save someone's time. – Roman Golyshev Mar 15 '16 at 16:39
  • @RomanGolyshev please state the Android version and name of your device/emulator. Your device seems old or maybe you aren't looking in the **Developer Options**. – Sufian May 06 '17 at 06:19
  • 4
    Don't keep activities does not kill the process https://youtu.be/SlZVYkhoSq8?t=36m23s – Maciej Beimcik May 16 '18 at 14:33
  • Confirmed, this works great for testing both `onSaveInstanceState` and `onRestoreInstanceState`. And, honestly, is something you absolutely should do when calling an "external" `Intents`, like `ACTION_IMAGE_CAPTURE`. On less-capable devices, it's surprisingly how often Android will suspend your activity when going to an "external" `Intent`. – Joshua Pinter Jun 05 '18 at 04:15
  • I enabled this and uploaded my app. Clicked on a button in the first activity to open the second activity and then minimized and restored the app. The entire app seems to get restarted and I'm at the first activity but I would like only the second activity to restart. I'm using a Xiaomi device with Android 7.1.2 – Spikatrix Mar 10 '19 at 06:13
  • This does not work anymore (Android 11). See Maciej comment above, see Richard Szalay answer. – arberg Feb 11 '21 at 13:35
18

We had an issue whereby re-launching an app after long periods of inactivity crashed. We found that "Don't keep activities" wasn't reproducing the issue, but Background process settings :: No background processes in Dev Settings did (even while debugging).

Richard Szalay
  • 83,269
  • 19
  • 178
  • 237
  • 1
    Awesome. I checked No background processes and it works. I'm in 5.1.1. Thanks – thuongle Apr 24 '16 at 07:08
  • I did this and uploaded my app. Clicked on a button in the first activity to open the second activity and then minimized and restored the app. The entire app seems to get restarted and I'm at the first activity but I would like only the second activity to restart. I'm using a Xiaomi device with Android 7.1.2 – Spikatrix Mar 10 '19 at 06:15
  • On my Samsung S20 with Android 11: 'No background processes' work, but I have to start another app, then revert to my app. – arberg Feb 11 '21 at 13:37
8

To test onSaveInstanceState and onRestoreInstanceState you can use either the SetAlwaysFinish tool (see link below) or the DevTools app included with the emulator.

http://bricolsoftconsulting.com/how-to-test-onsaveinstancestate-and-onrestoreinstancestate-on-a-real-device/

Both of these apps use a hidden setting called AlwaysFinish which is part of the ActivityManagerNative class to change the behavior of the Android OS. Under the new behavior, the OS will unload any activity as soon as it leaves the screen, triggering the onSaveInstanceState event. When the OS wants to bring the activity back, it will call the onRestoreInstanceState event.

The link above explains how to use the SetAlwaysFinish app to test your app's onSaveInstanceState and onRestoreInstanceState events. If you want to use the DevTools, then enable Development Settings > Immediately destroy activities.

Theo
  • 5,963
  • 3
  • 38
  • 56
  • This is wonderful. Never knew about that and this worked perfect! – Kirk Nov 17 '12 at 02:41
  • Great info, here are some breadcrumbs for others http://developer.android.com/tools/debugging/debugging-devtools.html – Michael Levy Mar 18 '14 at 16:05
  • To save time (copy from the above link) : "Yes, ICS added this functionality into the OS as “Do not keep activities” under Settings > Developer Settings." – ror Jun 12 '20 at 06:06
4

There is another way to test these events. First you have to navigate to the specific Activity that you wanna test, then press the home button and go to the Android device monitor.

Android device monitor location

Android device monitor example

In this tool you can select a thread of the Application and kill it with the stop button. For last, you have to open the app from the history and the thread will be recreated again.

snti
  • 131
  • 3
4

The testing tools offered by Android now offer a means of writing tests that can drive an activity from one state to another, or to recreate an activity to test the save and restore flow. See the Test your app's activities Android Developers documentation page for a list of the capabilities. An example of the syntax – taken from that page – is the following:

@RunWith(AndroidJUnit4::class)
class MyTestSuite {
    @Test fun testEvent() {
        val scenario = launchActivity<MyActivity>()
        scenario.moveToState(State.CREATED)
    }
}
Adil Hussain
  • 30,049
  • 21
  • 112
  • 147
  • This seems to be the up-to-date answer! Hower, `scenario.moveToState(State.CREATED)` just starts an `InstrumentationActivityInvoker$EmptyActivity` in my case. Any idea? Other option to test onSave/RestoreInstanceState is `scenario.recreate()`. Works in my case, but simply restores from Bundle instance like after rotation, it does not use Parcelable.Creator, but this is what I want to test! – allofmex Oct 24 '21 at 18:33
  • I'm not sure if it answers your question exactly but I created [this](https://github.com/adil-hussain-84/EspressoExperiments/tree/master/app1) application project previously to demonstrate how to use the [ActivityScenario.moveToState(newState:)](https://developer.android.com/reference/androidx/test/core/app/ActivityScenario#moveToState(android.arch.lifecycle.Lifecycle.State)) method to drive an activity forwards or backwards to particular lifecycle states. – Adil Hussain Nov 01 '21 at 19:09