Skip to content
tezvyn:
Menu

ActivityScenario: Test Your Android Activities in Isolation

Source: developer.android.comMediumHow cards are made

ActivityScenario lets you launch and control an Android Activity's lifecycle within an instrumented test. Use it to verify UI behavior during events like screen rotation or process death, isolating the component for reliable testing.

Why it exists

Testing Android Activities is complex because they are tied to the OS and a complicated lifecycle. Manually triggering states like process death or screen rotation is difficult and unreliable. ActivityScenario was created to provide a stable, official API for driving Activities through these states in a controlled test environment.

The mental model

Think of ActivityScenario as a remote control for a single Activity. Instead of launching your whole app to test one screen, you use the scenario to launch just that screen in a hermetic test environment. You can then use the remote to press buttons like "rotate screen," "send to background," or "destroy and recreate," and then check if the screen's UI is still correct.

How it works

ActivityScenario is part of the AndroidX Test library. You launch a scenario using ActivityScenario.launch(MyActivity.class). This creates and brings the Activity to the RESUMED state. You can then use methods like moveToState(Lifecycle.State.CREATED) to simulate lifecycle events. To interact with the Activity's UI, for example with Espresso, you use scenario.onActivity { activity -> ... } to get a safe reference to the Activity instance.

When to use it

Use ActivityScenario for instrumented UI tests that need to verify an Activity's behavior. It's ideal for testing state restoration after a configuration change (e.g., rotating the device), checking for memory leaks, or ensuring correct behavior when the Activity is paused and resumed. It is the standard for modern Android UI testing, replacing the older ActivityTestRule.

When not to use it

Don't use ActivityScenario for simple unit tests that don't involve the Android framework or an Activity's lifecycle. For testing ViewModels or business logic, a plain JUnit test is faster and more appropriate. It is also not designed for testing complex navigation flows between multiple Activities; for that, you would orchestrate tests at a higher level.

One canonical example

A common test is to verify that text entered into an EditText survives a screen rotation. You would launch the scenario, use Espresso to type text into the field, then call scenario.recreate() to simulate the rotation. Finally, you'd use Espresso again to assert that the EditText still contains the original text, confirming your state-saving logic works.

Interview question

Which testing goal is best addressed by using ActivityScenario?

  • a.Performing isolated unit tests on utility classes without Android dependencies.
  • b.Ensuring an Activity's UI state persists across configuration changes.Correct
  • c.Verifying the correct behavior of a ViewModel's business logic.
  • d.Testing complex navigation paths involving multiple Activities.
Why?

ActivityScenario is explicitly recommended for instrumented UI tests that verify an Activity's behavior, such as testing state restoration after a configuration change. It is not designed for unit testing business logic, utility classes, or complex navigation flows between multiple Activities.

Just read this? Test yourself on what you have been reading.

Read the original → developer.android.com

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on android — each one lists the topics its interview covers.

See open roles