我需要测试一个 ActivityonDestroy
方法,然后是onCreate
and onRestoreInstanceHandle
。我知道一种方法——改变屏幕方向。但是还有另一种情况是activity被销毁了——其他应用程序需要资源,而在某个时刻Android决定销毁后台activity。但是,它仍然可以恢复,Bundle
可用。有没有办法模拟这种情况?
问问题
3248 次
3 回答
7
我不确定在哪个版本的 Android 上可用,但至少在 Jelly Bean 中,有一个开发人员选项可以让这变得非常简单。在Settings → Developer Options下,向下滚动到 Apps 部分并启用Don't keep activities选项。
然后,每当您退出应用程序(使用返回按钮或主页按钮)时,操作系统都会破坏该应用程序的活动,而不是仅仅暂停它并将其置于后台。不过,不要忘记在完成测试后取消设置该设置。
于 2013-08-01T15:04:11.480 回答
2
public class MyActivityTests extends ActivityInstrumentationTestCase2<MyActivity> {
public void testLifecycle() {
Activity activity = this.getActivity();
//do stuff to the activity
this.getInstrumentation().callActivityOnStop(activity);
activity = this.getActivity(); // this should call onCreate() and onRestoreInstanceHandle()
// write assertions
}
}
于 2013-08-18T20:48:08.193 回答
2
您可以使用此示例并写入日志 OnDestroy 称为
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.finish();
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.i("test", "OnDestroy is called");
}
于 2012-10-11T14:16:17.760 回答