In test case, 2nd getActivity() is not calling onCreate()


I have a unit test, and after I destroy and finish the app, I call getActivity() again to verify it loads correctly. However, it is not calling onCreate()!

public void testHistory() { 
    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!!!

Here is the log

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...

I don’t know why it says onCreate in the log file. It’s like onCreate() is being called asynchronously! Here is my onCreate().

public void onCreate(Bundle savedInstanceState)
    Log.d(TAG, "onCreate()");
    extract  new Extractor();

I tried waitForIdleSync()

    Log.d(TAG, "**************  Restarting app to verify load.");
    mActivity  getActivity(); // restart the app, reload the history
    getInstrumentation().waitForIdleSync(); // ------------ NEW

But that didn’t help.


Found it! Use

    getInstrumentation().callActivityOnCreate(mActivity, null);

Answered By – Chloe

Leave a Comment