| ActivityTestRule<T extends Activity> |
This class is deprecated.
use ERROR(/androidx.test.core.app.ActivityScenario) or ERROR(/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:
- For simple cases where you want to launch the Activity before each test and tear it
down after each test (eg you are using
ActivityTestRule(Class)), convert
directly to ActivityScenarioRule.
- If you need control over when to launch the Activity (eg you are using
ERROR(/#ActivityTestRule(Class, false, false)), use ActivityScenario.launch. Its recommended
to wrap the launch in a try-block, so the Activity is closed automatically.
try (ActivityScenario.launch(activityClass)) {
...
}
- If you need access to the Activity during the test (eg you are calling
getActivity() provide a Runnable callback to ERROR(/androidx.test.core.app.ActivityScenario#onActivity(Runnable)) instead. The callback
provided to onActivity will run on the application's main thread, thus ensuring a safer
mechanism to access the Activity.
|