ActivityScenarioRule

public final class ActivityScenarioRule<A extends Activity>


ActivityScenarioRule launches a given activity before the test starts and closes after the test.

You can access the androidx.test.core.app.ActivityScenario instance via getScenario. You may finish your activity manually in your test, it will not cause any problems and this rule does nothing after the test in such cases.

This rule is an upgraded version of the now deprecated .

This rule does not support calling scenario.getResult(). Use ActivityScenario.launchActivityForResult() when the Activity result is needed.

Example:

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

  @Test
  public void myTest() {
    ActivityScenario

Summary

Public constructors

ActivityScenarioRule(Intent startActivityIntent)

Constructs ActivityScenarioRule with a given intent.

ActivityScenarioRule(Class<A> activityClass)

Constructs ActivityScenarioRule for a given activity class.

ActivityScenarioRule(
    Intent startActivityIntent,
    Bundle activityOptions
)
ActivityScenarioRule(Class<A> activityClass, Bundle activityOptions)

Public methods

ActivityScenario<A>

Returns ActivityScenario of the given activity class.

Public constructors

ActivityScenarioRule

public ActivityScenarioRule(Intent startActivityIntent)

Constructs ActivityScenarioRule with a given intent.

Parameters
Intent startActivityIntent

an intent to start an activity

ActivityScenarioRule

public ActivityScenarioRule(Class<A> activityClass)

Constructs ActivityScenarioRule for a given activity class.

Parameters
Class<A> activityClass

an activity class to launch

ActivityScenarioRule

public ActivityScenarioRule(
    Intent startActivityIntent,
    Bundle activityOptions
)
Parameters
Bundle activityOptions

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

ActivityScenarioRule

public ActivityScenarioRule(Class<A> activityClass, Bundle activityOptions)
Parameters
Bundle activityOptions

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

Public methods

getScenario

public ActivityScenario<A> getScenario()

Returns ActivityScenario of the given activity class.

Returns
ActivityScenario<A>

a non-null ActivityScenario instance

Throws
java.lang.NullPointerException java.lang.NullPointerException

if you call this method while test is not running