launching-fragments-with-fragmentscenario

Tests Android Fragments in isolation using FragmentScenario launchers and lifecycle controls.

1|Updated Jul 6, 2026
One-click install
npx skills add https://github.com/citytexi/team-yg-pesonal-agent --skill launching-fragments-with-fragmentscenario-citytexi
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: launching-fragments-with-fragmentscenario
Source: https://github.com/citytexi/team-yg-pesonal-agent/tree/main/.claude/skills/launching-fragments-with-fragmentscenario
Command: npx skills add https://github.com/citytexi/team-yg-pesonal-agent --skill launching-fragments-with-fragmentscenario-citytexi

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Testing a single Android Fragment without launching a full host Activity is error-prone: the default FragmentScenario theme is not AppCompat, the manifest artifact must sit on the right Gradle configuration, and findNavController() crashes because EmptyFragmentActivity has no NavHostFragment. This Skill encodes the correct setup and the known traps so instrumentation tests run on the first attempt. ## Core Features & Use Cases - Correct dependency setup: Places androidx.fragment:fragment-testing on androidTestImplementation and fragment-testing-manifest on debugImplementation so EmptyFragmentActivity is merged into the test APK manifest. - Launcher selection and configuration: Chooses between launchFragmentInContainer (attached to android.R.id.content) and launchFragment (headless), passes fragmentArgs, overrides themeResId for AppCompat/Material fragments, and injects a FragmentFactory for non-default constructors. - Lifecycle and navigation handling: Drives states with moveToState and recreate, captures state via withFragment, and works around the findNavController limitation with TestNavHostController. - Use Case: A test crashes with "You need to use a Theme.AppCompat theme (or descendant)" when launching a login fragment; the fix is passing themeResId = R.style.Theme_MyApp to launchFragmentInContainer. ## Quick Start Ask the AI to write an isolated instrumentation test for your Fragment using FragmentScenario with the correct dependencies, theme override, and lifecycle assertions.

Frequently Asked Questions about launching-fragments-with-fragmentscenario

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

FAQPage Schema
How do I test an Android Fragment in isolation?▼

Use FragmentScenario from androidx.fragment:fragment-testing. Call launchFragmentInContainer<MyFragment>() to attach the fragment to android.R.id.content with a full view lifecycle, or launchFragment<MyFragment>() for a headless launch with containerViewId 0.

Why does FragmentScenario crash with "You need to use a Theme.AppCompat theme"?▼

The default FragmentScenarioEmptyFragmentActivityTheme extends android:Theme.WithActionBar, the platform Holo theme, not AppCompat. Pass themeResId = R.style.YourAppTheme to launchFragmentInContainer so AppCompat or Material widgets inflate correctly.

Why does launchFragmentInContainer throw ActivityNotFoundException for EmptyFragmentActivity?▼

The androidx.fragment:fragment-testing-manifest artifact, which declares EmptyFragmentActivity, must be on debugImplementation (or testImplementation for host tests). On androidTestImplementation the manifest entry is not merged into the test APK.

Can I use findNavController inside a FragmentScenario test?▼

No. EmptyFragmentActivity does not install a NavHostFragment, so findNavController() throws IllegalStateException. Install a TestNavHostController on the fragment view with Navigation.setViewNavController, or use a custom test host Activity.

How do I inject dependencies into a Fragment under FragmentScenario?▼

Pass a FragmentFactory to the launcher's factory parameter, or use the inline lambda overload launchFragmentInContainer { MyFragment(FakeRepo()) }. The factory is stored in a ViewModel-backed holder so it survives scenario.recreate().

Why is initialState DESTROYED rejected by FragmentScenario?▼

FragmentScenario explicitly rejects Lifecycle.State.DESTROYED as an initial state with an IllegalArgumentException. Launch with CREATED instead, then call moveToState(Lifecycle.State.DESTROYED), which is allowed as a terminal transition.