# androidx.fragment.app.testing

*** ** * ** ***

## Interfaces

|---|---|
| `https://developer.android.com/reference/kotlin/androidx/fragment/app/testing/FragmentScenario.FragmentAction` | FragmentAction interface should be implemented by any class whose instances are intended to be executed by the main thread. |

## Classes

|---|---|
| `https://developer.android.com/reference/kotlin/androidx/fragment/app/testing/FragmentScenario` | FragmentScenario provides API to start and drive a Fragment's lifecycle state for testing. |

## Top-level functions summary

|---|---|
| `inline https://developer.android.com/reference/kotlin/androidx/fragment/app/testing/FragmentScenario<F>` | `<F : https://developer.android.com/reference/kotlin/androidx/fragment/app/Fragment> https://developer.android.com/reference/kotlin/androidx/fragment/app/testing/package-summary#launchFragment(android.os.Bundle,kotlin.Int,androidx.lifecycle.Lifecycle.State,androidx.fragment.app.FragmentFactory)( fragmentArgs: https://developer.android.com/reference/android/os/Bundle.html?, themeResId: @https://developer.android.com/reference/kotlin/androidx/annotation/StyleRes https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, initialState: https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State, factory: https://developer.android.com/reference/kotlin/androidx/fragment/app/FragmentFactory? )` Launches a Fragment with given arguments hosted by an empty `https://developer.android.com/reference/kotlin/androidx/fragment/app/FragmentActivity` using given `https://developer.android.com/reference/kotlin/androidx/fragment/app/FragmentFactory` and waits for it to reach `https://developer.android.com/reference/kotlin/androidx/fragment/app/testing/package-summary#launchFragment(android.os.Bundle,kotlin.Int,androidx.lifecycle.Lifecycle.State,androidx.fragment.app.FragmentFactory)`. |
| `inline https://developer.android.com/reference/kotlin/androidx/fragment/app/testing/FragmentScenario<F>` | `<F : https://developer.android.com/reference/kotlin/androidx/fragment/app/Fragment> https://developer.android.com/reference/kotlin/androidx/fragment/app/testing/package-summary#launchFragment(android.os.Bundle,kotlin.Int,androidx.lifecycle.Lifecycle.State,kotlin.Function0)( fragmentArgs: https://developer.android.com/reference/android/os/Bundle.html?, themeResId: @https://developer.android.com/reference/kotlin/androidx/annotation/StyleRes https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, initialState: https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State, crossinline instantiate: () -> F )` Launches a Fragment with given arguments hosted by an empty `https://developer.android.com/reference/kotlin/androidx/fragment/app/FragmentActivity` using `https://developer.android.com/reference/kotlin/androidx/fragment/app/testing/package-summary#launchFragment(android.os.Bundle,kotlin.Int,androidx.lifecycle.Lifecycle.State,kotlin.Function0)` to create the Fragment and waits for it to reach `https://developer.android.com/reference/kotlin/androidx/fragment/app/testing/package-summary#launchFragment(android.os.Bundle,kotlin.Int,androidx.lifecycle.Lifecycle.State,kotlin.Function0)`. |
| `inline https://developer.android.com/reference/kotlin/androidx/fragment/app/testing/FragmentScenario<F>` | `<F : https://developer.android.com/reference/kotlin/androidx/fragment/app/Fragment> https://developer.android.com/reference/kotlin/androidx/fragment/app/testing/package-summary#launchFragmentInContainer(android.os.Bundle,kotlin.Int,androidx.lifecycle.Lifecycle.State,androidx.fragment.app.FragmentFactory)( fragmentArgs: https://developer.android.com/reference/android/os/Bundle.html?, themeResId: @https://developer.android.com/reference/kotlin/androidx/annotation/StyleRes https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, initialState: https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State, factory: https://developer.android.com/reference/kotlin/androidx/fragment/app/FragmentFactory? )` Launches a Fragment in the Activity's root view container `android.R.id.content`, with given arguments hosted by an empty `https://developer.android.com/reference/kotlin/androidx/fragment/app/FragmentActivity` and waits for it to reach `https://developer.android.com/reference/kotlin/androidx/fragment/app/testing/package-summary#launchFragmentInContainer(android.os.Bundle,kotlin.Int,androidx.lifecycle.Lifecycle.State,androidx.fragment.app.FragmentFactory)`. |
| `inline https://developer.android.com/reference/kotlin/androidx/fragment/app/testing/FragmentScenario<F>` | `<F : https://developer.android.com/reference/kotlin/androidx/fragment/app/Fragment> https://developer.android.com/reference/kotlin/androidx/fragment/app/testing/package-summary#launchFragmentInContainer(android.os.Bundle,kotlin.Int,androidx.lifecycle.Lifecycle.State,kotlin.Function0)( fragmentArgs: https://developer.android.com/reference/android/os/Bundle.html?, themeResId: @https://developer.android.com/reference/kotlin/androidx/annotation/StyleRes https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, initialState: https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State, crossinline instantiate: () -> F )` Launches a Fragment in the Activity's root view container `android.R.id.content`, with given arguments hosted by an empty `https://developer.android.com/reference/kotlin/androidx/fragment/app/FragmentActivity` using `https://developer.android.com/reference/kotlin/androidx/fragment/app/testing/package-summary#launchFragmentInContainer(android.os.Bundle,kotlin.Int,androidx.lifecycle.Lifecycle.State,kotlin.Function0)` to create the Fragment and waits for it to reach `https://developer.android.com/reference/kotlin/androidx/fragment/app/testing/package-summary#launchFragmentInContainer(android.os.Bundle,kotlin.Int,androidx.lifecycle.Lifecycle.State,kotlin.Function0)`. |

## Extension functions summary

|---|---|
| `inline T` | `<F : https://developer.android.com/reference/kotlin/androidx/fragment/app/Fragment, T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html> https://developer.android.com/reference/kotlin/androidx/fragment/app/testing/FragmentScenario<F>.https://developer.android.com/reference/kotlin/androidx/fragment/app/testing/package-summary#(androidx.fragment.app.testing.FragmentScenario).withFragment(kotlin.Function1)( crossinline block: F.() -> T )` Run `https://developer.android.com/reference/kotlin/androidx/fragment/app/testing/package-summary#(androidx.fragment.app.testing.FragmentScenario).withFragment(kotlin.Function1)` using `https://developer.android.com/reference/kotlin/androidx/fragment/app/testing/FragmentScenario#onFragment(androidx.fragment.app.testing.FragmentScenario.FragmentAction)`, returning the result of the `https://developer.android.com/reference/kotlin/androidx/fragment/app/testing/package-summary#(androidx.fragment.app.testing.FragmentScenario).withFragment(kotlin.Function1)`. |

## Top-level functions

### launchFragment

Artifact: [androidx.fragment:fragment-testing](https://developer.android.com/jetpack/androidx/releases/fragment) [View Source](https://cs.android.com/search?q=file:androidx/fragment/app/testing/FragmentScenario.kt+function:launchFragment)

```
inline fun <F : Fragment> launchFragment(
    fragmentArgs: Bundle? = null,
    themeResId: @StyleRes Int = R.style.FragmentScenarioEmptyFragmentActivityTheme,
    initialState: Lifecycle.State = Lifecycle.State.RESUMED,
    factory: FragmentFactory? = null
): FragmentScenario<F>
```

Launches a Fragment with given arguments hosted by an empty `https://developer.android.com/reference/kotlin/androidx/fragment/app/FragmentActivity` using given `https://developer.android.com/reference/kotlin/androidx/fragment/app/FragmentFactory` and waits for it to reach `https://developer.android.com/reference/kotlin/androidx/fragment/app/testing/package-summary#launchFragment(android.os.Bundle,kotlin.Int,androidx.lifecycle.Lifecycle.State,androidx.fragment.app.FragmentFactory)`.

This method cannot be called from the main thread.

| Parameters |
|---|---|
| `fragmentArgs: https://developer.android.com/reference/android/os/Bundle.html? = null` | a bundle to passed into fragment |
| `themeResId: @https://developer.android.com/reference/kotlin/androidx/annotation/StyleRes https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html = R.style.FragmentScenarioEmptyFragmentActivityTheme` | a style resource id to be set to the host activity's theme |
| `initialState: https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State = Lifecycle.State.RESUMED` | the initial `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State`. Passing in `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State#DESTROYED` will result in an `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-argument-exception/index.html`. |
| `factory: https://developer.android.com/reference/kotlin/androidx/fragment/app/FragmentFactory? = null` | a fragment factory to use or null to use default factory |

### launchFragment

Artifact: [androidx.fragment:fragment-testing](https://developer.android.com/jetpack/androidx/releases/fragment) [View Source](https://cs.android.com/search?q=file:androidx/fragment/app/testing/FragmentScenario.kt+function:launchFragment)

```
inline fun <F : Fragment> launchFragment(
    fragmentArgs: Bundle? = null,
    themeResId: @StyleRes Int = R.style.FragmentScenarioEmptyFragmentActivityTheme,
    initialState: Lifecycle.State = Lifecycle.State.RESUMED,
    crossinline instantiate: () -> F
): FragmentScenario<F>
```

Launches a Fragment with given arguments hosted by an empty `https://developer.android.com/reference/kotlin/androidx/fragment/app/FragmentActivity` using `https://developer.android.com/reference/kotlin/androidx/fragment/app/testing/package-summary#launchFragment(android.os.Bundle,kotlin.Int,androidx.lifecycle.Lifecycle.State,kotlin.Function0)` to create the Fragment and waits for it to reach `https://developer.android.com/reference/kotlin/androidx/fragment/app/testing/package-summary#launchFragment(android.os.Bundle,kotlin.Int,androidx.lifecycle.Lifecycle.State,kotlin.Function0)`.

This method cannot be called from the main thread.

| Parameters |
|---|---|
| `fragmentArgs: https://developer.android.com/reference/android/os/Bundle.html? = null` | a bundle to passed into fragment |
| `themeResId: @https://developer.android.com/reference/kotlin/androidx/annotation/StyleRes https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html = R.style.FragmentScenarioEmptyFragmentActivityTheme` | a style resource id to be set to the host activity's theme |
| `initialState: https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State = Lifecycle.State.RESUMED` | the initial `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State`. Passing in `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State#DESTROYED` will result in an `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-argument-exception/index.html`. |
| `crossinline instantiate: () -> F` | method which will be used to instantiate the Fragment. |

### launchFragmentInContainer

Artifact: [androidx.fragment:fragment-testing](https://developer.android.com/jetpack/androidx/releases/fragment) [View Source](https://cs.android.com/search?q=file:androidx/fragment/app/testing/FragmentScenario.kt+function:launchFragmentInContainer)

```
inline fun <F : Fragment> launchFragmentInContainer(
    fragmentArgs: Bundle? = null,
    themeResId: @StyleRes Int = R.style.FragmentScenarioEmptyFragmentActivityTheme,
    initialState: Lifecycle.State = Lifecycle.State.RESUMED,
    factory: FragmentFactory? = null
): FragmentScenario<F>
```

Launches a Fragment in the Activity's root view container `android.R.id.content`, with given arguments hosted by an empty `https://developer.android.com/reference/kotlin/androidx/fragment/app/FragmentActivity` and waits for it to reach `https://developer.android.com/reference/kotlin/androidx/fragment/app/testing/package-summary#launchFragmentInContainer(android.os.Bundle,kotlin.Int,androidx.lifecycle.Lifecycle.State,androidx.fragment.app.FragmentFactory)`.

This method cannot be called from the main thread.

| Parameters |
|---|---|
| `fragmentArgs: https://developer.android.com/reference/android/os/Bundle.html? = null` | a bundle to passed into fragment |
| `themeResId: @https://developer.android.com/reference/kotlin/androidx/annotation/StyleRes https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html = R.style.FragmentScenarioEmptyFragmentActivityTheme` | a style resource id to be set to the host activity's theme |
| `initialState: https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State = Lifecycle.State.RESUMED` | the initial `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State`. Passing in `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State#DESTROYED` will result in an `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-argument-exception/index.html`. |
| `factory: https://developer.android.com/reference/kotlin/androidx/fragment/app/FragmentFactory? = null` | a fragment factory to use or null to use default factory |

### launchFragmentInContainer

Artifact: [androidx.fragment:fragment-testing](https://developer.android.com/jetpack/androidx/releases/fragment) [View Source](https://cs.android.com/search?q=file:androidx/fragment/app/testing/FragmentScenario.kt+function:launchFragmentInContainer)

```
inline fun <F : Fragment> launchFragmentInContainer(
    fragmentArgs: Bundle? = null,
    themeResId: @StyleRes Int = R.style.FragmentScenarioEmptyFragmentActivityTheme,
    initialState: Lifecycle.State = Lifecycle.State.RESUMED,
    crossinline instantiate: () -> F
): FragmentScenario<F>
```

Launches a Fragment in the Activity's root view container `android.R.id.content`, with given arguments hosted by an empty `https://developer.android.com/reference/kotlin/androidx/fragment/app/FragmentActivity` using `https://developer.android.com/reference/kotlin/androidx/fragment/app/testing/package-summary#launchFragmentInContainer(android.os.Bundle,kotlin.Int,androidx.lifecycle.Lifecycle.State,kotlin.Function0)` to create the Fragment and waits for it to reach `https://developer.android.com/reference/kotlin/androidx/fragment/app/testing/package-summary#launchFragmentInContainer(android.os.Bundle,kotlin.Int,androidx.lifecycle.Lifecycle.State,kotlin.Function0)`.

This method cannot be called from the main thread.

| Parameters |
|---|---|
| `fragmentArgs: https://developer.android.com/reference/android/os/Bundle.html? = null` | a bundle to passed into fragment |
| `themeResId: @https://developer.android.com/reference/kotlin/androidx/annotation/StyleRes https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html = R.style.FragmentScenarioEmptyFragmentActivityTheme` | a style resource id to be set to the host activity's theme |
| `initialState: https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State = Lifecycle.State.RESUMED` | the initial `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State`. Passing in `https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle.State#DESTROYED` will result in an `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-argument-exception/index.html`. |
| `crossinline instantiate: () -> F` | method which will be used to instantiate the Fragment. This is a simplification of the `https://developer.android.com/reference/kotlin/androidx/fragment/app/FragmentFactory` interface for cases where only a single class needs a custom constructor called. |

## Extension functions

### FragmentScenario.withFragment

Artifact: [androidx.fragment:fragment-testing](https://developer.android.com/jetpack/androidx/releases/fragment) [View Source](https://cs.android.com/search?q=file:androidx/fragment/app/testing/FragmentScenario.kt+function:withFragment)

```
inline fun <F : Fragment, T : Any> FragmentScenario<F>.withFragment(
    crossinline block: F.() -> T
): T
```

Run `https://developer.android.com/reference/kotlin/androidx/fragment/app/testing/package-summary#(androidx.fragment.app.testing.FragmentScenario).withFragment(kotlin.Function1)` using `https://developer.android.com/reference/kotlin/androidx/fragment/app/testing/FragmentScenario#onFragment(androidx.fragment.app.testing.FragmentScenario.FragmentAction)`, returning the result of the `https://developer.android.com/reference/kotlin/androidx/fragment/app/testing/package-summary#(androidx.fragment.app.testing.FragmentScenario).withFragment(kotlin.Function1)`.

If any exceptions are raised while running `https://developer.android.com/reference/kotlin/androidx/fragment/app/testing/package-summary#(androidx.fragment.app.testing.FragmentScenario).withFragment(kotlin.Function1)`, they are rethrown.