Simulate killing activity in an emulator

I would like to check onSaveInstanceState and onRestoreInstanceState for my application on emulator.

I found this one that says we could simulate this when changing orientation, but I keep some of my variables at the application level (subclass android.app.Application ), so changing the orientation does not destroy the variables.

So my question is: how can I simulate a low memory situation, thus killing my actions?

I hope I get it. Thanks

+20
android android-activity activity-lifecycle
Mar 13 2018-11-11T00:
source share
7 answers

You can pause your application (by clicking the "Home" button, simulating a call, whatever). Then kill the application process through adb. Since the docs say that after onPause () returns your application, it can be killed without further notice, this is an honest test.

+21
Mar 13 2018-11-11T00:
source share

To test the onSaveInstanceState and onRestoreInstanceState events, you can use the SetAlwaysFinish utility (see link below). It uses the hidden Always Finish system parameter to change the behavior of the Android system. If the "Always Ready" option is enabled, the Android OS will immediately unload any activity that moves from the foreground to the background, causing its onSaveInstanceState event. Then, when the system needs activity again, it is forced to reboot it by raising the onRestoreInstanceState event.

The advantage of this method compared to killing a process is that it is simpler and you get finer control (activity level, not process level).

For more information about the tool and its use, see the blog below: http://bricolsoftconsulting.com/how-to-test-onsaveinstancestate-and-onrestoreinstancestate-on-a-real-device/

The Always Finish function is also present in the emulator in the DevTools application under Development Settings> Destroy Actions Immediately. However, the DevTools application does not work outside the emulator.

+15
Dec 23 2018-11-23T00:
source share

There is a more robust solution for Android 6 and newer. I have successfully used the following method on Android 6, 7 and 8:

1) Open the application, go to the action you want to test, and leave it by clicking the "Home" button

2) Open the "Settings" application, go to "System" - "Developer Settings", then click "Service Execution"

3) Click "Show cached processes" in the upper right corner, then find your application in the list, click on it, and then click the "Stop" button.

4) Use the latest list of applications to return to the application. It should restart the entire application in the form in which you previously left it.

I found that this is enough to completely kill the application, including any delegated applications and the state of its own C / C ++ code, and then check that it restarts with the saved activity status ... This effectively replicates what happens when the user leaves applications for a while, and then the system kills him to save memory, battery or something else.

+7
Sep 14 '17 at 19:57
source share

You can use the "Do nothing" setting in the developer’s settings or kill the application process yourself.

To kill a process, open the action you want to test, then press the "home" button to send your application in the background, and then using DDMS in Android Studio (Android Device Monitor), select the process and then stop the process (as seen in the image below). Your application has been killed. Now open the application again (access to the list of open applications). Now you can check the killed state.

enter image description here

+6
Jun 22 '16 at 20:56 on
source share

Let's clarify your question.

But before we do this, it's a fact about onSaveInstanceState - its call is called for various reasons, such as:

  • orientation change
  • transition from foreground to background (by pressing the home button or launching another action or click)
  • the system is in low memory

Now, if your question is, “How can I simulate an onSaveInstance call just for testing purposes,” then Theo and Ted are the correct answer. Both changing the parameters of the developer’s parameters and clicking on the house will result in a call to onSaveInstance.

But it will just be a change of orientation (as you noted in your question), which makes me think that you can ask: "How can I simulate a call to the onSaveInstance method when the system is under low memory pressure"

The short answer to this question is the lack of an automated way to simulate a low memory state. The above methods only mimic the onSaveInstanceState method call, not the low memory condition as such

A few caveats here.

The first caveat to this is that under extreme conditions, the kernel "uncleanly" kills your process to require memory, which means that onSaveInstanceState will never be called. In these situations, testing onSaveInstanceState is controversial. There is not much you can do with this scenario, but the good news is that its a very rare esp event. for foreground operations.

The second caveat is that the ActivityManager can return your activity resource by "killing it cleanly", i.e. your onSaveInstance will be called. This happens with actions that are not in the foreground (i.e. not visible to the user, so that they are already in a stopped state), and the system is under pressure from the memory.

In this second caution, you cannot automatically simulate this condition. This can be done if you start working with heavy loads manually and hope that the ActivityManager will be forced to process one of your actions.

The more it is worth remembering, the more there is no need to simulate a low memory state. While you simulate how onSaveInstanceState is called, you automatically check the condition under which it can be called for low memory situations. And the easiest way to call this method is to change the orientation on the emulator (ctrl-f11). And if you use the actual device to temporarily change the developer settings ("Do not perform actions")

+5
Feb 24 '13 at 2:39
source share

From android doc, http://developer.android.com/reference/android/content/ComponentCallbacks.html#onLowMemory ()

This is initiated by the system. I think you can set the size of the device plunger when creating an Android virtual device. (In the "Hardware" section, select "Device Size")

0
Mar 13 2018-11-11T00:
source share

The simplest solution that works for me is to simply change the orientation of the device (you can do this regardless of whether you use AVD, Genymotion or any other emulator). By default, the system recreates the Activity by calling onDestory and then onCreate .

Make sure the Activity does not have the attribute below on AndroidManifest.xml

 android:configChanges="orientation" 
0
May 28 '15 at 9:04
source share



All Articles