Android 中模拟 Activity 死亡
我们知道,当系统耗尽资源时,后台的活动会序列化其状态并被操作系统终止。当我们恢复它时,操作系统通过传递给onCreate方法的savedInstanceState恢复活动状态。考虑到我们负责处理将要序列化/恢复的内容,我希望终止我的活动,以便测试我为恢复创建的代码。我怎样才能做到这一点?通过应用程序菜单强制终止应用程序没有帮助。
We know that when the system runs out of resources, an activity in background serializes its state and gets killed by the OS. When we resume it, the OS recovers the activity state by savedInstanceState passed to onCreate method. Considering we are responsible for handling what is going to be serialized/recovered, I'd like to have my activity killed in order to test the code I created for recovering. How can I achieve that? Forcing the application to be killed through the applications menu doesn't help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
旋转您的设备(或模拟器)。 Android 在新方向上保存、销毁并重新创建 Activity。
Rotate your device (or emulator). Android saves, destroys, and re-creates the activity in the new orientation.
下载一个任务管理器,它以比“管理应用程序”设置中的“强制停止”破坏性更小的方式终止进程。示例:GO 任务管理器。
任务管理器将杀死应用程序(和调试),但不知何故不会杀死活动堆栈(不知道为什么)。
当您再次重新启动应用程序时,将使用上次保存的包/状态调用 onCreate。
与 Darrell 的解决方案相比,此解决方案的缺点是您无法对其进行调试。
与 Darrell 的解决方案相比,该解决方案的优点是它更接近现实生活场景。
Download a task manager that kills the process in a less destructive way than "Force stop" in "Manage applications" settings. Example: GO task manager.
The task manager will kill the app (and the debug) but somehow not the activity stack (don't know why).
When you'll relaunch the app again, onCreate will be invoked with the last saved bundle/state.
The disadvantage of this solution, compared to Darrell's, is that you cannot debug it.
The advantage of this solution, compared to Darrell's, is that it is more close to real life scenario.
您也可以从 Eclipse 中杀死它。
转到 Android 视图。您应该在“设备”选项卡中看到进程列表。
单击您的进程,然后单击小“停止”按钮。
瞬间死亡!
仅供参考,您也可以通过单击绿色小虫以这种方式附加调试器
You can kill it from Eclipse also.
Go to the Android view. YOu should see the list of processes in the Devices tab.
Click on your process and then click the little "STOP" button.
Instant death!
FYI you can also attach the debugger this way by clicking on the little green bug