What problem does it solve?
This Skill helps you test an Android Fragment in isolation without launching a full Activity, while avoiding common crashes related to theming, lifecycle state, fragment factories, and navigation wiring.
Core Features & Use Cases
- FragmentScenario-based isolated launches: use
launchFragmentInContainer<F>() for in-container UI lifecycle and launchFragment<F>() for headless fragment behavior.
- Correct artifact configuration on Android: handle the required split between
androidx.fragment:fragment-testing (API) and androidx.fragment:fragment-testing-manifest (manifest-hosted EmptyFragmentActivity) so the host Activity is found.
- Theme and navigation gotchas: fix AppCompat/Material theme inflation errors via
themeResId, and address findNavController() failures by installing a TestNavHostController (since the default host does not provide a NavHostFragment).
- Lifecycle control and safety constraints: drive lifecycle with
moveToState, support recreate(), and avoid the invalid initial DESTROYED state error.
Quick Start
Add androidx.fragment:fragment-testing to androidTestImplementation, add androidx.fragment:fragment-testing-manifest to debugImplementation, then launch your fragment with launchFragmentInContainer<YourFragment>() (and set themeResId if it uses AppCompat/Material).