launching-activities-with-activityscenario

Launch Android Activities with ActivityScenario for instrumentation testing.

303|10|Updated May 15, 2026
One-click install
npx skills add https://github.com/skydoves/android-testing-skills --skill launching-activities-with-activityscenario
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: launching-activities-with-activityscenario
Source: https://github.com/skydoves/android-testing-skills/tree/main/instrumentation/scenarios/launching-activities-with-activityscenario
Command: npx skills add https://github.com/skydoves/android-testing-skills --skill launching-activities-with-activityscenario

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

It solves flaky or failing Android instrumentation tests by showing the canonical, lifecycle-safe way to launch an Activity with ActivityScenario instead of deprecated ActivityTestRule, including correct threading, recreation, and teardown.

Core Features & Use Cases

  • Canonical ActivityScenario launch & lifecycle control: Covers ActivityScenario.launch, launch with Intent, launchActivityForResult, moveToState across CREATED/STARTED/RESUMED/DESTROYED, and proper closing behavior.
  • UI-thread safe interaction: Uses onActivity { } to mutate or read views/state without deadlocks and explains how to avoid calling scenario APIs from the main thread.
  • Configuration-change emulation: Uses recreate() to trigger state save/restore flows and explains why rememberSaveable/SavedStateHandle/onSaveInstanceState survive while plain remember does not.
  • Common migration traps: Clarifies the single canonical FQN for ActivityScenarioRule (androidx.test.ext.junit.rules.ActivityScenarioRule) and avoids the non-existent androidx.test.rule.ActivityScenarioRule confusion.

Quick Start

Use the launching-activities-with-activityscenario skill to show how to migrate from ActivityTestRule to ActivityScenarioRule and launch the Activity safely in an instrumentation test.

Frequently Asked Questions about launching-activities-with-activityscenario

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I migrate from ActivityTestRule to ActivityScenario for Android instrumentation testing?

To migrate from ActivityTestRule to ActivityScenario, use ActivityScenarioRule with the canonical FQN androidx.test.ext.junit.rules.ActivityScenarioRule to launch Activities safely and ensure proper lifecycle-aware teardown.

How do I test Android lifecycle transitions using moveToState?

You can test Android lifecycle transitions using ActivityScenario by calling moveToState to drive an Activity through CREATED, STARTED, RESUMED, or DESTROYED states, verifying behavior at each lifecycle phase.

How do I interact with UI views safely in ActivityScenario tests without deadlocking?

To interact with UI views safely without deadlocks in ActivityScenario tests, use the onActivity block to execute view mutations and state reads on the UI thread, avoiding direct scenario API calls from the main thread.

How do I simulate configuration changes to test state restoration in Android instrumentation tests?

To simulate configuration changes and test state restoration, call recreate on your ActivityScenario to trigger state save and restore flows, verifying that rememberSaveable, SavedStateHandle, and onSaveInstanceState survive the process.

How do I assert results from an Activity launched with launchActivityForResult?

You can assert results from an Activity launched with launchActivityForResult by accessing scenario.result, which captures the returned Intent data and result code for robust instrumentation test assertions.

Why does my ActivityScenario test fail with a main thread deadlock?

ActivityScenario tests fail with main thread deadlocks when scenario APIs are incorrectly called from the UI thread; prevent this by wrapping interactions in onActivity and ensuring proper cleanup via close or the JUnit rule.