Simulate killing of activity in emulator

前端 未结 7 2044
误落风尘
误落风尘 2020-12-01 06:26

I would like to test out onSaveInstanceState and onRestoreInstanceState for my app on the emulator.

I have found this, which says that we c

7条回答
  •  北荒
    北荒 (楼主)
    2020-12-01 07:10

    To test the onSaveInstanceState and onRestoreInstanceState events you can use the SetAlwaysFinish utility (see link below). It uses a hidden system setting called Always Finish to change the behavior of the Android system. With the Always Finish option enabled, the Android OS will immediately unload any activity that moves from the foreground to the background, triggering its onSaveInstanceState event. Then, when the system needs the activity again, it is forced to reload it triggering the onRestoreInstanceState event.

    The advantage of this method compared to killing the process is that it's easier and you get finer control (activity level rather than process level).

    See the blog post below for more details on the tool and how to use it: http://bricolsoftconsulting.com/how-to-test-onsaveinstancestate-and-onrestoreinstancestate-on-a-real-device/

    The Always Finish feature is also present in the emulator in the DevTools app under Development Settings > Immediately destroy activities. However, the DevTools app does not work outside of the emulator.

提交回复
热议问题