How to simulate killing activity to conserve memor

2019-01-23 22:54发布

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.

8条回答
我欲成王,谁敢阻挡
2楼-- · 2019-01-23 23:17

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

查看更多
仙女界的扛把子
3楼-- · 2019-01-23 23:20

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.

查看更多
男人必须洒脱
4楼-- · 2019-01-23 23:24

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.

查看更多
兄弟一词,经得起流年.
5楼-- · 2019-01-23 23:25

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.
查看更多
何必那么认真
6楼-- · 2019-01-23 23:28

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.

查看更多
该账号已被封号
7楼-- · 2019-01-23 23:33

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.

查看更多
登录 后发表回答