# ActivityScenarioRule

# 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)

*** ** * ** ***

Kotlin\|[Java](https://developer.android.com/reference/androidx/test/ext/junit/rules/ActivityScenarioRule "View this page in Java")

<br />

```
class ActivityScenarioRule<A : Activity?> : ExternalResource
```

<br />

|---|---|---------------------------------------------------------------------------------------------------------------------------------------------------------|
| [kotlin.Any](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html)                                                                            |||
| ↳ | [org.junit.rules.ExternalResource](https://junit.org/junit4/javadoc/4.12/org/junit/rules/ExternalResource.html)                                            ||
|   | ↳ | [androidx.test.ext.junit.rules.ActivityScenarioRule](https://developer.android.com/reference/kotlin/androidx/test/ext/junit/rules/ActivityScenarioRule) |

*** ** * ** ***

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

You can access the[androidx.test.core.app.ActivityScenario](https://developer.android.com/reference/kotlin/androidx/test/core/app/ActivityScenario)instance via[getScenario](https://developer.android.com/reference/kotlin/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[androidx.test.rule.ActivityTestRule](https://developer.android.com/reference/kotlin/androidx/test/rule/ActivityTestRule).

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

Example:  

```kotlin
  @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 constructors                                                                                                                                                                                          |
|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [ActivityScenarioRule](https://developer.android.com/reference/kotlin/androidx/test/ext/junit/rules/ActivityScenarioRule#ActivityScenarioRule(java.lang.Class<A>))`(activityClass: `[Class](https://developer.android.com/reference/java/lang/Class.html)`<A!>!)` Constructs ActivityScenarioRule for a given activity class.                                                                             |
| [ActivityScenarioRule](https://developer.android.com/reference/kotlin/androidx/test/ext/junit/rules/ActivityScenarioRule#ActivityScenarioRule(android.content.Intent))`(startActivityIntent: `[Intent](https://developer.android.com/reference/android/content/Intent.html)`!)` Constructs ActivityScenarioRule with a given intent.                                                                      |
| [ActivityScenarioRule](https://developer.android.com/reference/kotlin/androidx/test/ext/junit/rules/ActivityScenarioRule#ActivityScenarioRule(java.lang.Class<A>,android.os.Bundle))`(activityClass: `[Class](https://developer.android.com/reference/java/lang/Class.html)`<A!>!, activityOptions: `[Bundle](https://developer.android.com/reference/android/os/Bundle.html)`?)`                         |
| [ActivityScenarioRule](https://developer.android.com/reference/kotlin/androidx/test/ext/junit/rules/ActivityScenarioRule#ActivityScenarioRule(android.content.Intent,android.os.Bundle))`(` ` startActivityIntent: `[Intent](https://developer.android.com/reference/android/content/Intent.html)`!,` ` activityOptions: `[Bundle](https://developer.android.com/reference/android/os/Bundle.html)`?` `)` |

|                            ### Protected functions                            |
|-------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------|
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html) | [after](https://developer.android.com/reference/kotlin/androidx/test/ext/junit/rules/ActivityScenarioRule#after())`()`   |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html) | [before](https://developer.android.com/reference/kotlin/androidx/test/ext/junit/rules/ActivityScenarioRule#before())`()` |

|                                               ### Public properties                                               |
|-------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------|
| [ActivityScenario](https://developer.android.com/reference/kotlin/androidx/test/core/app/ActivityScenario)`<A!>?` | [scenario](https://developer.android.com/reference/kotlin/androidx/test/ext/junit/rules/ActivityScenarioRule#scenario()) |

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

## Public constructors

### ActivityScenarioRule

```
ActivityScenarioRule(activityClass: Class<A!>!)
```

Constructs ActivityScenarioRule for a given activity class.  

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

### ActivityScenarioRule

```
ActivityScenarioRule(startActivityIntent: Intent!)
```

Constructs ActivityScenarioRule with a given intent.  

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

### ActivityScenarioRule

```
ActivityScenarioRule(activityClass: Class<A!>!, activityOptions: Bundle?)
```  

|                                           Parameters                                           |
|------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------|
| `activityClass: `[Class](https://developer.android.com/reference/java/lang/Class.html)`<A!>!`  | an activity class to launch                                                      |
| `activityOptions: `[Bundle](https://developer.android.com/reference/android/os/Bundle.html)`?` | an activity options bundle to be passed along with the intent to start activity. |

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

### ActivityScenarioRule

```
ActivityScenarioRule(
    startActivityIntent: Intent!,
    activityOptions: Bundle?
)
```  

|                                               Parameters                                                |
|---------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------|
| `startActivityIntent: `[Intent](https://developer.android.com/reference/android/content/Intent.html)`!` | an intent to start an activity                                                   |
| `activityOptions: `[Bundle](https://developer.android.com/reference/android/os/Bundle.html)`?`          | an activity options bundle to be passed along with the intent to start activity. |

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

## Protected functions

### after

```
protected fun after(): Unit
```  

### before

```
protected fun before(): Unit
```  

|                                         Throws                                          |
|-----------------------------------------------------------------------------------------|---|
| [java.lang.Throwable](https://developer.android.com/reference/java/lang/Throwable.html) |   |

## Public properties

### scenario

```
val scenario: ActivityScenario<A!>?
```