How to stop and restart an activity in an android instrumentation test?

17,088

Solution 1

By calling (or trigger a screen orientation change):

activity.finish(); // old activity instance is destroyed and shut down.
activity = getActivity(); // new activity instance is launched and created.

Causing the activity go through the complete recreation life cycle:

onPause() -> onStop() -> onDestroy() -> onCreate()

What you need is:

onPause() -> onStop() -> onRestart()

I exposed the Instrumentation API recently and found plenty of interesting activity life cycle trigger method callActivityOnXXX(), the following single line of code should do the tricky:

MyActivity myActivity = getActivity();
// make activity falling into restart phase:
getInstrumentation().callActivityOnRestart(myActivity);

Activity life cycle diagram quoting from official dev guide: enter image description here

Solution 2

I tried calling .finish(), setActivity(null), getActivity() and it does restart the activity, but for me it was not restoring the state. I tried out all the other answers on SO, and every other method to do this I could find online, and none of them worked for me. After much experimentation I found the following works (nb: requires API level 11+):

    getInstrumentation().runOnMainSync(new Runnable() {
        @Override
        public void run() {
            activity.recreate();
        }
    });
    setActivity(null);
    activity = getActivity();

When I do this a new Activity instance is created, and a new instance of the fragment I had attached to the activity earlier in the test is also created, and both the activity and fragment restore their state in the expected manner.

I don't know how this works or why this works, I reached this solution through trial and error, and I have only tested it on a Nexus 4 running KitKat. I can't guarantee it correctly simulates an activity recreation, but it worked for my purposes.

Edit: At a later date I figured out how this works. getActivity() works through registering hooks that receive new Activities being created, which catch the new Activity created by activity.recreate(). setActivity(null) was required to clear the internal cache backing getActivity, otherwise it will return the old one and not look for a new one.

You can see how this works from examining the source code for the various test case classes one extends from.

Solution 3

A good way to test lifecycle events is through screen orientation changes. In my experience it's a convenient way to bombproof the onPause / onStart pattern.

Share:
17,088

Related videos on Youtube

Danilo Bargen
Author by

Danilo Bargen

I'm a software engineer from Switzerland. I especially enjoy working with Python, Django and Rust. I usually post my OSS Code at https://github.com/dbrgn.

Updated on June 17, 2022

Comments

  • Danilo Bargen
    Danilo Bargen about 2 years

    I'm trying to write an Android activity instrumentation test that stops (onPause(), then onStop()) and restarts the current activity. I tried

    activity.finish();
    activity = getActivity();
    

    ...but that doesn't seem to work properly.

    The goal of the test is to assert that form data is stored during the onPause() method and re-read during the onStart() method. It works when doing it manually, but the test fails, from which I draw the conclusion that activity.finish() seems to be the wrong way to stop and restart an activity.


    Edit: My main problem seems to have been a synchronization issue. After restarting the activity, the test runner didn't wait for all event handlers to finish. The following line halts the test execution until the activity is idle:

    getInstrumentation().waitForIdleSync()
    

    Besides that, take a look at the accepted answer for more valuable information about the lifecycle.

    • yorkw
      yorkw about 12 years
      What exactly doesn't seem to work properly?
    • Danilo Bargen
      Danilo Bargen about 12 years
      @yorkw I updated the question, thanks for the comment.
    • Joel Skrepnek
      Joel Skrepnek about 12 years
      What do you mean when you say, "doing it manually?"
    • Danilo Bargen
      Danilo Bargen about 12 years
      @JoelSkrepnek There is a checkbox that enables and disables the feature. When I uncheck the checkbox and close and reopen the app, all data is gone. When I check it and close or kill and then reopen the app, all form data is restored.
  • Danilo Bargen
    Danilo Bargen about 12 years
    Interesting idea. The problem with this is that Android saves form data by default when doing a screen orientation change, so that doesn't really cover this test case...
  • yorkw
    yorkw about 12 years
    A screen orientation change resulting complete Activity life cycle: ... -> onPause() -> onStop() -> onDestroy() -> onCreate() -> onStart() -> ..., which has the same behavior of calling activity.finish(); activity = getActivity();
  • Joel Skrepnek
    Joel Skrepnek about 12 years
    @DaniloBargen I don't believe form data is saved by default when an activity is destroyed.
  • Danilo Bargen
    Danilo Bargen about 12 years
    @JoelSkrepnek I tested it on my ICS device. killing or closing the app results in an empty form field. When changing the screen orientation, all form data is retained.
  • Danilo Bargen
    Danilo Bargen about 12 years
    Actually, I already did give the first solution a try, and it did not seem to work as you can see in my question. But maybe you're right and the problem is unrelated... Re your second suggestion, I'll try that one later on...
  • Danilo Bargen
    Danilo Bargen about 12 years
    that actually did it, thanks very much! :) would you mind putting this in the top of your answer for others to find more easily?
  • Danilo Bargen
    Danilo Bargen about 12 years
    Thanks for the edit. One last remark: I also ran into synchronization problems. The test didn't wait for my lifecycle eventhandler to finish. Adding getInstrumentation().waitForIdleSync() after restarting the activity helped.
  • Eugene
    Eugene over 11 years
    Does getInstrumentation().callActivityOnRestart(myActivity) really results into onPause() -> onStop() -> onRestart()? I tried and it is not.
  • yorkw
    yorkw over 11 years
    @siik, it is hard to tell without more details and investigation. My first shot is make sure callActivityOnRestart() is called off the application's main thread (or UI thread), i.e. in case if using @UiThreadTest annotation wrap the whole test method body.