# ActivityScenario.ActivityAction

# ActivityScenario.ActivityAction

Artifact:[androidx.test:core](https://developer.android.com/jetpack/androidx/releases/test)  
[View Source](https://cs.android.com/search?q=file:androidx/test/core/app/ActivityScenario.java+class:androidx.test.core.app.ActivityScenario.ActivityAction)

*** ** * ** ***

Kotlin\|[Java](https://developer.android.com/reference/androidx/test/core/app/ActivityScenario.ActivityAction "View this page in Java")

<br />

```
interface ActivityScenario.ActivityAction<A : Activity?>
```

<br />

*** ** * ** ***

ActivityAction interface should be implemented by any class whose instances are intended to be executed by the main thread. An Activity that is instrumented by the ActivityScenario is passed to[perform](https://developer.android.com/reference/kotlin/androidx/test/core/app/ActivityScenario.ActivityAction#perform(A))method.  

```kotlin
Example:
  ActivityScenario<MyActivity> scenario = ActivityScenario.launch(MyActivity.class);
  scenario.onActivity(activity -> {
    assertThat(activity.getSomething()).isEqualTo("something");
  });
```

You should never keep the Activity reference. It should only be accessed in[perform](https://developer.android.com/reference/kotlin/androidx/test/core/app/ActivityScenario.ActivityAction#perform(A))scope for two reasons: 1) Android framework may re-create the Activity during lifecycle changes, your holding reference might be stale. 2) It increases the reference counter and it may affect to the framework behavior, especially after you finish the Activity.  

```kotlin
Bad Example:
  ActivityScenario<MyActivity> scenario = ActivityScenario.launch(MyActivity.class);
  final MyActivity[] myActivityHolder = new MyActivity[1];
  scenario.onActivity(activity -> {
    myActivityHolder[0] = activity;
  });
  assertThat(myActivityHolder[0].getSomething()).isEqualTo("something");
```

## Summary

|                             ### Public functions                              |
|-------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html) | [perform](https://developer.android.com/reference/kotlin/androidx/test/core/app/ActivityScenario.ActivityAction#perform(A))`(activity: A!)` This method is invoked on the main thread with the reference to the Activity. |

## Public functions

### perform

```
fun perform(activity: A!): Unit
```

This method is invoked on the main thread with the reference to the Activity.  

|   Parameters   |
|----------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `activity: A!` | an Activity instrumented by the[ActivityScenario](https://developer.android.com/reference/kotlin/androidx/test/core/app/ActivityScenario). It never be null. |