# ActivityTestRule

Summary:[Ctors](https://developer.android.com/reference/android/support/test/rule/ActivityTestRule#pubctors)\|[Methods](https://developer.android.com/reference/android/support/test/rule/ActivityTestRule#pubmethods)\|[Protected Methods](https://developer.android.com/reference/android/support/test/rule/ActivityTestRule#promethods)\|[Inherited Methods](https://developer.android.com/reference/android/support/test/rule/ActivityTestRule#inhmethods)\|[\[Expand All\]](https://developer.android.com/reference/android/support/test/rule/ActivityTestRule#)  

# ActivityTestRule

| This package is part of the Android[support library](https://developer.android.com/topic/libraries/support-library)which is no longer maintained. The support library has been superseded by[AndroidX](https://developer.android.com/jetpack/androidx)which is part of[Jetpack](https://developer.android.com/jetpack). We recommend using the AndroidX libraries in all new projects. You should also consider[migrating](https://developer.android.com/jetpack/androidx/migrate)existing projects to AndroidX. To find the AndroidX class that maps to this deprecated class, see the AndroidX support library[class mappings](https://developer.android.com/jetpack/androidx/migrate/class-mappings).

`
public


class
ActivityTestRule
`  
`

extends `[Object](https://developer.android.com/reference/java/lang/Object.html)`


``


implements

TestRule


`

|---|--------------------------------------------------------------------------------------------------------------------------------------------------|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object.html)                                                                   ||
| ↳ | android.support.test.rule.ActivityTestRule\<T extends[android.app.Activity](https://developer.android.com/reference/android/app/Activity.html)\> |

|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [![](https://developer.android.com/static/assets/images/styles/disclosure_down.png)](https://developer.android.com/reference/android/support/test/rule/ActivityTestRule#)Known Direct Subclasses [IntentsTestRule](https://developer.android.com/reference/android/support/test/espresso/intent/rule/IntentsTestRule)\<T extends[Activity](https://developer.android.com/reference/android/app/Activity.html)\> |----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------| | [IntentsTestRule](https://developer.android.com/reference/android/support/test/espresso/intent/rule/IntentsTestRule)\<T extends[Activity](https://developer.android.com/reference/android/app/Activity.html)\> | This rule makes it easy to use Espresso-Intents APIs in functional UI tests. | |

<br />

*** ** * ** ***

This rule provides functional testing of a single[Activity](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[`Test`](http://junit.org/javadoc/latest/org/junit/Test.html)and before methods annotated with[`Before`](http://junit.source.net/javadoc/org/junit/Before.html), and it will be terminated after the test is completed and methods annotated with[`After
`](http://junit.source.net/javadoc/org/junit/After.html)are finished.

The Activity can be manually launched with[launchActivity(Intent)](https://developer.android.com/reference/android/support/test/rule/ActivityTestRule#launchActivity(android.content.Intent)), and manually finished with[finishActivity()](https://developer.android.com/reference/android/support/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[getActivity()](https://developer.android.com/reference/android/support/test/rule/ActivityTestRule#getActivity()). If the Activity is finished and relaunched, the reference returned by[getActivity()](https://developer.android.com/reference/android/support/test/rule/ActivityTestRule#getActivity())will always point to the current instance of the Activity.

## Summary

|                                                                                                                                                                                                                                                                                                                                                                                                                                     ### Public constructors                                                                                                                                                                                                                                                                                                                                                                                                                                     ||
|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---|
| ` `[ActivityTestRule](https://developer.android.com/reference/android/support/test/rule/ActivityTestRule#ActivityTestRule(java.lang.Class<T>))`(`[Class](https://developer.android.com/reference/java/lang/Class.html)`<T> activityClass) ` Similar to[ActivityTestRule(Class, boolean)](https://developer.android.com/reference/android/support/test/rule/ActivityTestRule#ActivityTestRule(java.lang.Class<T>, boolean))but with "touch mode" disabled.                                                                                                                                                                                                                                                                                                                                                                                                                                    |
| ` `[ActivityTestRule](https://developer.android.com/reference/android/support/test/rule/ActivityTestRule#ActivityTestRule(java.lang.Class<T>, boolean))`(`[Class](https://developer.android.com/reference/java/lang/Class.html)`<T> activityClass, boolean initialTouchMode) ` Similar to[ActivityTestRule(Class, boolean, boolean)](https://developer.android.com/reference/android/support/test/rule/ActivityTestRule#ActivityTestRule(java.lang.Class<T>, boolean, boolean))but defaults to launch the activity under test once per[`Test`](http://junit.org/javadoc/latest/org/junit/Test.html)method.                                                                                                                                                                                                                                                                                   |
| ` `[ActivityTestRule](https://developer.android.com/reference/android/support/test/rule/ActivityTestRule#ActivityTestRule(java.lang.Class<T>, boolean, boolean))`(`[Class](https://developer.android.com/reference/java/lang/Class.html)`<T> activityClass, boolean initialTouchMode, boolean launchActivity) ` Similar to[ActivityTestRule(Class, String, int, boolean, boolean)](https://developer.android.com/reference/android/support/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()](https://developer.android.com/reference/android/support/test/InstrumentationRegistry#getTargetContext())and[FLAG_ACTIVITY_NEW_TASK](https://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_NEW_TASK)launch flag. |
| ` `[ActivityTestRule](https://developer.android.com/reference/android/support/test/rule/ActivityTestRule#ActivityTestRule(android.support.test.runner.intercepting.SingleActivityFactory<T>, boolean, boolean))`(`[SingleActivityFactory](https://developer.android.com/reference/android/support/test/runner/intercepting/SingleActivityFactory)`<T> activityFactory, boolean initialTouchMode, boolean launchActivity) ` Creates an[ActivityTestRule](https://developer.android.com/reference/android/support/test/rule/ActivityTestRule)for the Activity under test.                                                                                                                                                                                                                                                                                                                      |
| ` `[ActivityTestRule](https://developer.android.com/reference/android/support/test/rule/ActivityTestRule#ActivityTestRule(java.lang.Class<T>, java.lang.String, int, boolean, boolean))`(`[Class](https://developer.android.com/reference/java/lang/Class.html)`<T> activityClass, `[String](https://developer.android.com/reference/java/lang/String.html)` targetPackage, int launchFlags, boolean initialTouchMode, boolean launchActivity) ` Creates an[ActivityTestRule](https://developer.android.com/reference/android/support/test/rule/ActivityTestRule)for the Activity under test.                                                                                                                                                                                                                                                                                                |

|                                                                                                                                                                                                                                                                         ### Public methods                                                                                                                                                                                                                                                                          ||
|------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` Statement`                                                                                                                 | ` `[apply](https://developer.android.com/reference/android/support/test/rule/ActivityTestRule#apply(org.junit.runners.model.Statement, org.junit.runner.Description))`(Statement base, Description description) `                                                                                                                                                                                                                     |
| ` void`                                                                                                                      | ` `[finishActivity](https://developer.android.com/reference/android/support/test/rule/ActivityTestRule#finishActivity())`() ` Finishes the currently launched Activity.                                                                                                                                                                                                                                                               |
| ` T`                                                                                                                         | ` `[getActivity](https://developer.android.com/reference/android/support/test/rule/ActivityTestRule#getActivity())`() ` Returns the reference to the activity under test.                                                                                                                                                                                                                                                             |
| ` `[Instrumentation.ActivityResult](https://developer.android.com/reference/android/app/Instrumentation.ActivityResult.html) | ` `[getActivityResult](https://developer.android.com/reference/android/support/test/rule/ActivityTestRule#getActivityResult())`() ` This method can be used to retrieve the[Instrumentation.ActivityResult](https://developer.android.com/reference/android/app/Instrumentation.ActivityResult.html)of an Activity that has called[setResult(int)](https://developer.android.com/reference/android/app/Activity.html#setResult(int)). |
| ` T`                                                                                                                         | ` `[launchActivity](https://developer.android.com/reference/android/support/test/rule/ActivityTestRule#launchActivity(android.content.Intent))`(`[Intent](https://developer.android.com/reference/android/content/Intent.html)` startIntent) ` Launches the Activity under test.                                                                                                                                                      |
| ` void`                                                                                                                      | ` `[runOnUiThread](https://developer.android.com/reference/android/support/test/rule/ActivityTestRule#runOnUiThread(java.lang.Runnable))`(`[Runnable](https://developer.android.com/reference/java/lang/Runnable.html)` runnable) ` Helper method for running part of a method on the UI thread, blocking until it is complete.                                                                                                       |

|                                                                                                                                                                                                                                                      ### Protected methods                                                                                                                                                                                                                                                       ||
|----------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` void`                                                                          | ` `[afterActivityFinished](https://developer.android.com/reference/android/support/test/rule/ActivityTestRule#afterActivityFinished())`() ` Override this method to execute any code that should run after the currently launched[Activity](https://developer.android.com/reference/android/app/Activity.html)is finished.                                                                                                                     |
| ` void`                                                                          | ` `[afterActivityLaunched](https://developer.android.com/reference/android/support/test/rule/ActivityTestRule#afterActivityLaunched())`() ` Override this method to execute any code that should run after your[Activity](https://developer.android.com/reference/android/app/Activity.html)is launched, but before any test code is run including any method annotated with[`Before`](http://junit.source.net/javadoc/org/junit/Before.html). |
| ` void`                                                                          | ` `[beforeActivityLaunched](https://developer.android.com/reference/android/support/test/rule/ActivityTestRule#beforeActivityLaunched())`() ` Override this method to execute any code that should run before your[Activity](https://developer.android.com/reference/android/app/Activity.html)is created and launched.                                                                                                                        |
| ` `[Intent](https://developer.android.com/reference/android/content/Intent.html) | ` `[getActivityIntent](https://developer.android.com/reference/android/support/test/rule/ActivityTestRule#getActivityIntent())`() ` Override this method to set up a custom Intent as if supplied to[startActivity(Intent)](https://developer.android.com/reference/android/content/Context.html#startActivity(android.content.Intent)).                                                                                                       |

| ### Inherited methods |
|-----------------------|---|
| [![](https://developer.android.com/static/assets/images/styles/disclosure_down.png)](https://developer.android.com/reference/android/support/test/rule/ActivityTestRule#)From class` `[java.lang.Object](https://developer.android.com/reference/java/lang/Object.html)` ` |-------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------| | ` `[Object](https://developer.android.com/reference/java/lang/Object.html)          | ` clone() `                                                                                | | ` boolean`                                                                          | ` equals(`[Object](https://developer.android.com/reference/java/lang/Object.html)` arg0) ` | | ` void`                                                                             | ` finalize() `                                                                             | | ` final `[Class](https://developer.android.com/reference/java/lang/Class.html)`<?>` | ` getClass() `                                                                             | | ` int`                                                                              | ` hashCode() `                                                                             | | ` final void`                                                                       | ` notify() `                                                                               | | ` final void`                                                                       | ` notifyAll() `                                                                            | | ` `[String](https://developer.android.com/reference/java/lang/String.html)          | ` toString() `                                                                             | | ` final void`                                                                       | ` wait(long arg0, int arg1) `                                                              | | ` final void`                                                                       | ` wait(long arg0) `                                                                        | | ` final void`                                                                       | ` wait() `                                                                                 | ||
| [![](https://developer.android.com/static/assets/images/styles/disclosure_down.png)](https://developer.android.com/reference/android/support/test/rule/ActivityTestRule#)From interface` org.junit.rules.TestRule ` |-----------------------|---------------------------------------------| | ` abstract Statement` | ` apply(Statement arg0, Description arg1) ` | ||

## Public constructors

### ActivityTestRule

```
ActivityTestRule (Class<T> activityClass)
```

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

<br />

|                                                                        Parameters                                                                        ||
|-----------------|-----------------------------------------------------------------------------------------------------------------------------------------|
| `activityClass` | `Class`: The activity under test. This must be a class in the instrumentation targetPackage specified in the AndroidManifest.xml <br /> |

**See also:**

- [ActivityTestRule(Class, boolean, boolean)](https://developer.android.com/reference/android/support/test/rule/ActivityTestRule#ActivityTestRule(java.lang.Class<T>, boolean, boolean))  

### ActivityTestRule

```
ActivityTestRule (Class<T> activityClass, 
                boolean initialTouchMode)
```

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

<br />

|                                                                         Parameters                                                                          ||
|--------------------|-----------------------------------------------------------------------------------------------------------------------------------------|
| `activityClass`    | `Class`: The activity under test. This must be a class in the instrumentation targetPackage specified in the AndroidManifest.xml <br /> |
| `initialTouchMode` | `boolean`: true if the Activity should be placed into "touch mode" when started <br />                                                  |

**See also:**

- [ActivityTestRule(Class, boolean, boolean)](https://developer.android.com/reference/android/support/test/rule/ActivityTestRule#ActivityTestRule(java.lang.Class<T>, boolean, boolean))  

### ActivityTestRule

```
ActivityTestRule (Class<T> activityClass, 
                boolean initialTouchMode, 
                boolean launchActivity)
```

Similar to[ActivityTestRule(Class, String, int, boolean, boolean)](https://developer.android.com/reference/android/support/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()](https://developer.android.com/reference/android/support/test/InstrumentationRegistry#getTargetContext())and[FLAG_ACTIVITY_NEW_TASK](https://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_NEW_TASK)launch flag.

<br />

|                                                                                                                                                                                 Parameters                                                                                                                                                                                  ||
|--------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `activityClass`    | `Class`: The activity under test. This must be a class in the instrumentation targetPackage specified in the AndroidManifest.xml <br />                                                                                                                                                                                                                 |
| `initialTouchMode` | `boolean`: true if the Activity should be placed into "touch mode" when started <br />                                                                                                                                                                                                                                                                  |
| `launchActivity`   | `boolean`: true if the Activity should be launched once per[`Test`](http://junit.org/javadoc/latest/org/junit/Test.html)method. It will be launched before the first[`Before`](http://junit.source.net/javadoc/org/junit/Before.html)method, and terminated after the last[`After`](http://junit.source.net/javadoc/org/junit/After.html)method. <br /> |

### ActivityTestRule

```
ActivityTestRule (SingleActivityFactory<T> activityFactory, 
                boolean initialTouchMode, 
                boolean launchActivity)
```

Creates an[ActivityTestRule](https://developer.android.com/reference/android/support/test/rule/ActivityTestRule)for the Activity under test.

<br />

|                                                                                                                                                                                 Parameters                                                                                                                                                                                  ||
|--------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `activityFactory`  | `SingleActivityFactory`: factory to be used for creating Activity instance <br />                                                                                                                                                                                                                                                                       |
| `initialTouchMode` | `boolean`: true if the Activity should be placed into "touch mode" when started <br />                                                                                                                                                                                                                                                                  |
| `launchActivity`   | `boolean`: true if the Activity should be launched once per[`Test`](http://junit.org/javadoc/latest/org/junit/Test.html)method. It will be launched before the first[`Before`](http://junit.source.net/javadoc/org/junit/Before.html)method, and terminated after the last[`After`](http://junit.source.net/javadoc/org/junit/After.html)method. <br /> |

### ActivityTestRule

```
ActivityTestRule (Class<T> activityClass, 
                String targetPackage, 
                int launchFlags, 
                boolean initialTouchMode, 
                boolean launchActivity)
```

Creates an[ActivityTestRule](https://developer.android.com/reference/android/support/test/rule/ActivityTestRule)for the Activity under test.

<br />

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

## Public methods

### apply

```
Statement apply (Statement base, 
                Description description)
```

<br />

<br />

|             Parameters              ||
|---------------|----------------------|
| `base`        | `Statement` <br />   |
| `description` | `Description` <br /> |

|       Returns       ||
|-------------|--------|
| `Statement` | <br /> |

### finishActivity

```
void finishActivity ()
```

Finishes the currently launched Activity.

<br />

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

### getActivity

```
T getActivity ()
```

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[ERROR(/Activity#OnResumed())](https://developer.android.com/)lifecycle change.

**Note:**Lifecycle changes happen on the UI thread (not the instrumenation 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[ERROR(/Activity#OnResumed())](https://developer.android.com/)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 destroyed (ex. back button was pressed) then the system no longer holds a strong reference to the acitivty and this refernce may get garbage collected.

<br />

|   Returns   ||
|-----|--------|
| `T` | <br /> |

### getActivityResult

```
Instrumentation.ActivityResult getActivityResult ()
```

This method can be used to retrieve the[Instrumentation.ActivityResult](https://developer.android.com/reference/android/app/Instrumentation.ActivityResult.html)of an Activity that has called[setResult(int)](https://developer.android.com/reference/android/app/Activity.html#setResult(int)). Usually, the result is handled in[onActivityResult(int, int, Intent)](https://developer.android.com/reference/android/app/Activity.html#onActivityResult(int, int, android.content.Intent))of the parent Activity, that has called[startActivityForResult(Intent, int)](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[setResult(int)](https://developer.android.com/reference/android/app/Activity.html#setResult(int))is called no later than in[onPause()](https://developer.android.com/reference/android/app/Activity.html#onPause()).

<br />

|                                                                                                                                         Returns                                                                                                                                          ||
|---------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [Instrumentation.ActivityResult](https://developer.android.com/reference/android/app/Instrumentation.ActivityResult.html) | the[Instrumentation.ActivityResult](https://developer.android.com/reference/android/app/Instrumentation.ActivityResult.html)that was set most recently <br /> |

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

### launchActivity

```
T launchActivity (Intent startIntent)
```

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[ActivityTestRule(Class, boolean, boolean)](https://developer.android.com/reference/android/support/test/rule/ActivityTestRule#ActivityTestRule(java.lang.Class<T>, boolean, boolean)).

Usage:  

```
    @Test
    public void customIntentToStartActivity() {
        Intent intent = new Intent(Intent.ACTION_PICK);
        mActivity = 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[getActivityIntent()](https://developer.android.com/reference/android/support/test/rule/ActivityTestRule#getActivityIntent()). The same override rules documented in[getActivityIntent()](https://developer.android.com/reference/android/support/test/rule/ActivityTestRule#getActivityIntent())apply.

<br />

<br />

|                                                                                                                                     Parameters                                                                                                                                     ||
|---------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `startIntent` | `Intent`: The Intent that will be used to start the Activity under test. If`startIntent`is null, the Intent returned by[getActivityIntent()](https://developer.android.com/reference/android/support/test/rule/ActivityTestRule#getActivityIntent())is used. <br /> |

|                     Returns                     ||
|-----|--------------------------------------------|
| `T` | the Activity launched by this rule. <br /> |

### runOnUiThread

```
void runOnUiThread (Runnable runnable)
```

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[UiThreadTest](https://developer.android.com/reference/android/support/test/annotation/UiThreadTest).

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

<br />

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

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

**See also:**

- [UiThreadTest](https://developer.android.com/reference/android/support/test/annotation/UiThreadTest)

## Protected methods

### afterActivityFinished

```
void afterActivityFinished ()
```

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

Prefer[`Before
`](http://junit.source.net/javadoc/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.

<br />

### afterActivityLaunched

```
void afterActivityLaunched ()
```

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

Prefer[`Before
`](http://junit.source.net/javadoc/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.

<br />

### beforeActivityLaunched

```
void beforeActivityLaunched ()
```

Override this method to execute any code that should run before your[Activity](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[`
Before`](http://junit.source.net/javadoc/org/junit/Before.html).

<br />

### getActivityIntent

```
Intent getActivityIntent ()
```

Override this method to set up a custom Intent as if supplied to[startActivity(Intent)](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[launchActivity(Intent)](https://developer.android.com/reference/android/support/test/rule/ActivityTestRule#launchActivity(android.content.Intent)).

The default Intent (if this method returns null or is not overwritten) is: action =[ACTION_MAIN](https://developer.android.com/reference/android/content/Intent.html#ACTION_MAIN)flags =[FLAG_ACTIVITY_NEW_TASK](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[setClassName(String, String)](https://developer.android.com/reference/android/content/Intent.html#setClassName(java.lang.String, java.lang.String))
- launch flags through[setFlags(int)](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[ActivityTestRule(Class, String, int, boolean, boolean)](https://developer.android.com/reference/android/support/test/rule/ActivityTestRule#ActivityTestRule(java.lang.Class<T>, java.lang.String, int, boolean, boolean))are supplemented.

<br />

|                                                                                                                        Returns                                                                                                                         ||
|-------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [Intent](https://developer.android.com/reference/android/content/Intent.html) | The Intent as if supplied to[startActivity(Intent)](https://developer.android.com/reference/android/content/Context.html#startActivity(android.content.Intent)). <br /> |

-

  ## Classes

  - [ActivityTestRule](https://developer.android.com/reference/android/support/test/rule/ActivityTestRule)
  - [DisableOnAndroidDebug](https://developer.android.com/reference/android/support/test/rule/DisableOnAndroidDebug)
  - [GrantPermissionRule](https://developer.android.com/reference/android/support/test/rule/GrantPermissionRule)
  - [ServiceTestRule](https://developer.android.com/reference/android/support/test/rule/ServiceTestRule)
  - [UiThreadTestRule](https://developer.android.com/reference/android/support/test/rule/UiThreadTestRule)