# FragmentScenarioKt

Added in 1.1.0

*** ** * ** ***


```
public final class FragmentScenarioKt
```

<br />

*** ** * ** ***

## Summary

| ### Public methods |
|---|---|
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/fragment/app/testing/FragmentScenario<@https://developer.android.com/reference/androidx/annotation/NonNull F>` | `<F extends https://developer.android.com/reference/androidx/fragment/app/Fragment> https://developer.android.com/reference/androidx/fragment/app/testing/FragmentScenarioKt#launchFragment(android.os.Bundle,kotlin.Int,androidx.lifecycle.Lifecycle.State,androidx.fragment.app.FragmentFactory)( https://developer.android.com/reference/android/os/Bundle.html fragmentArgs, @https://developer.android.com/reference/androidx/annotation/StyleRes int themeResId, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State initialState, https://developer.android.com/reference/androidx/fragment/app/FragmentFactory factory )` Launches a Fragment with given arguments hosted by an empty `https://developer.android.com/reference/androidx/fragment/app/FragmentActivity` using given `https://developer.android.com/reference/androidx/fragment/app/FragmentFactory` and waits for it to reach `https://developer.android.com/reference/androidx/fragment/app/testing/package-summary#launchFragment(android.os.Bundle,kotlin.Int,androidx.lifecycle.Lifecycle.State,androidx.fragment.app.FragmentFactory)`. |
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/fragment/app/testing/FragmentScenario<@https://developer.android.com/reference/androidx/annotation/NonNull F>` | `<F extends https://developer.android.com/reference/androidx/fragment/app/Fragment> https://developer.android.com/reference/androidx/fragment/app/testing/FragmentScenarioKt#launchFragment(android.os.Bundle,kotlin.Int,androidx.lifecycle.Lifecycle.State,kotlin.Function0)( https://developer.android.com/reference/android/os/Bundle.html fragmentArgs, @https://developer.android.com/reference/androidx/annotation/StyleRes int themeResId, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State initialState, @https://developer.android.com/reference/androidx/annotation/NonNull Function0<@https://developer.android.com/reference/androidx/annotation/NonNull F> instantiate )` Launches a Fragment with given arguments hosted by an empty `https://developer.android.com/reference/androidx/fragment/app/FragmentActivity` using `https://developer.android.com/reference/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/androidx/fragment/app/testing/package-summary#launchFragment(android.os.Bundle,kotlin.Int,androidx.lifecycle.Lifecycle.State,kotlin.Function0)`. |
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/fragment/app/testing/FragmentScenario<@https://developer.android.com/reference/androidx/annotation/NonNull F>` | `<F extends https://developer.android.com/reference/androidx/fragment/app/Fragment> https://developer.android.com/reference/androidx/fragment/app/testing/FragmentScenarioKt#launchFragmentInContainer(android.os.Bundle,kotlin.Int,androidx.lifecycle.Lifecycle.State,androidx.fragment.app.FragmentFactory)( https://developer.android.com/reference/android/os/Bundle.html fragmentArgs, @https://developer.android.com/reference/androidx/annotation/StyleRes int themeResId, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State initialState, https://developer.android.com/reference/androidx/fragment/app/FragmentFactory factory )` 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/androidx/fragment/app/FragmentActivity` and waits for it to reach `https://developer.android.com/reference/androidx/fragment/app/testing/package-summary#launchFragmentInContainer(android.os.Bundle,kotlin.Int,androidx.lifecycle.Lifecycle.State,androidx.fragment.app.FragmentFactory)`. |
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/fragment/app/testing/FragmentScenario<@https://developer.android.com/reference/androidx/annotation/NonNull F>` | `<F extends https://developer.android.com/reference/androidx/fragment/app/Fragment> https://developer.android.com/reference/androidx/fragment/app/testing/FragmentScenarioKt#launchFragmentInContainer(android.os.Bundle,kotlin.Int,androidx.lifecycle.Lifecycle.State,kotlin.Function0)( https://developer.android.com/reference/android/os/Bundle.html fragmentArgs, @https://developer.android.com/reference/androidx/annotation/StyleRes int themeResId, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State initialState, @https://developer.android.com/reference/androidx/annotation/NonNull Function0<@https://developer.android.com/reference/androidx/annotation/NonNull F> instantiate )` 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/androidx/fragment/app/FragmentActivity` using `https://developer.android.com/reference/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/androidx/fragment/app/testing/package-summary#launchFragmentInContainer(android.os.Bundle,kotlin.Int,androidx.lifecycle.Lifecycle.State,kotlin.Function0)`. |
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull T` | `<F extends https://developer.android.com/reference/androidx/fragment/app/Fragment, T extends https://developer.android.com/reference/java/lang/Object.html> https://developer.android.com/reference/androidx/fragment/app/testing/FragmentScenarioKt#(androidx.fragment.app.testing.FragmentScenario).withFragment(kotlin.Function1)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/fragment/app/testing/FragmentScenario<@https://developer.android.com/reference/androidx/annotation/NonNull F> receiver, @https://developer.android.com/reference/androidx/annotation/NonNull Function1<@https://developer.android.com/reference/androidx/annotation/NonNull F, @https://developer.android.com/reference/androidx/annotation/NonNull T> block )` Run `https://developer.android.com/reference/androidx/fragment/app/testing/package-summary#(androidx.fragment.app.testing.FragmentScenario).withFragment(kotlin.Function1)` using `https://developer.android.com/reference/androidx/fragment/app/testing/FragmentScenario#onFragment(androidx.fragment.app.testing.FragmentScenario.FragmentAction)`, returning the result of the `https://developer.android.com/reference/androidx/fragment/app/testing/package-summary#(androidx.fragment.app.testing.FragmentScenario).withFragment(kotlin.Function1)`. |

## Public methods

### 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)

```
public static final @NonNull FragmentScenario<@NonNull F> <F extends Fragment> launchFragment(
    Bundle fragmentArgs,
    @StyleRes int themeResId,
    @NonNull Lifecycle.State initialState,
    FragmentFactory factory
)
```

Launches a Fragment with given arguments hosted by an empty `https://developer.android.com/reference/androidx/fragment/app/FragmentActivity` using given `https://developer.android.com/reference/androidx/fragment/app/FragmentFactory` and waits for it to reach `https://developer.android.com/reference/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 |
|---|---|
| `https://developer.android.com/reference/android/os/Bundle.html fragmentArgs` | a bundle to passed into fragment |
| `@https://developer.android.com/reference/androidx/annotation/StyleRes int themeResId` | a style resource id to be set to the host activity's theme |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State initialState` | the initial `https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State`. Passing in `https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State#DESTROYED` will result in an `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-argument-exception/index.html`. |
| `https://developer.android.com/reference/androidx/fragment/app/FragmentFactory factory` | 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)

```
public static final @NonNull FragmentScenario<@NonNull F> <F extends Fragment> launchFragment(
    Bundle fragmentArgs,
    @StyleRes int themeResId,
    @NonNull Lifecycle.State initialState,
    @NonNull Function0<@NonNull F> instantiate
)
```

Launches a Fragment with given arguments hosted by an empty `https://developer.android.com/reference/androidx/fragment/app/FragmentActivity` using `https://developer.android.com/reference/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/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 |
|---|---|
| `https://developer.android.com/reference/android/os/Bundle.html fragmentArgs` | a bundle to passed into fragment |
| `@https://developer.android.com/reference/androidx/annotation/StyleRes int themeResId` | a style resource id to be set to the host activity's theme |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State initialState` | the initial `https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State`. Passing in `https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State#DESTROYED` will result in an `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-argument-exception/index.html`. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull Function0<@https://developer.android.com/reference/androidx/annotation/NonNull F> instantiate` | 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)

```
public static final @NonNull FragmentScenario<@NonNull F> <F extends Fragment> launchFragmentInContainer(
    Bundle fragmentArgs,
    @StyleRes int themeResId,
    @NonNull Lifecycle.State initialState,
    FragmentFactory factory
)
```

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/androidx/fragment/app/FragmentActivity` and waits for it to reach `https://developer.android.com/reference/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 |
|---|---|
| `https://developer.android.com/reference/android/os/Bundle.html fragmentArgs` | a bundle to passed into fragment |
| `@https://developer.android.com/reference/androidx/annotation/StyleRes int themeResId` | a style resource id to be set to the host activity's theme |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State initialState` | the initial `https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State`. Passing in `https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State#DESTROYED` will result in an `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-argument-exception/index.html`. |
| `https://developer.android.com/reference/androidx/fragment/app/FragmentFactory factory` | 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)

```
public static final @NonNull FragmentScenario<@NonNull F> <F extends Fragment> launchFragmentInContainer(
    Bundle fragmentArgs,
    @StyleRes int themeResId,
    @NonNull Lifecycle.State initialState,
    @NonNull Function0<@NonNull F> instantiate
)
```

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

### 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)

```
public static final @NonNull T <F extends Fragment, T extends Object> withFragment(
    @NonNull FragmentScenario<@NonNull F> receiver,
    @NonNull Function1<@NonNull F, @NonNull T> block
)
```

Run `https://developer.android.com/reference/androidx/fragment/app/testing/package-summary#(androidx.fragment.app.testing.FragmentScenario).withFragment(kotlin.Function1)` using `https://developer.android.com/reference/androidx/fragment/app/testing/FragmentScenario#onFragment(androidx.fragment.app.testing.FragmentScenario.FragmentAction)`, returning the result of the `https://developer.android.com/reference/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/androidx/fragment/app/testing/package-summary#(androidx.fragment.app.testing.FragmentScenario).withFragment(kotlin.Function1)`, they are rethrown.