# ActivityTestRule

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

*** ** * ** ***


```
class ActivityTestRule<T : Activity?> : TestRule
```

<br />

Known direct subclasses [IntentsTestRule](https://developer.android.com/reference/kotlin/androidx/test/espresso/intent/rule/IntentsTestRule)

|---|---|
| `https://developer.android.com/reference/kotlin/androidx/test/espresso/intent/rule/IntentsTestRule` | **This class is deprecated.** Use `https://developer.android.com/reference/kotlin/androidx/test/espresso/intent/rule/IntentsRule`, and if necessary in conjunction with `https://developer.android.com/reference/kotlin/androidx/test/core/app/ActivityScenario` or `https://developer.android.com/reference/kotlin/androidx/test/ext/junit/rules/ActivityScenarioRule` instead. <br /> |

*** ** * ** ***

> [!CAUTION]
> **This class is deprecated.**   
>
> use `https://developer.android.com/reference/kotlin/androidx/test/core/app/ActivityScenario` or `https://developer.android.com/reference/kotlin/androidx/test/ext/junit/rules/ActivityScenarioRule` instead. They offer a simpler, and safer way of controlling Activity lifecycles.
>
> Here are some tips to consider when converting to `ActivityScenario/Rule`:
>
> 1. For simple cases where you want to launch the Activity before each test and tear it down after each test (eg you are using `https://developer.android.com/reference/kotlin/androidx/test/rule/ActivityTestRule#ActivityTestRule(java.lang.Class<T>)`), convert directly to ActivityScenarioRule.
> 2. If you need control over when to launch the Activity (eg you are using ActivityTestRule), use ActivityScenario.launch. It's recommended to wrap the launch in a try-block, so the Activity is closed automatically. `try (ActivityScenario.launch(activityClass)) { ... } `
> 3. If you need access to the Activity during the test (eg you are calling `https://developer.android.com/reference/kotlin/androidx/test/rule/ActivityTestRule#getActivity()`) provide a Runnable callback to onActivity instead. The callback provided to onActivity will run on the application's main thread, thus ensuring a safer mechanism to access the Activity.

This rule provides functional testing of a single `https://developer.android.com/reference/android/app/Activity.html`. When `launchActivity` is set to true in the constructor, the Activity under test will be launched before each test annotated with `https://junit.org/junit4/javadoc/latest/org/junit/Test.html` and before methods annotated with `https://junit.org/junit4/javadoc/latest/org/junit/Before.html`, and it will be terminated after the test is completed and methods annotated with `https://junit.org/junit4/javadoc/latest/org/junit/After.html` are finished.

The Activity can be manually launched with `https://developer.android.com/reference/kotlin/androidx/test/rule/ActivityTestRule#launchActivity(android.content.Intent)`, and manually finished with `https://developer.android.com/reference/kotlin/androidx/test/rule/ActivityTestRule#finishActivity()`. If the Activity is running at the end of the test, the test rule will finish it.

During the duration of the test you will be able to manipulate your Activity directly using the reference obtained from `https://developer.android.com/reference/kotlin/androidx/test/rule/ActivityTestRule#getActivity()`. If the Activity is finished and relaunched, the reference returned by `https://developer.android.com/reference/kotlin/androidx/test/rule/ActivityTestRule#getActivity()` will always point to the current instance of the Activity.

| Parameters |
|---|---|
| `<T : https://developer.android.com/reference/android/app/Activity.html?>` | The Activity class under test |

## Summary

| ### Public constructors |
|---|
| `[ActivityTestRule](https://developer.android.com/reference/kotlin/androidx/test/rule/ActivityTestRule#ActivityTestRule(java.lang.Class<T>))(activityClass: https://developer.android.com/reference/java/lang/Class.html<T!>!)` **This function is deprecated.** Deprecated in Java |
| `[ActivityTestRule](https://developer.android.com/reference/kotlin/androidx/test/rule/ActivityTestRule#ActivityTestRule(java.lang.Class<T>,boolean))(activityClass: https://developer.android.com/reference/java/lang/Class.html<T!>!, initialTouchMode: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)` **This function is deprecated.** Deprecated in Java |
| `[ActivityTestRule](https://developer.android.com/reference/kotlin/androidx/test/rule/ActivityTestRule#ActivityTestRule(java.lang.Class<T>,boolean,boolean))( activityClass: https://developer.android.com/reference/java/lang/Class.html<T!>!, initialTouchMode: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html, launchActivity: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html )` **This function is deprecated.** Deprecated in Java |
| `[ActivityTestRule](https://developer.android.com/reference/kotlin/androidx/test/rule/ActivityTestRule#ActivityTestRule(androidx.test.runner.intercepting.SingleActivityFactory<T>,boolean,boolean))( activityFactory: https://developer.android.com/reference/kotlin/androidx/test/runner/intercepting/SingleActivityFactory<T!>!, initialTouchMode: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html, launchActivity: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html )` **This function is deprecated.** Deprecated in Java |
| `[ActivityTestRule](https://developer.android.com/reference/kotlin/androidx/test/rule/ActivityTestRule#ActivityTestRule(java.lang.Class<T>,java.lang.String,int,boolean,boolean))( activityClass: https://developer.android.com/reference/java/lang/Class.html<T!>!, targetPackage: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html, launchFlags: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, initialTouchMode: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html, launchActivity: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html )` **This function is deprecated.** Deprecated in Java |

| ### Public functions |
|---|---|
| `https://junit.org/junit4/javadoc/latest/org/junit/runners/model/Statement.html!` | `[apply](https://developer.android.com/reference/kotlin/androidx/test/rule/ActivityTestRule#apply(org.junit.runners.model.Statement,org.junit.runner.Description))(base: https://junit.org/junit4/javadoc/latest/org/junit/runners/model/Statement.html!, description: https://junit.org/junit4/javadoc/latest/org/junit/runner/Description.html!)` **This function is deprecated.** Deprecated in Java |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `[finishActivity](https://developer.android.com/reference/kotlin/androidx/test/rule/ActivityTestRule#finishActivity())()` **This function is deprecated.** Deprecated in Java |
| `T!` | `[getActivity](https://developer.android.com/reference/kotlin/androidx/test/rule/ActivityTestRule#getActivity())()` **This function is deprecated.** Deprecated in Java |
| `T!` | `[launchActivity](https://developer.android.com/reference/kotlin/androidx/test/rule/ActivityTestRule#launchActivity(android.content.Intent))(startIntent: https://developer.android.com/reference/android/content/Intent.html?)` **This function is deprecated.** Deprecated in Java |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `[runOnUiThread](https://developer.android.com/reference/kotlin/androidx/test/rule/ActivityTestRule#runOnUiThread(java.lang.Runnable))(runnable: https://developer.android.com/reference/java/lang/Runnable.html!)` **This function is deprecated.** Deprecated in Java |

| ### Protected functions |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `[afterActivityFinished](https://developer.android.com/reference/kotlin/androidx/test/rule/ActivityTestRule#afterActivityFinished())()` **This function is deprecated.** Deprecated in Java |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `[afterActivityLaunched](https://developer.android.com/reference/kotlin/androidx/test/rule/ActivityTestRule#afterActivityLaunched())()` **This function is deprecated.** Deprecated in Java |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `[beforeActivityLaunched](https://developer.android.com/reference/kotlin/androidx/test/rule/ActivityTestRule#beforeActivityLaunched())()` **This function is deprecated.** Deprecated in Java |
| `https://developer.android.com/reference/android/content/Intent.html!` | `[getActivityIntent](https://developer.android.com/reference/kotlin/androidx/test/rule/ActivityTestRule#getActivityIntent())()` **This function is deprecated.** Deprecated in Java |

| ### Public properties |
|---|---|
| `volatile https://developer.android.com/reference/android/app/Instrumentation.ActivityResult.html!` | `https://developer.android.com/reference/kotlin/androidx/test/rule/ActivityTestRule#activityResult()` **This property is deprecated.** Deprecated in Java |

## Public constructors

### ActivityTestRule

```
[ActivityTestRule](https://developer.android.com/reference/kotlin/androidx/test/rule/ActivityTestRule#ActivityTestRule(java.lang.Class<T>))(activityClass: Class<T!>!)
```

> [!CAUTION]
> **This function is deprecated.**   
> Deprecated in Java

Similar to `https://developer.android.com/reference/kotlin/androidx/test/rule/ActivityTestRule#ActivityTestRule(java.lang.Class<T>,boolean)` but with "touch mode" disabled.

| Parameters |
|---|---|
| `activityClass: https://developer.android.com/reference/java/lang/Class.html<T!>!` | The activity under test. This must be a class in the instrumentation targetPackage specified in the AndroidManifest.xml |

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/test/rule/ActivityTestRule#ActivityTestRule(java.lang.Class<T>,boolean,boolean)` |   |

### ActivityTestRule

```
[ActivityTestRule](https://developer.android.com/reference/kotlin/androidx/test/rule/ActivityTestRule#ActivityTestRule(java.lang.Class<T>,boolean))(activityClass: Class<T!>!, initialTouchMode: Boolean)
```

> [!CAUTION]
> **This function is deprecated.**   
> Deprecated in Java

Similar to `https://developer.android.com/reference/kotlin/androidx/test/rule/ActivityTestRule#ActivityTestRule(java.lang.Class<T>,boolean,boolean)` but defaults to launch the activity under test once per `https://junit.org/junit4/javadoc/latest/org/junit/Test.html` method. It is launched before the first `https://junit.org/junit4/javadoc/latest/org/junit/Before.html` method, and terminated after the last `https://junit.org/junit4/javadoc/latest/org/junit/After.html` method.

| Parameters |
|---|---|
| `activityClass: https://developer.android.com/reference/java/lang/Class.html<T!>!` | The activity under test. This must be a class in the instrumentation targetPackage specified in the AndroidManifest.xml |
| `initialTouchMode: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | true if the Activity should be placed into "touch mode" when started |

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/test/rule/ActivityTestRule#ActivityTestRule(java.lang.Class<T>,boolean,boolean)` |   |

### ActivityTestRule

```
[ActivityTestRule](https://developer.android.com/reference/kotlin/androidx/test/rule/ActivityTestRule#ActivityTestRule(java.lang.Class<T>,boolean,boolean))(
    activityClass: Class<T!>!,
    initialTouchMode: Boolean,
    launchActivity: Boolean
)
```

> [!CAUTION]
> **This function is deprecated.**   
> Deprecated in Java

Similar to `https://developer.android.com/reference/kotlin/androidx/test/rule/ActivityTestRule#ActivityTestRule(java.lang.Class<T>,java.lang.String,int,boolean,boolean)` but defaults to launch the Activity with the default target package name getTargetContext and `https://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_NEW_TASK` launch flag.

| Parameters |
|---|---|
| `activityClass: https://developer.android.com/reference/java/lang/Class.html<T!>!` | The activity under test. This must be a class in the instrumentation targetPackage specified in the AndroidManifest.xml |
| `initialTouchMode: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | true if the Activity should be placed into "touch mode" when started |
| `launchActivity: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | true if the Activity should be launched once per `https://junit.org/junit4/javadoc/latest/org/junit/Test.html` method. It will be launched before the first `https://junit.org/junit4/javadoc/latest/org/junit/Before.html` method, and terminated after the last `https://junit.org/junit4/javadoc/latest/org/junit/After.html` method. |

### ActivityTestRule

```
[ActivityTestRule](https://developer.android.com/reference/kotlin/androidx/test/rule/ActivityTestRule#ActivityTestRule(androidx.test.runner.intercepting.SingleActivityFactory<T>,boolean,boolean))(
    activityFactory: SingleActivityFactory<T!>!,
    initialTouchMode: Boolean,
    launchActivity: Boolean
)
```

> [!CAUTION]
> **This function is deprecated.**   
> Deprecated in Java

Creates an `https://developer.android.com/reference/kotlin/androidx/test/rule/ActivityTestRule` for the Activity under test.

| Parameters |
|---|---|
| `activityFactory: https://developer.android.com/reference/kotlin/androidx/test/runner/intercepting/SingleActivityFactory<T!>!` | factory to be used for creating Activity instance |
| `initialTouchMode: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | true if the Activity should be placed into "touch mode" when started |
| `launchActivity: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | true if the Activity should be launched once per `https://junit.org/junit4/javadoc/latest/org/junit/Test.html` method. It will be launched before the first `https://junit.org/junit4/javadoc/latest/org/junit/Before.html` method, and terminated after the last `https://junit.org/junit4/javadoc/latest/org/junit/After.html` method. |

### ActivityTestRule

```
[ActivityTestRule](https://developer.android.com/reference/kotlin/androidx/test/rule/ActivityTestRule#ActivityTestRule(java.lang.Class<T>,java.lang.String,int,boolean,boolean))(
    activityClass: Class<T!>!,
    targetPackage: String,
    launchFlags: Int,
    initialTouchMode: Boolean,
    launchActivity: Boolean
)
```

> [!CAUTION]
> **This function is deprecated.**   
> Deprecated in Java

Creates an `https://developer.android.com/reference/kotlin/androidx/test/rule/ActivityTestRule` for the Activity under test.

| Parameters |
|---|---|
| `activityClass: https://developer.android.com/reference/java/lang/Class.html<T!>!` | The activity under test. This must be a class in the instrumentation targetPackage specified in the AndroidManifest.xml |
| `targetPackage: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html` | The name of the target package that the Activity is started under. This value is passed down to the start Intent using `https://developer.android.com/reference/android/content/Intent.html#setClassName(android.content.Context, java.lang.String)`. Can not be null. |
| `launchFlags: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | launch flags to start the Activity under test. |
| `initialTouchMode: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | true if the Activity should be placed into "touch mode" when started |
| `launchActivity: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | true if the Activity should be launched once per `https://junit.org/junit4/javadoc/latest/org/junit/Test.html` method. It will be launched before the first `https://junit.org/junit4/javadoc/latest/org/junit/Before.html` method, and terminated after the last `https://junit.org/junit4/javadoc/latest/org/junit/After.html` method. |

## Public functions

### apply

```
fun [apply](https://developer.android.com/reference/kotlin/androidx/test/rule/ActivityTestRule#apply(org.junit.runners.model.Statement,org.junit.runner.Description))(base: Statement!, description: Description!): Statement!
```

> [!CAUTION]
> **This function is deprecated.**   
> Deprecated in Java

### finishActivity

```
fun [finishActivity](https://developer.android.com/reference/kotlin/androidx/test/rule/ActivityTestRule#finishActivity())(): Unit
```

> [!CAUTION]
> **This function is deprecated.**   
> Deprecated in Java

Finishes the currently launched Activity.

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalStateException.html` | if the Activity is not running or failed to finish it. |

### getActivity

```
fun [getActivity](https://developer.android.com/reference/kotlin/androidx/test/rule/ActivityTestRule#getActivity())(): T!
```

> [!CAUTION]
> **This function is deprecated.**   
> Deprecated in Java

Returns the reference to the activity under test.

The reference to the activity is assigned during the initial creation of the acivity and for every sinlge OnResumed lifecycle change.

**Note:** Lifecycle changes happen on the UI thread (not the instrumentation thread where this test code usually executes). Thus, the return value may vary depending on timing.

For example, if the activity is finished and relaunched, the reference returned by this method will point to the new instance of the activity assuming OnResumed was called prior to calling this method.

If the activity wasn't created yet or already finished, `null` will be returned.

**Note:** The activity reference is stored in a weak reference which means if the activity under test is detroyed (ex. back button was pressed) then the system no longer holds a strong reference to the acitivty and this refernce may get garbage collected.

### launchActivity

```
fun [launchActivity](https://developer.android.com/reference/kotlin/androidx/test/rule/ActivityTestRule#launchActivity(android.content.Intent))(startIntent: Intent?): T!
```

> [!CAUTION]
> **This function is deprecated.**   
> Deprecated in Java

Launches the Activity under test.

Don't call this method directly, unless you explicitly requested not to lazily launch the Activity manually using the launchActivity flag in `https://developer.android.com/reference/kotlin/androidx/test/rule/ActivityTestRule#ActivityTestRule(java.lang.Class<T>,boolean,boolean)`.

Usage:

```kotlin
   @Test
   public void customIntentToStartActivity() {
       Intent intent = new Intent(Intent.ACTION_PICK);
       activity = mActivityRule.launchActivity(intent);
   }
```
Note: Custom start Intents provided through this method will take precedence over default Intents that where created in the constructor and any Intent returned from `https://developer.android.com/reference/kotlin/androidx/test/rule/ActivityTestRule#getActivityIntent()`. The same override rules documented in `https://developer.android.com/reference/kotlin/androidx/test/rule/ActivityTestRule#getActivityIntent()` apply.

| Parameters |
|---|---|
| `startIntent: https://developer.android.com/reference/android/content/Intent.html?` | The Intent that will be used to start the Activity under test. If ` startIntent` is null, the Intent returned by `https://developer.android.com/reference/kotlin/androidx/test/rule/ActivityTestRule#getActivityIntent()` is used. |

| Returns |
|---|---|
| `T!` | the Activity launched by this rule. |

### runOnUiThread

```
fun [runOnUiThread](https://developer.android.com/reference/kotlin/androidx/test/rule/ActivityTestRule#runOnUiThread(java.lang.Runnable))(runnable: Runnable!): Unit
```

> [!CAUTION]
> **This function is deprecated.**   
> Deprecated in Java

Helper method for running part of a method on the UI thread, blocking until it is complete.

Note: In most cases it is simpler to annotate the test method with `https://developer.android.com/reference/kotlin/androidx/test/annotation/UiThreadTest`.

Use this method if you need to switch in and out of the UI thread within your method.

| Parameters |
|---|---|
| `runnable: https://developer.android.com/reference/java/lang/Runnable.html!` | runnable containing test code in the `https://developer.android.com/reference/java/lang/Runnable.html#run()` method |

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

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/test/annotation/UiThreadTest` |   |

## Protected functions

### afterActivityFinished

```
protected fun [afterActivityFinished](https://developer.android.com/reference/kotlin/androidx/test/rule/ActivityTestRule#afterActivityFinished())(): Unit
```

> [!CAUTION]
> **This function is deprecated.**   
> Deprecated in Java

Override this method to execute any code that should run after the currently launched `https://developer.android.com/reference/android/app/Activity.html` is finished. This method is called after each test method, including any method annotated with `https://junit.org/junit4/javadoc/latest/org/junit/After.html`.

Prefer `https://junit.org/junit4/javadoc/latest/org/junit/After.html` over this method. This method should usually not be overwritten directly in tests and only be used by subclasses of ActivityTestRule to get notified when the activity is created and visible but test runs.

### afterActivityLaunched

```
protected fun [afterActivityLaunched](https://developer.android.com/reference/kotlin/androidx/test/rule/ActivityTestRule#afterActivityLaunched())(): Unit
```

> [!CAUTION]
> **This function is deprecated.**   
> Deprecated in Java

Override this method to execute any code that should run after your `https://developer.android.com/reference/android/app/Activity.html` is launched, but before any test code is run including any method annotated with `https://junit.org/junit4/javadoc/latest/org/junit/Before.html`.

Prefer `https://junit.org/junit4/javadoc/latest/org/junit/Before.html` over this method. This method should usually not be overwritten directly in tests and only be used by subclasses of ActivityTestRule to get notified when the activity is created and visible but test runs.

### beforeActivityLaunched

```
protected fun [beforeActivityLaunched](https://developer.android.com/reference/kotlin/androidx/test/rule/ActivityTestRule#beforeActivityLaunched())(): Unit
```

> [!CAUTION]
> **This function is deprecated.**   
> Deprecated in Java

Override this method to execute any code that should run before your `https://developer.android.com/reference/android/app/Activity.html` is created and launched. This method is called before each test method, including any method annotated with `https://junit.org/junit4/javadoc/latest/org/junit/Before.html`.

### getActivityIntent

```
protected fun [getActivityIntent](https://developer.android.com/reference/kotlin/androidx/test/rule/ActivityTestRule#getActivityIntent())(): Intent!
```

> [!CAUTION]
> **This function is deprecated.**   
> Deprecated in Java

Override this method to set up a custom Intent as if supplied to `https://developer.android.com/reference/android/content/Context.html#startActivity(android.content.Intent)`. Custom Intents provided by this method will take precedence over default Intents that where created in the constructor but be overridden by any Intents passed in through `https://developer.android.com/reference/kotlin/androidx/test/rule/ActivityTestRule#launchActivity(android.content.Intent)`.

The default Intent (if this method returns null or is not overwritten) is: action = `https://developer.android.com/reference/android/content/Intent.html#ACTION_MAIN` flags = `https://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_NEW_TASK` All other intent fields are null or empty.

If the custom Intent provided by this methods overrides any of the following fields:

- componentName through `https://developer.android.com/reference/android/content/Intent.html#setClassName(java.lang.String, java.lang.String)`
- launch flags through `https://developer.android.com/reference/android/content/Intent.html#setFlags(int)`

These custom values will be used to start the Activity. However, if some of these values are not set, the default values documented in `https://developer.android.com/reference/kotlin/androidx/test/rule/ActivityTestRule#ActivityTestRule(java.lang.Class<T>,java.lang.String,int,boolean,boolean)` are supplemented.

| Returns |
|---|---|
| `https://developer.android.com/reference/android/content/Intent.html!` | The Intent as if supplied to `https://developer.android.com/reference/android/content/Context.html#startActivity(android.content.Intent)`. |

## Public properties

### activityResult

```
volatile val activityResult: Instrumentation.ActivityResult!
```

> [!CAUTION]
> **This property is deprecated.**   
> Deprecated in Java