The Android Developer Challenge is back! Submit your idea before December 2.

ActivityScenario.ActivityAction

public static interface ActivityScenario.ActivityAction

androidx.test.core.app.ActivityScenario.ActivityAction<A extends android.app.Activity>


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(A) method.

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(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.

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 methods

abstract void perform(A activity)

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

Public methods

perform

void perform (A activity)

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

Parameters
activity A: an Activity instrumented by the ActivityScenario. It never be null.