我有一个单元测试,在我销毁并完成应用程序后,我再次调用 getActivity() 以验证它是否正确加载。但是,它没有调用 onCreate()!
public void testHistory() {
...
mActivity.onPause();
mActivity.finish();
assertTrue(mActivity.getExtractor() == null);
assertTrue(mActivity.getSettings() == null);
Log.d(TAG, "************** Restarting app to verify load.");
mActivity = getActivity();
assertTrue(mActivity != null);
assertTrue(mActivity.getExtractor() != null); //////// THIS FAILS!!!
这是日志
10-07 21:11:40.467: D/SpeedyReader(15441): onPause()
10-07 21:11:40.514: D/SpeedyReader(15441): Saving 5 articles, 1 historical articles...
10-07 21:11:40.709: D/SpeedyReader(15441): Saved articles. success: true, length: 88218
10-07 21:11:40.764: D/SpeedyReader(15441): onStop()
10-07 21:11:40.764: D/SpeedyReader(15441): onDestroy()
10-07 21:11:40.772: D/SpeedyReader(15441): finish()
10-07 21:11:40.772: D/SpeedyReader(15441): ************** Restarting app to verify load.
10-07 21:11:40.772: D/SpeedyReader(15441): finish()
10-07 21:11:40.897: D/SpeedyReader(15441): onCreate()
10-07 21:11:40.944: D/SpeedyReader(15441): finalize()
10-07 21:11:40.944: D/SpeedyReader(15441): Settings.load() wpm:300
10-07 21:11:40.944: D/SpeedyReader(15441): Loading articles...
我不知道为什么它在日志文件中说 onCreate 。就像 onCreate() 被异步调用一样!这是我的 onCreate()。
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.d(TAG, "onCreate()");
....
extract = new Extractor();
我试过 waitForIdleSync()
Log.d(TAG, "************** Restarting app to verify load.");
mActivity = getActivity(); // restart the app, reload the history
getInstrumentation().waitForIdleSync(); // ------------ NEW
但这没有帮助。