Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to emulate unloading of a background application by Android OS?

Tags:

android

For testing purposes I need to easily reproduce a situation when Android system decides to save a state of and kill a background application, in the same manner as it normally does for memory optimization purposes. In fact, I need also to test the restoration process of such a removed process when a user switches back to it.

The straighforward approach would be to open the application and then open more other tasks trying to allocate as much resources as possible. That's too complicated and unreliable.

I've found this question on SO, but the answer implies simply killing the process, which seems not an equivalent, because there seems no means for further automatic restoring of the killed process with a saved state, when a user decides to switch back to the application. If I understand correctly, after such explicit killing the application, if started, will run from very beginning, not from a saved state. Please, correct me, if I'm wrong.

According to Android documentation, what I need is performed by ActivityManager.killBackgroundProcesses(packageName), but this is the programmatic way of doing the thing. Is there an utility which already provides the same option from UI?

like image 608
Stan Avatar asked Dec 21 '12 10:12

Stan


1 Answers

If I understand correctly, after such explicit killing the application, if started, will run from very beginning, not from a saved state.

That depends on how the app is launched. If you mean from the launcher icon, yes. However, the user could return to you via the BACK button, or via the recent tasks list, depending upon circumstances, and those would return the user to the spot they left, not the "very beginning".

Is there an utility which already provides the same option from UI?

On an Android 4.0+ device, with your app in the background, open up the recent tasks list (long-press HOME or press the dedicated RECENTS affordance), and swipe your app off to the right. This appears to basically call killBackgroundProcesses() on that package name.

I have not tried this in an emulator, but probably it does the same thing.

like image 54
CommonsWare Avatar answered Nov 15 '22 04:11

CommonsWare