{% setvar book_path %}/reference/androidx/_book.yaml{% endsetvar %} {% include "_shared/_reference-head-tags.html" %}

ActivityScenario

public final class ActivityScenario<A extends Activity>


ActivityScenario provides APIs to start and drive an Activity's lifecycle state for testing. It works with arbitrary activities and works consistently across different versions of the Android framework.

The ActivityScenario API uses State extensively. If you are unfamiliar with components, please read lifecycle before starting. It is crucial to understand the difference between State and Event.

ActivityScenario#moveToState(State) allows you to transition your Activity's state to State#CREATED, State#STARTED, State#RESUMED, or State#DESTROYED. There are two paths for an Activity to reach State#CREATED: after Event#ON_CREATE happens but before Event#ON_START, or after Event#ON_STOP. ActivityScenario always moves the Activity's state using the second path. The same applies to STARTED.

State#DESTROYED is the terminal state. You cannot move your Activity to other state once it reaches to that state. If you want to test recreation of Activity instance, use .

ActivityScenario does't clean up device state automatically and may leave the activity keep running after the test finishes. Call close in your test to clean up the state or use try-with-resources statement. This is optional but highly recommended to improve the stability of your tests. Also, consider using androidx.test.ext.junit.rules.ActivityScenarioRule.

This class is a replacement of ActivityController in Robolectric and ActivityTestRule in ATSL.

Following are the example of common use cases.

Before:
  MyActivity activity = Robolectric.setupActivity(MyActivity.class);
  assertThat(activity.getSomething()).isEqualTo("something");

After:
  try(ActivityScenario<MyActivity> scenario = ActivityScenario.launch(MyActivity.class)) {
    scenario.onActivity(activity -> {
      assertThat(activity.getSomething()).isEqualTo("something");
    });
  }

Before:
  ActivityController<MyActivity> controller = Robolectric.buildActivity(MyActivity.class);
  controller.create().start().resume();  // Moves the activity state to State.RESUMED.
  controller.pause();    // Moves the activity state to State.STARTED. (ON_PAUSE is an event).
  controller.stop();     // Moves the activity state to State.CREATED. (ON_STOP is an event).
  controller.destroy();  // Moves the activity state to State.DESTROYED.

After:
  try(ActivityScenario<MyActivity> scenario = ActivityScenario.launch(MyActivity.class)) {
    scenario.moveToState(State.RESUMED);    // Moves the activity state to State.RESUMED.
    scenario.moveToState(State.STARTED);    // Moves the activity state to State.STARTED.
    scenario.moveToState(State.CREATED);    // Moves the activity state to State.CREATED.
    scenario.moveToState(State.DESTROYED);  // Moves the activity state to State.DESTROYED.
  }

Summary

Nested types

ActivityScenario.ActivityAction

ActivityAction interface should be implemented by any class whose instances are intended to be executed by the main thread.

Public methods

@NonNull void

Finishes the managed activity and cleans up device's state.

@NonNull ActivityResult

Waits for the activity to be finished and returns the activity result.

@NonNull State

Returns the current activity state.

static @NonNull ActivityScenario<@NonNull A>
<A extends Activity> launch(@NonNull Class<@NonNull A> activityClass)

Launches an activity of a given class and constructs ActivityScenario with the activity.

static @NonNull ActivityScenario<@NonNull A>
<A extends Activity> launch(
    @NonNull Class<@NonNull A> activityClass,
    @NonNull Bundle activityOptions
)
static @NonNull ActivityScenario<@NonNull A>
<A extends Activity> launch(@NonNull Intent startActivityIntent)

Launches an activity by a given intent and constructs ActivityScenario with the activity.

static @NonNull ActivityScenario<@NonNull A>
<A extends Activity> launch(
    @NonNull Intent startActivityIntent,
    @NonNull Bundle activityOptions
)
static @NonNull ActivityScenario<@NonNull A>
<A extends Activity> launchActivityForResult(
    @NonNull Class<@NonNull A> activityClass
)

Launches an activity of a given class and constructs ActivityScenario with the activity.

static @NonNull ActivityScenario<@NonNull A>
<A extends Activity> launchActivityForResult(
    @NonNull Class<@NonNull A> activityClass,
    @NonNull Bundle activityOptions
)

Launches an activity of a given class and activity options and constructs ActivityScenario with the activity.

static @NonNull ActivityScenario<@NonNull A>
<A extends Activity> launchActivityForResult(@NonNull Intent startActivityIntent)

Launches an activity by a given intent and constructs ActivityScenario with the activity.

static @NonNull ActivityScenario<@NonNull A>
<A extends Activity> launchActivityForResult(
    @NonNull Intent startActivityIntent,
    @NonNull Bundle activityOptions
)

Launches an activity by a given intent and constructs ActivityScenario with the activity.

@NonNull ActivityScenario<@NonNull A>
moveToState(@NonNull State newState)

Moves Activity state to a new state.

@NonNull ActivityScenario<@NonNull A>

Runs a given action on the current Activity's main thread.

@NonNull ActivityScenario<@NonNull A>

Recreates the Activity.

Public methods

close

@NonNull
public void close()

Finishes the managed activity and cleans up device's state. This method blocks execution until the activity becomes State#DESTROYED.

It is highly recommended to call this method after you test is done to keep the device state clean although this is optional.

You may call this method more than once. If the activity has been finished already, this method does nothing.

Avoid calling this method directly. Consider one of the following options instead:

        
 Option 1, use try-with-resources:

 try (ActivityScenario<MyActivity> scenario = ActivityScenario.launch(MyActivity.class)) {
   // Your test code goes here.
 }

 Option 2, use ActivityScenarioRule:

@Rule
 public ActivityScenarioRule<MyActivity> rule = new ActivityScenarioRule<>(MyActivity.class);

@Test
 public void myTest() {
   ActivityScenario<MyActivity> scenario = rule.getScenario();
   // Your test code goes here.
 }

getResult

@NonNull
public ActivityResult getResult()

Waits for the activity to be finished and returns the activity result.

Note: This method doesn't call Activity#finish(). The activity must be finishing or finished otherwise this method will throws runtime exception after the timeout.

Example:
  ActivityScenario<MyActivity> scenario = ActivityScenario.launch(MyActivity.class);
  // Let's say MyActivity has a button that finishes itself.
  onView(withId(R.id.finish_button)).perform(click());
  assertThat(scenario.getResult().getResultCode()).isEqualTo(Activity.RESULT_OK);
Returns
ActivityResult

activity result of the activity that managed by this scenario class.

getState

@NonNull
public State getState()

Returns the current activity state. The possible states are State#CREATED, STARTED, State#RESUMED, and State#DESTROYED.

This method cannot be called from the main thread except in Robolectric tests.

launch

@NonNull
public static ActivityScenario<@NonNull A> <A extends Activity> launch(@NonNull Class<@NonNull A> activityClass)

Launches an activity of a given class and constructs ActivityScenario with the activity. Waits for the lifecycle state transitions to be complete. Typically the initial state of the activity is State#RESUMED but can be in another state. For instance, if your activity calls Activity#finish from your Activity#onCreate, the state is DESTROYED when this method returns.

If you need to supply parameters to the start activity intent, use launch.

If you need to get the activity result, use launchActivityForResult.

This method cannot be called from the main thread except in Robolectric tests.

Parameters
@NonNull Class<@NonNull A> activityClass

an activity class to launch

Returns
ActivityScenario<@NonNull A>

ActivityScenario which you can use to make further state transitions

Throws
java.lang.AssertionError

if the lifecycle state transition never completes within the timeout

launch

@NonNull
public static ActivityScenario<@NonNull A> <A extends Activity> launch(
    @NonNull Class<@NonNull A> activityClass,
    @NonNull Bundle activityOptions
)
Parameters
@NonNull Bundle activityOptions

an activity options bundle to be passed along with the intent to start activity.

See also
launch

#launch(Class)

launch

@NonNull
public static ActivityScenario<@NonNull A> <A extends Activity> launch(@NonNull Intent startActivityIntent)

Launches an activity by a given intent and constructs ActivityScenario with the activity. Waits for the lifecycle state transitions to be complete. Typically the initial state of the activity is State#RESUMED but can be in another state. For instance, if your activity calls Activity#finish from your Activity#onCreate, the state is DESTROYED when this method returns.

If you need to get the activity result, use launchActivityForResult.

This method cannot be called from the main thread except in Robolectric tests.

Parameters
@NonNull Intent startActivityIntent

an intent to start the activity

Returns
ActivityScenario<@NonNull A>

ActivityScenario which you can use to make further state transitions

Throws
java.lang.AssertionError

if the lifecycle state transition never completes within the timeout

launch

@NonNull
public static ActivityScenario<@NonNull A> <A extends Activity> launch(
    @NonNull Intent startActivityIntent,
    @NonNull Bundle activityOptions
)
Parameters
@NonNull Bundle activityOptions

an activity options bundle to be passed along with the intent to start activity.

See also
launch

#launch(Intent)

launchActivityForResult

@NonNull
public static ActivityScenario<@NonNull A> <A extends Activity> launchActivityForResult(
    @NonNull Class<@NonNull A> activityClass
)

Launches an activity of a given class and constructs ActivityScenario with the activity. Waits for the lifecycle state transitions to be complete. Typically the initial state of the activity is State#RESUMED but can be in another state. For instance, if your activity calls Activity#finish from your Activity#onCreate, the state is DESTROYED when this method returns. Broadcasts activity result.

If you need to supply parameters to the start activity intent, use .

This method cannot be called from the main thread except in Robolectric tests.

Parameters
@NonNull Class<@NonNull A> activityClass

an activity class to launch

Returns
ActivityScenario<@NonNull A>

ActivityScenario which you can use to make further state transitions

Throws
java.lang.AssertionError

if the lifecycle state transition never completes within the timeout

launchActivityForResult

@NonNull
public static ActivityScenario<@NonNull A> <A extends Activity> launchActivityForResult(
    @NonNull Class<@NonNull A> activityClass,
    @NonNull Bundle activityOptions
)

Launches an activity of a given class and activity options and constructs ActivityScenario with the activity. @see #launchActivityForResult(Class)

Parameters
@NonNull Bundle activityOptions

an activity options bundle to be passed along with the intent to start activity.

launchActivityForResult

@NonNull
public static ActivityScenario<@NonNull A> <A extends Activity> launchActivityForResult(@NonNull Intent startActivityIntent)

Launches an activity by a given intent and constructs ActivityScenario with the activity. Waits for the lifecycle state transitions to be complete. Typically the initial state of the activity is State#RESUMED but can be in another state. For instance, if your activity calls Activity#finish from your Activity#onCreate, the state is DESTROYED when this method returns. Broadcasts activity result.

This method cannot be called from the main thread except in Robolectric tests.

Parameters
@NonNull Intent startActivityIntent

an intent to start the activity

Returns
ActivityScenario<@NonNull A>

ActivityScenario which you can use to make further state transitions

Throws
java.lang.AssertionError

if the lifecycle state transition never completes within the timeout

launchActivityForResult

@NonNull
public static ActivityScenario<@NonNull A> <A extends Activity> launchActivityForResult(
    @NonNull Intent startActivityIntent,
    @NonNull Bundle activityOptions
)

Launches an activity by a given intent and constructs ActivityScenario with the activity. @see #launchActivityForResult(Intent)

Parameters
@NonNull Bundle activityOptions

an activity options bundle to be passed along with the intent to start activity.

moveToState

@NonNull
public ActivityScenario<@NonNull A> moveToState(@NonNull State newState)

Moves Activity state to a new state.

If a new state and current state are the same, it does nothing. It accepts CREATED, State#STARTED, State#RESUMED, and State#DESTROYED.

State#DESTROYED is the terminal state. You cannot move the state to other state after the activity reaches that state.

The activity must be at the top of the back stack (excluding internal facilitator activities started by this library), otherwise AssertionError may be thrown. If the activity starts another activity (such as DialogActivity), make sure you close these activities and bring back the original activity foreground before you call this method.

This method cannot be called from the main thread except in Robolectric tests.

Throws
java.lang.IllegalArgumentException

if unsupported newState is given

java.lang.IllegalStateException

if Activity is destroyed, finished or finishing

java.lang.AssertionError

if Activity never becomes requested state

onActivity

@NonNull
public ActivityScenario<@NonNull A> onActivity(@NonNull ActivityScenario.ActivityAction<@NonNull A> action)

Runs a given action on the current Activity's main thread.

Note that you should never keep Activity reference passed into your action because it can be recreated at anytime during state transitions.

Throws
java.lang.IllegalStateException

if Activity is destroyed, finished or finishing

recreate

@NonNull
public ActivityScenario<@NonNull A> recreate()

Recreates the Activity.

A current Activity will be destroyed after its data is saved into android.os.Bundle with Activity#onSaveInstanceState(Bundle), then it creates a new Activity with the saved Bundle. After this method call, it is ensured that the Activity state goes back to the same state as its previous state.

This method cannot be called from the main thread except in Robolectric tests.

Throws
java.lang.IllegalStateException

if Activity is destroyed, finished or finishing

java.lang.AssertionError

if Activity never be re-created