# ActivityScenarioRule

Artifact: [androidx.test.ext:junit](https://developer.android.com/jetpack/androidx/releases/test-ext) [View Source](https://cs.android.com/search?q=file:androidx/test/ext/junit/rules/ActivityScenarioRule.java+class:androidx.test.ext.junit.rules.ActivityScenarioRule)

*** ** * ** ***


```
public final class ActivityScenarioRule<A extends Activity> extends ExternalResource
```

<br />

|---|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object.html) |||
| ↳ | [org.junit.rules.ExternalResource](https://junit.org/junit4/javadoc/latest/org/junit/rules/ExternalResource.html) ||
|   | ↳ | [androidx.test.ext.junit.rules.ActivityScenarioRule](https://developer.android.com/reference/androidx/test/ext/junit/rules/ActivityScenarioRule) |

*** ** * ** ***

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

You can access the `https://developer.android.com/reference/androidx/test/core/app/ActivityScenario` instance via `https://developer.android.com/reference/androidx/test/ext/junit/rules/ActivityScenarioRule#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 `https://developer.android.com/reference/androidx/test/rule/ActivityTestRule`.

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

Example:

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

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

## Summary

| ### Public fields |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/test/core/app/ActivityScenario<A>` | `https://developer.android.com/reference/androidx/test/ext/junit/rules/ActivityScenarioRule#scenario()` |

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/test/ext/junit/rules/ActivityScenarioRule#ActivityScenarioRule(java.lang.Class<A>)(https://developer.android.com/reference/java/lang/Class.html<A> activityClass)` Constructs ActivityScenarioRule for a given activity class. |
| `https://developer.android.com/reference/androidx/test/ext/junit/rules/ActivityScenarioRule#ActivityScenarioRule(android.content.Intent)(https://developer.android.com/reference/android/content/Intent.html startActivityIntent)` Constructs ActivityScenarioRule with a given intent. |
| `https://developer.android.com/reference/androidx/test/ext/junit/rules/ActivityScenarioRule#ActivityScenarioRule(java.lang.Class<A>,android.os.Bundle)( https://developer.android.com/reference/java/lang/Class.html<A> activityClass, @https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/android/os/Bundle.html activityOptions )` |
| `https://developer.android.com/reference/androidx/test/ext/junit/rules/ActivityScenarioRule#ActivityScenarioRule(android.content.Intent,android.os.Bundle)( https://developer.android.com/reference/android/content/Intent.html startActivityIntent, @https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/android/os/Bundle.html activityOptions )` |

| ### Public methods |
|---|---|
| `https://developer.android.com/reference/androidx/test/core/app/ActivityScenario<A>` | `https://developer.android.com/reference/androidx/test/ext/junit/rules/ActivityScenarioRule#getScenario()()` Returns `https://developer.android.com/reference/androidx/test/core/app/ActivityScenario` of the given activity class. |

| ### Protected methods |
|---|---|
| `void` | `https://developer.android.com/reference/androidx/test/ext/junit/rules/ActivityScenarioRule#after()()` |
| `void` | `https://developer.android.com/reference/androidx/test/ext/junit/rules/ActivityScenarioRule#before()()` |

| ### Inherited methods |
|---|
| From [org.junit.rules.ExternalResource](https://junit.org/junit4/javadoc/latest/org/junit/rules/ExternalResource.html) |---|---| | `https://junit.org/junit4/javadoc/latest/org/junit/runners/model/Statement.html` | `https://junit.org/junit4/javadoc/latest/org/junit/rules/ExternalResource.html#apply-org.junit.runners.model.Statement-org.junit.runner.Description-(https://junit.org/junit4/javadoc/latest/org/junit/runners/model/Statement.html base, https://junit.org/junit4/javadoc/latest/org/junit/runner/Description.html description)` | |

## Public fields

### scenario

```
public @Nullable ActivityScenario<A> scenario
```

## Public constructors

### ActivityScenarioRule

```
public ActivityScenarioRule(Class<A> activityClass)
```

Constructs ActivityScenarioRule for a given activity class.

| Parameters |
|---|---|
| `https://developer.android.com/reference/java/lang/Class.html<A> activityClass` | an activity class to launch |

### ActivityScenarioRule

```
public ActivityScenarioRule(Intent startActivityIntent)
```

Constructs ActivityScenarioRule with a given intent.

| Parameters |
|---|---|
| `https://developer.android.com/reference/android/content/Intent.html startActivityIntent` | an intent to start an activity |

### ActivityScenarioRule

```
public ActivityScenarioRule(
    Class<A> activityClass,
    @Nullable Bundle activityOptions
)
```

| Parameters |
|---|---|
| `https://developer.android.com/reference/java/lang/Class.html<A> activityClass` | an activity class to launch |
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/android/os/Bundle.html activityOptions` | an activity options bundle to be passed along with the intent to start activity. |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/test/ext/junit/rules/ActivityScenarioRule#ActivityScenarioRule(java.lang.Class<A>)` |   |

### ActivityScenarioRule

```
public ActivityScenarioRule(
    Intent startActivityIntent,
    @Nullable Bundle activityOptions
)
```

| Parameters |
|---|---|
| `https://developer.android.com/reference/android/content/Intent.html startActivityIntent` | an intent to start an activity |
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/android/os/Bundle.html activityOptions` | an activity options bundle to be passed along with the intent to start activity. |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/test/ext/junit/rules/ActivityScenarioRule#ActivityScenarioRule(android.content.Intent)` |   |

## Public methods

### getScenario

```
public ActivityScenario<A> getScenario()
```

Returns `https://developer.android.com/reference/androidx/test/core/app/ActivityScenario` of the given activity class.

| Returns |
|---|---|
| `https://developer.android.com/reference/androidx/test/core/app/ActivityScenario<A>` | a non-null `https://developer.android.com/reference/androidx/test/core/app/ActivityScenario` instance |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/NullPointerException.html` | if you call this method while test is not running |

## Protected methods

### after

```
protected void after()
```

### before

```
protected void before()
```

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/Throwable.html` |   |