mocking memory loss configuration change - android

As I understood from the doc, saving a fragment instance should be used when the system kills the app's process when the app is in the background. Would it be possible to mock this situation to test out saving states under this specific scenario ?

Yes you can
Press home on your device
In Android Studio there is button in debug window called Close with red cross, press it
Go back to your application through latest apps on device
Thats it

Related

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.
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
Another option: manipulate the device settings as explained by Xavi Gil in his answer to Simulate low battery & low memory in Android
Steps:
Navigate to your app
Press home button so the app will not get the onDestroy call
Go to system settings and find the right place to "force close" your app
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.
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.

How can you easily test the save restore methods in android

I know that if you have to test the onrestoreinstance() the system must kill previously the application. Is there any easy way to test that? I mean not going to open tons of other applications for the system to kill it.
If your AndroidManifest.xml does not block Activity's kill-restore on device rotations, then you can just rotate the device (or emulate device rotation if on emulator).
There is also another cool tool - on the emulator by default you should have 'Dev Tools' app installed. Open it and go to 'Development Settings', then check the 'Don't keep activities' checkbox. Since this moment the activities will be killed by OS as soon as they become invisible (in the background). So pressing 'Home' button will cause a kill for your activity. Then on the home screen pressing your app icon will restore your activity.
You could open your app then kill in the via the home menu.
Device menu
Applications sub menu
Manage Applications
Find application and kill it.
That should help ya out.

Flex 4.5 - Android application keeps running in background, How can I shut it down?

I am building an Android application in Flex 4.5 and I am testing on a Samsung Galaxy S phone.
My application uses GPS and also Google maps.
When I run the application, and I try to close it, the application keeps on running in the background. I would like the application to stop when the user presses the device's home button or when the user clicks the back button until they leave the application.
How can I make the application shut down in both the cases?
This is how Android applications are supposed to work. See here: http://developer.android.com/guide/topics/fundamentals/processes-and-threads.html
Your application's process is in either the "background" state (if the user pressed how) or "empty" state (if the user pressed back).
For starters, you can try using the Activity noHistory flag in the manifest. This will make sure an Activity is finished if navigated away from.
http://developer.android.com/guide/topics/manifest/activity-element.html#nohist

How to force all activities in an application to always be destroyed (for testing purposes)?

I am basically looking for the same functionality found in the DevTools (Development.apk) app that comes with the emulator. I am wanting to perform similar testing on an actual device but the DevTools app does not work properly on the device I have so I cannot use it.
I am looking to test in a similar way.
What this does is causes each Activity to be destroyed whenever it leaves the screen, holding onto its instance state just as if the system needed resources and had killed it. (So I can't just call finish)
Is there a way to do this?
Thanks
You can kill your app's process at any time using the DDMS stop button. highlight your application in the list and click the stop sign button. your application will be destroyed like it was killed by the system.
See the stop button in the left pane(Devices) above each device listing:

How to simulate killing activity to conserve memory?

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.
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:
losing focus (by pressing home which in essence is like switching from your app to launcher app), launching another activity, pressing recents
changing orientation. this is the easier way if you are using an emulator
changing developer setting: goto developer options --> Apps --> Don't keep activities. This is best option if you are testing temporarily on an actual device.
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.
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.
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.
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/
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.
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.
There's a decent solution for this in Android 6 and newer. See my answer here: Simulate killing of activity in emulator

Categories

Resources