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

*** ** * ** ***


```
public class ActivityTestRule<T extends Activity> implements TestRule
```

<br />

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

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

*** ** * ** ***

> [!CAUTION]
> **This class is deprecated.**   
>
> use `https://developer.android.com/reference/androidx/test/core/app/ActivityScenario` or `https://developer.android.com/reference/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/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/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/androidx/test/rule/ActivityTestRule#launchActivity(android.content.Intent)`, and manually finished with `https://developer.android.com/reference/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/androidx/test/rule/ActivityTestRule#getActivity()`. If the Activity is finished and relaunched, the reference returned by `https://developer.android.com/reference/androidx/test/rule/ActivityTestRule#getActivity()` will always point to the current instance of the Activity.

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

## Summary

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

| ### Public constructors |
|---|
| `[ActivityTestRule](https://developer.android.com/reference/androidx/test/rule/ActivityTestRule#ActivityTestRule(java.lang.Class<T>))(https://developer.android.com/reference/java/lang/Class.html<T> activityClass)` **This method is deprecated.** Deprecated in Java |
| `[ActivityTestRule](https://developer.android.com/reference/androidx/test/rule/ActivityTestRule#ActivityTestRule(java.lang.Class<T>,boolean))(https://developer.android.com/reference/java/lang/Class.html<T> activityClass, boolean initialTouchMode)` **This method is deprecated.** Deprecated in Java |
| `[ActivityTestRule](https://developer.android.com/reference/androidx/test/rule/ActivityTestRule#ActivityTestRule(java.lang.Class<T>,boolean,boolean))( https://developer.android.com/reference/java/lang/Class.html<T> activityClass, boolean initialTouchMode, boolean launchActivity )` **This method is deprecated.** Deprecated in Java |
| `[ActivityTestRule](https://developer.android.com/reference/androidx/test/rule/ActivityTestRule#ActivityTestRule(androidx.test.runner.intercepting.SingleActivityFactory<T>,boolean,boolean))( https://developer.android.com/reference/androidx/test/runner/intercepting/SingleActivityFactory<T> activityFactory, boolean initialTouchMode, boolean launchActivity )` **This method is deprecated.** Deprecated in Java |
| `[ActivityTestRule](https://developer.android.com/reference/androidx/test/rule/ActivityTestRule#ActivityTestRule(java.lang.Class<T>,java.lang.String,int,boolean,boolean))( https://developer.android.com/reference/java/lang/Class.html<T> activityClass, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html targetPackage, int launchFlags, boolean initialTouchMode, boolean launchActivity )` **This method is deprecated.** Deprecated in Java |

| ### Public methods |
|---|---|
| `https://junit.org/junit4/javadoc/latest/org/junit/runners/model/Statement.html` | `[apply](https://developer.android.com/reference/androidx/test/rule/ActivityTestRule#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)` **This method is deprecated.** Deprecated in Java |
| `void` | `[finishActivity](https://developer.android.com/reference/androidx/test/rule/ActivityTestRule#finishActivity())()` **This method is deprecated.** Deprecated in Java |
| `T` | `[getActivity](https://developer.android.com/reference/androidx/test/rule/ActivityTestRule#getActivity())()` **This method is deprecated.** Deprecated in Java |
| `https://developer.android.com/reference/android/app/Instrumentation.ActivityResult.html` | `[getActivityResult](https://developer.android.com/reference/androidx/test/rule/ActivityTestRule#getActivityResult())()` **This method is deprecated.** Deprecated in Java |
| `T` | `[launchActivity](https://developer.android.com/reference/androidx/test/rule/ActivityTestRule#launchActivity(android.content.Intent))(@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/android/content/Intent.html startIntent)` **This method is deprecated.** Deprecated in Java |
| `void` | `[runOnUiThread](https://developer.android.com/reference/androidx/test/rule/ActivityTestRule#runOnUiThread(java.lang.Runnable))(https://developer.android.com/reference/java/lang/Runnable.html runnable)` **This method is deprecated.** Deprecated in Java |

| ### Protected methods |
|---|---|
| `void` | `[afterActivityFinished](https://developer.android.com/reference/androidx/test/rule/ActivityTestRule#afterActivityFinished())()` **This method is deprecated.** Deprecated in Java |
| `void` | `[afterActivityLaunched](https://developer.android.com/reference/androidx/test/rule/ActivityTestRule#afterActivityLaunched())()` **This method is deprecated.** Deprecated in Java |
| `void` | `[beforeActivityLaunched](https://developer.android.com/reference/androidx/test/rule/ActivityTestRule#beforeActivityLaunched())()` **This method is deprecated.** Deprecated in Java |
| `https://developer.android.com/reference/android/content/Intent.html` | `[getActivityIntent](https://developer.android.com/reference/androidx/test/rule/ActivityTestRule#getActivityIntent())()` **This method is deprecated.** Deprecated in Java |

## Public fields

### activityResult

```
volatile public Instrumentation.ActivityResult activityResult
```

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

## Public constructors

### ActivityTestRule

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

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

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

| Parameters |
|---|---|
| `https://developer.android.com/reference/java/lang/Class.html<T> activityClass` | 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/androidx/test/rule/ActivityTestRule#ActivityTestRule(java.lang.Class<T>,boolean,boolean)` |   |

### ActivityTestRule

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

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

Similar to `https://developer.android.com/reference/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 |
|---|---|
| `https://developer.android.com/reference/java/lang/Class.html<T> activityClass` | The activity under test. This must be a class in the instrumentation targetPackage specified in the AndroidManifest.xml |
| `boolean initialTouchMode` | true if the Activity should be placed into "touch mode" when started |

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

### ActivityTestRule

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

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

Similar to `https://developer.android.com/reference/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 |
|---|---|
| `https://developer.android.com/reference/java/lang/Class.html<T> activityClass` | The activity under test. This must be a class in the instrumentation targetPackage specified in the AndroidManifest.xml |
| `boolean initialTouchMode` | true if the Activity should be placed into "touch mode" when started |
| `boolean launchActivity` | 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

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

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

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

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/test/runner/intercepting/SingleActivityFactory<T> activityFactory` | factory to be used for creating Activity instance |
| `boolean initialTouchMode` | true if the Activity should be placed into "touch mode" when started |
| `boolean launchActivity` | 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

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

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

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

| Parameters |
|---|---|
| `https://developer.android.com/reference/java/lang/Class.html<T> activityClass` | The activity under test. This must be a class in the instrumentation targetPackage specified in the AndroidManifest.xml |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html targetPackage` | 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. |
| `int launchFlags` | launch flags to start the Activity under test. |
| `boolean initialTouchMode` | true if the Activity should be placed into "touch mode" when started |
| `boolean launchActivity` | 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 methods

### apply

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

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

### finishActivity

```
public void [finishActivity](https://developer.android.com/reference/androidx/test/rule/ActivityTestRule#finishActivity())()
```

> [!CAUTION]
> **This method 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

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

> [!CAUTION]
> **This method 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.

### getActivityResult

```
public Instrumentation.ActivityResult [getActivityResult](https://developer.android.com/reference/androidx/test/rule/ActivityTestRule#getActivityResult())()
```

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

This method can be used to retrieve the `https://developer.android.com/reference/android/app/Instrumentation.ActivityResult.html` of an Activity that has called `https://developer.android.com/reference/android/app/Activity.html#setResult(int)`. Usually, the result is handled in `https://developer.android.com/reference/android/app/Activity.html#onActivityResult(int, int, android.content.Intent)` of the parent Activity, that has called `https://developer.android.com/reference/android/app/Activity.html#startActivityForResult(android.content.Intent, int)`.

This method must **not** be called before `Activity.finish` was called or after the activity was already destroyed.

Note: This method assumes `https://developer.android.com/reference/android/app/Activity.html#setResult(int)` is called no later than in `https://developer.android.com/reference/android/app/Activity.html#onPause()`.

| Returns |
|---|---|
| `https://developer.android.com/reference/android/app/Instrumentation.ActivityResult.html` | the `https://developer.android.com/reference/android/app/Instrumentation.ActivityResult.html` that was set most recently |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalStateException.html` | if the activity is not in finishing state. |

### launchActivity

```
public T [launchActivity](https://developer.android.com/reference/androidx/test/rule/ActivityTestRule#launchActivity(android.content.Intent))(@Nullable Intent startIntent)
```

> [!CAUTION]
> **This method 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/androidx/test/rule/ActivityTestRule#ActivityTestRule(java.lang.Class<T>,boolean,boolean)`.

Usage:

```
   @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/androidx/test/rule/ActivityTestRule#getActivityIntent()`. The same override rules documented in `https://developer.android.com/reference/androidx/test/rule/ActivityTestRule#getActivityIntent()` apply.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/android/content/Intent.html startIntent` | 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/androidx/test/rule/ActivityTestRule#getActivityIntent()` is used. |

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

### runOnUiThread

```
public void [runOnUiThread](https://developer.android.com/reference/androidx/test/rule/ActivityTestRule#runOnUiThread(java.lang.Runnable))(Runnable runnable)
```

> [!CAUTION]
> **This method 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/androidx/test/annotation/UiThreadTest`.

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

| Parameters |
|---|---|
| `https://developer.android.com/reference/java/lang/Runnable.html runnable` | 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/androidx/test/annotation/UiThreadTest` |   |

## Protected methods

### afterActivityFinished

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

> [!CAUTION]
> **This method 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 void [afterActivityLaunched](https://developer.android.com/reference/androidx/test/rule/ActivityTestRule#afterActivityLaunched())()
```

> [!CAUTION]
> **This method 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 void [beforeActivityLaunched](https://developer.android.com/reference/androidx/test/rule/ActivityTestRule#beforeActivityLaunched())()
```

> [!CAUTION]
> **This method 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 Intent [getActivityIntent](https://developer.android.com/reference/androidx/test/rule/ActivityTestRule#getActivityIntent())()
```

> [!CAUTION]
> **This method 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/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/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)`. |