# FragmentStateAdapter

Artifact: [androidx.viewpager2:viewpager2](https://developer.android.com/jetpack/androidx/releases/viewpager2) [View Source](https://cs.android.com/search?q=file:androidx/viewpager2/adapter/FragmentStateAdapter.java+class:androidx.viewpager2.adapter.FragmentStateAdapter) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/viewpager2#1.0.0)

*** ** * ** ***


```
abstract class FragmentStateAdapter : RecyclerView.Adapter<FragmentViewHolder!>, StatefulAdapter
```

<br />

|---|---|---|
| [kotlin.Any](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html) |||
| ↳ | [androidx.recyclerview.widget.RecyclerView.Adapter](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Adapter)\<[FragmentViewHolder](https://developer.android.com/reference/kotlin/androidx/viewpager2/adapter/FragmentViewHolder)!\> ||
|   | ↳ | [androidx.viewpager2.adapter.FragmentStateAdapter](https://developer.android.com/reference/kotlin/androidx/viewpager2/adapter/FragmentStateAdapter) |

*** ** * ** ***

Similar in behavior to `https://developer.android.com/reference/kotlin/androidx/fragment/app/FragmentStatePagerAdapter`

Lifecycle within `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView`:

- `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.ViewHolder` initially an empty `https://developer.android.com/reference/android/widget/FrameLayout.html`, serves as a re-usable container for a `https://developer.android.com/reference/kotlin/androidx/fragment/app/Fragment` in later stages.
- `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Adapter#onBindViewHolder(VH,int)` we ask for a `https://developer.android.com/reference/kotlin/androidx/fragment/app/Fragment` for the position. If we already have the fragment, or have previously saved its state, we use those.
- `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView#onAttachedToWindow()` we attach the `https://developer.android.com/reference/kotlin/androidx/fragment/app/Fragment` to a container.
- `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Adapter#onViewRecycled(VH)` we remove, save state, destroy the `https://developer.android.com/reference/kotlin/androidx/fragment/app/Fragment`.

## Summary

| ### Nested types |
|---|
| `@https://developer.android.com/reference/kotlin/androidx/annotation/RequiresOptIn(level = https://developer.android.com/reference/kotlin/androidx/annotation/RequiresOptIn.Level#WARNING) annotation https://developer.android.com/reference/kotlin/androidx/viewpager2/adapter/FragmentStateAdapter.ExperimentalFragmentStateAdapterApi` |
| `abstract class https://developer.android.com/reference/kotlin/androidx/viewpager2/adapter/FragmentStateAdapter.FragmentTransactionCallback` Callback interface for listening to fragment lifecycle changes that happen inside the adapter. |
| `interface https://developer.android.com/reference/kotlin/androidx/viewpager2/adapter/FragmentStateAdapter.FragmentTransactionCallback.OnPostEventListener` Callback returned by `https://developer.android.com/reference/kotlin/androidx/viewpager2/adapter/FragmentStateAdapter.FragmentTransactionCallback#onFragmentPreAdded(androidx.fragment.app.Fragment)`, `https://developer.android.com/reference/kotlin/androidx/viewpager2/adapter/FragmentStateAdapter.FragmentTransactionCallback#onFragmentPreRemoved(androidx.fragment.app.Fragment)`, `https://developer.android.com/reference/kotlin/androidx/viewpager2/adapter/FragmentStateAdapter.FragmentTransactionCallback#onFragmentMaxLifecyclePreUpdated(androidx.fragment.app.Fragment,androidx.lifecycle.Lifecycle.State)` called after the operation ends. |

| ### Public constructors |
|---|
| `https://developer.android.com/reference/kotlin/androidx/viewpager2/adapter/FragmentStateAdapter#FragmentStateAdapter(androidx.fragment.app.FragmentActivity)(fragmentActivity: https://developer.android.com/reference/kotlin/androidx/fragment/app/FragmentActivity)` |
| `https://developer.android.com/reference/kotlin/androidx/viewpager2/adapter/FragmentStateAdapter#FragmentStateAdapter(androidx.fragment.app.Fragment)(fragment: https://developer.android.com/reference/kotlin/androidx/fragment/app/Fragment)` |
| `https://developer.android.com/reference/kotlin/androidx/viewpager2/adapter/FragmentStateAdapter#FragmentStateAdapter(androidx.fragment.app.FragmentManager,androidx.lifecycle.Lifecycle)( fragmentManager: https://developer.android.com/reference/kotlin/androidx/fragment/app/FragmentManager, lifecycle: https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle )` |

| ### Public functions |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/viewpager2/adapter/FragmentStateAdapter#containsItem(long)(itemId: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)` Default implementation works for collections that don't add, move, remove items. |
| `abstract https://developer.android.com/reference/kotlin/androidx/fragment/app/Fragment` | `https://developer.android.com/reference/kotlin/androidx/viewpager2/adapter/FragmentStateAdapter#createFragment(int)(position: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` Provide a new Fragment associated with the specified position. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | `https://developer.android.com/reference/kotlin/androidx/viewpager2/adapter/FragmentStateAdapter#getItemId(int)(position: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` Default implementation works for collections that don't add, move, remove items. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `@https://developer.android.com/reference/kotlin/androidx/annotation/CallSuper https://developer.android.com/reference/kotlin/androidx/viewpager2/adapter/FragmentStateAdapter#onAttachedToRecyclerView(androidx.recyclerview.widget.RecyclerView)(recyclerView: https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView)` Called by RecyclerView when it starts observing this Adapter. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/viewpager2/adapter/FragmentStateAdapter#onBindViewHolder(androidx.viewpager2.adapter.FragmentViewHolder,int)(holder: https://developer.android.com/reference/kotlin/androidx/viewpager2/adapter/FragmentViewHolder, position: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` Called by RecyclerView to display the data at the specified position. |
| `https://developer.android.com/reference/kotlin/androidx/viewpager2/adapter/FragmentViewHolder` | `https://developer.android.com/reference/kotlin/androidx/viewpager2/adapter/FragmentStateAdapter#onCreateViewHolder(android.view.ViewGroup,int)(parent: https://developer.android.com/reference/android/view/ViewGroup.html, viewType: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` Called when RecyclerView needs a new `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.ViewHolder` of the given type to represent an item. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `@https://developer.android.com/reference/kotlin/androidx/annotation/CallSuper https://developer.android.com/reference/kotlin/androidx/viewpager2/adapter/FragmentStateAdapter#onDetachedFromRecyclerView(androidx.recyclerview.widget.RecyclerView)(recyclerView: https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView)` Called by RecyclerView when it stops observing this Adapter. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/viewpager2/adapter/FragmentStateAdapter#onFailedToRecycleView(androidx.viewpager2.adapter.FragmentViewHolder)(holder: https://developer.android.com/reference/kotlin/androidx/viewpager2/adapter/FragmentViewHolder)` Called by the RecyclerView if a ViewHolder created by this Adapter cannot be recycled due to its transient state. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/viewpager2/adapter/FragmentStateAdapter#onViewAttachedToWindow(androidx.viewpager2.adapter.FragmentViewHolder)(holder: https://developer.android.com/reference/kotlin/androidx/viewpager2/adapter/FragmentViewHolder)` Called when a view created by this adapter has been attached to a window. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/viewpager2/adapter/FragmentStateAdapter#onViewRecycled(androidx.viewpager2.adapter.FragmentViewHolder)(holder: https://developer.android.com/reference/kotlin/androidx/viewpager2/adapter/FragmentViewHolder)` Called when a view created by this adapter has been recycled. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/viewpager2/adapter/FragmentStateAdapter#registerFragmentTransactionCallback(androidx.viewpager2.adapter.FragmentStateAdapter.FragmentTransactionCallback)( callback: https://developer.android.com/reference/kotlin/androidx/viewpager2/adapter/FragmentStateAdapter.FragmentTransactionCallback )` Registers a `https://developer.android.com/reference/kotlin/androidx/viewpager2/adapter/FragmentStateAdapter.FragmentTransactionCallback` to listen to fragment lifecycle changes that happen inside the adapter. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/viewpager2/adapter/FragmentStateAdapter#restoreState(android.os.Parcelable)(savedState: https://developer.android.com/reference/android/os/Parcelable.html)` Restores adapter state |
| `https://developer.android.com/reference/android/os/Parcelable.html` | `https://developer.android.com/reference/kotlin/androidx/viewpager2/adapter/FragmentStateAdapter#saveState()()` Saves adapter state |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/viewpager2/adapter/FragmentStateAdapter#setHasStableIds(boolean)(hasStableIds: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)` Indicates whether each item in the data set can be represented with a unique identifier of type `https://developer.android.com/reference/java/lang/Long.html`. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/viewpager2/adapter/FragmentStateAdapter#unregisterFragmentTransactionCallback(androidx.viewpager2.adapter.FragmentStateAdapter.FragmentTransactionCallback)( callback: https://developer.android.com/reference/kotlin/androidx/viewpager2/adapter/FragmentStateAdapter.FragmentTransactionCallback )` Unregisters a `https://developer.android.com/reference/kotlin/androidx/viewpager2/adapter/FragmentStateAdapter.FragmentTransactionCallback`. |

### Inherited functions

From [androidx.recyclerview.widget.RecyclerView.Adapter](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Adapter) `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Adapter#bindViewHolder(VH,int)(holder: VH, position: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`

This method internally calls `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Adapter#onBindViewHolder(VH,int)` to update the `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.ViewHolder` contents with the item at the given position and also sets up some private fields to be used by RecyclerView.
`VH` `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Adapter#createViewHolder(android.view.ViewGroup,int)(parent: https://developer.android.com/reference/android/view/ViewGroup.html, viewType: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`

This method calls `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Adapter#onCreateViewHolder(android.view.ViewGroup,int)` to create a new `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.ViewHolder` and initializes some private fields to be used by RecyclerView.
`https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Adapter#findRelativeAdapterPositionIn(androidx.recyclerview.widget.RecyclerView.Adapter<? extends androidx.recyclerview.widget.RecyclerView.ViewHolder>,androidx.recyclerview.widget.RecyclerView.ViewHolder,int)(
adapter: https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Adapter<https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.ViewHolder!>,
viewHolder: https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.ViewHolder,
localPosition: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html
)`

Returns the position of the given `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.ViewHolder` in the given `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Adapter`.
`abstract https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Adapter#getItemCount()()`

Returns the total number of items in the data set held by the adapter.
`https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Adapter#getItemViewType(int)(position: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`

Return the view type of the item at `position` for the purposes of view recycling.
`https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Adapter.StateRestorationPolicy` `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Adapter#getStateRestorationPolicy()()`

Returns when this Adapter wants to restore the state.
`https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Adapter#hasObservers()()`

Returns true if one or more observers are attached to this adapter.
`https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Adapter#hasStableIds()()`

Returns true if this adapter publishes a unique `long` value that can act as a key for the item at a given position in the data set.
`https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Adapter#notifyDataSetChanged()()`

Notify any registered observers that the data set has changed.
`https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Adapter#notifyItemChanged(int)(position: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`

Notify any registered observers that the item at `position` has changed.
`https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Adapter#notifyItemChanged(int,java.lang.Object)(position: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, payload: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?)`

Notify any registered observers that the item at `position` has changed with an optional payload object.
`https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Adapter#notifyItemInserted(int)(position: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`

Notify any registered observers that the item reflected at `position` has been newly inserted.
`https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Adapter#notifyItemMoved(int,int)(fromPosition: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, toPosition: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`

Notify any registered observers that the item reflected at `fromPosition` has been moved to `toPosition`.
`https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Adapter#notifyItemRangeChanged(int,int)(positionStart: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, itemCount: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`

Notify any registered observers that the `itemCount` items starting at position `positionStart` have changed.
`https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Adapter#notifyItemRangeChanged(int,int,java.lang.Object)(positionStart: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, itemCount: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, payload: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?)`

Notify any registered observers that the `itemCount` items starting at position `positionStart` have changed.
`https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Adapter#notifyItemRangeInserted(int,int)(positionStart: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, itemCount: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`

Notify any registered observers that the currently reflected `itemCount` items starting at `positionStart` have been newly inserted.
`https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Adapter#notifyItemRangeRemoved(int,int)(positionStart: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, itemCount: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`

Notify any registered observers that the `itemCount` items previously located at `positionStart` have been removed from the data set.
`https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Adapter#notifyItemRemoved(int)(position: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`

Notify any registered observers that the item previously located at `position` has been removed from the data set.
`https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Adapter#onBindViewHolder(VH,int,java.util.List<java.lang.Object>)(holder: VH, position: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, payloads: (https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-list/index.html)https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html!>)`

Called by RecyclerView to display the data at the specified position.
`https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Adapter#onViewDetachedFromWindow(VH)(holder: VH)`

Called when a view created by this adapter has been detached from its window.
`https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Adapter#registerAdapterDataObserver(androidx.recyclerview.widget.RecyclerView.AdapterDataObserver)(observer: https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.AdapterDataObserver)`

Register a new observer to listen for data changes.
`https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Adapter#setStateRestorationPolicy(androidx.recyclerview.widget.RecyclerView.Adapter.StateRestorationPolicy)(
strategy: https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Adapter.StateRestorationPolicy
)`

Sets the state restoration strategy for the Adapter.
`https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Adapter#unregisterAdapterDataObserver(androidx.recyclerview.widget.RecyclerView.AdapterDataObserver)(
observer: https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.AdapterDataObserver
)`

Unregister an observer currently listening for data changes.

## Public constructors

### FragmentStateAdapter

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/viewpager2#1.0.0)

```
FragmentStateAdapter(fragmentActivity: FragmentActivity)
```

| Parameters |
|---|---|
| `fragmentActivity: https://developer.android.com/reference/kotlin/androidx/fragment/app/FragmentActivity` | if the `https://developer.android.com/reference/kotlin/androidx/viewpager2/widget/ViewPager2` lives directly in a `https://developer.android.com/reference/kotlin/androidx/fragment/app/FragmentActivity` subclass. |

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/viewpager2/adapter/FragmentStateAdapter#FragmentStateAdapter(androidx.fragment.app.Fragment)` |   |
| `https://developer.android.com/reference/kotlin/androidx/viewpager2/adapter/FragmentStateAdapter#FragmentStateAdapter(androidx.fragment.app.FragmentManager,androidx.lifecycle.Lifecycle)` |   |

### FragmentStateAdapter

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/viewpager2#1.0.0)

```
FragmentStateAdapter(fragment: Fragment)
```

| Parameters |
|---|---|
| `fragment: https://developer.android.com/reference/kotlin/androidx/fragment/app/Fragment` | if the `https://developer.android.com/reference/kotlin/androidx/viewpager2/widget/ViewPager2` lives directly in a `https://developer.android.com/reference/kotlin/androidx/fragment/app/Fragment` subclass. |

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/viewpager2/adapter/FragmentStateAdapter#FragmentStateAdapter(androidx.fragment.app.FragmentActivity)` |   |
| `https://developer.android.com/reference/kotlin/androidx/viewpager2/adapter/FragmentStateAdapter#FragmentStateAdapter(androidx.fragment.app.FragmentManager,androidx.lifecycle.Lifecycle)` |   |

### FragmentStateAdapter

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/viewpager2#1.0.0)

```
FragmentStateAdapter(
    fragmentManager: FragmentManager,
    lifecycle: Lifecycle
)
```

| Parameters |
|---|---|
| `fragmentManager: https://developer.android.com/reference/kotlin/androidx/fragment/app/FragmentManager` | of `https://developer.android.com/reference/kotlin/androidx/viewpager2/widget/ViewPager2`'s host |
| `lifecycle: https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle` | of `https://developer.android.com/reference/kotlin/androidx/viewpager2/widget/ViewPager2`'s host |

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/viewpager2/adapter/FragmentStateAdapter#FragmentStateAdapter(androidx.fragment.app.FragmentActivity)` |   |
| `https://developer.android.com/reference/kotlin/androidx/viewpager2/adapter/FragmentStateAdapter#FragmentStateAdapter(androidx.fragment.app.Fragment)` |   |

## Public functions

### containsItem

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/viewpager2#1.0.0)

```
fun containsItem(itemId: Long): Boolean
```

Default implementation works for collections that don't add, move, remove items.

When overriding, also override `https://developer.android.com/reference/kotlin/androidx/viewpager2/adapter/FragmentStateAdapter#getItemId(int)`

### createFragment

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/viewpager2#1.0.0)

```
abstract fun createFragment(position: Int): Fragment
```

Provide a new Fragment associated with the specified position.

The adapter will be responsible for the Fragment lifecycle:

- The Fragment will be used to display an item.
- The Fragment will be destroyed when it gets too far from the viewport, and its state will be saved. When the item is close to the viewport again, a new Fragment will be requested, and a previously saved state will be used to initialize it.

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/viewpager2/widget/ViewPager2#setOffscreenPageLimit(int)` |   |

### getItemId

```
fun getItemId(position: Int): Long
```

Default implementation works for collections that don't add, move, remove items.

When overriding, also override `https://developer.android.com/reference/kotlin/androidx/viewpager2/adapter/FragmentStateAdapter#containsItem(long)`.

If the item is not a part of the collection, return `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView#NO_ID()`.

| Parameters |
|---|---|
| `position: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | Adapter position |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | stable item id `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Adapter#hasStableIds()` |

### onAttachedToRecyclerView

```
@CallSuper
fun onAttachedToRecyclerView(recyclerView: RecyclerView): Unit
```

Called by RecyclerView when it starts observing this Adapter.

Keep in mind that same adapter may be observed by multiple RecyclerViews.

| Parameters |
|---|---|
| `recyclerView: https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView` | The RecyclerView instance which started observing this adapter. |

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Adapter#onDetachedFromRecyclerView(androidx.recyclerview.widget.RecyclerView)` |   |

### onBindViewHolder

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/viewpager2#1.0.0)

```
fun onBindViewHolder(holder: FragmentViewHolder, position: Int): Unit
```

Called by RecyclerView to display the data at the specified position. This method should update the contents of the `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.ViewHolder#itemView()` to reflect the item at the given position.

Note that unlike `https://developer.android.com/reference/android/widget/ListView.html`, RecyclerView will not call this method again if the position of the item changes in the data set unless the item itself is invalidated or the new position cannot be determined. For this reason, you should only use the `position` parameter while acquiring the related data item inside this method and should not keep a copy of it. If you need the position of an item later on (e.g. in a click listener), use `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.ViewHolder#getBindingAdapterPosition()` which will have the updated adapter position. Override `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Adapter#onBindViewHolder(VH,int,java.util.List<java.lang.Object>)` instead if Adapter can handle efficient partial bind.

| Parameters |
|---|---|
| `holder: https://developer.android.com/reference/kotlin/androidx/viewpager2/adapter/FragmentViewHolder` | The ViewHolder which should be updated to represent the contents of the item at the given position in the data set. |
| `position: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | The position of the item within the adapter's data set. |

### onCreateViewHolder

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/viewpager2#1.0.0)

```
fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FragmentViewHolder
```

Called when RecyclerView needs a new `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.ViewHolder` of the given type to represent an item.

This new ViewHolder should be constructed with a new View that can represent the items of the given type. You can either create a new View manually or inflate it from an XML layout file.

The new ViewHolder will be used to display items of the adapter using `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Adapter#onBindViewHolder(VH,int,java.util.List<java.lang.Object>)`. Since it will be re-used to display different items in the data set, it is a good idea to cache references to sub views of the View to avoid unnecessary `https://developer.android.com/reference/android/view/View.html#findViewById(int)` calls.

| Parameters |
|---|---|
| `parent: https://developer.android.com/reference/android/view/ViewGroup.html` | The ViewGroup into which the new View will be added after it is bound to an adapter position. |
| `viewType: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | The view type of the new View. |

| Returns |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/viewpager2/adapter/FragmentViewHolder` | A new ViewHolder that holds a View of the given view type. |

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Adapter#getItemViewType(int)` |   |
| `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Adapter#onBindViewHolder(VH,int)` |   |

### onDetachedFromRecyclerView

```
@CallSuper
fun onDetachedFromRecyclerView(recyclerView: RecyclerView): Unit
```

Called by RecyclerView when it stops observing this Adapter.

| Parameters |
|---|---|
| `recyclerView: https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView` | The RecyclerView instance which stopped observing this adapter. |

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Adapter#onAttachedToRecyclerView(androidx.recyclerview.widget.RecyclerView)` |   |

### onFailedToRecycleView

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/viewpager2#1.0.0)

```
fun onFailedToRecycleView(holder: FragmentViewHolder): Boolean
```

Called by the RecyclerView if a ViewHolder created by this Adapter cannot be recycled due to its transient state. Upon receiving this callback, Adapter can clear the animation(s) that effect the View's transient state and return `true` so that the View can be recycled. Keep in mind that the View in question is already removed from the RecyclerView.

In some cases, it is acceptable to recycle a View although it has transient state. Most of the time, this is a case where the transient state will be cleared in `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Adapter#onBindViewHolder(VH,int)` call when View is rebound to a new position. For this reason, RecyclerView leaves the decision to the Adapter and uses the return value of this method to decide whether the View should be recycled or not.

Note that when all animations are created by `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.ItemAnimator`, you should never receive this callback because RecyclerView keeps those Views as children until their animations are complete. This callback is useful when children of the item views create animations which may not be easy to implement using an `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.ItemAnimator`.

You should *never* fix this issue by calling `holder.itemView.setHasTransientState(false);` unless you've previously called `holder.itemView.setHasTransientState(true);`. Each `View.setHasTransientState(true)` call must be matched by a `View.setHasTransientState(false)` call, otherwise, the state of the View may become inconsistent. You should always prefer to end or cancel animations that are triggering the transient state instead of handling it manually.

| Parameters |
|---|---|
| `holder: https://developer.android.com/reference/kotlin/androidx/viewpager2/adapter/FragmentViewHolder` | The ViewHolder containing the View that could not be recycled due to its transient state. |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | True if the View should be recycled, false otherwise. Note that if this method returns `true`, RecyclerView *will ignore* the transient state of the View and recycle it regardless. If this method returns `false`, RecyclerView will check the View's transient state again before giving a final decision. Default implementation returns false. |

### onViewAttachedToWindow

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/viewpager2#1.0.0)

```
fun onViewAttachedToWindow(holder: FragmentViewHolder): Unit
```

Called when a view created by this adapter has been attached to a window.

This can be used as a reasonable signal that the view is about to be seen by the user. If the adapter previously freed any resources in `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Adapter#onViewDetachedFromWindow(VH)` those resources should be restored here.

| Parameters |
|---|---|
| `holder: https://developer.android.com/reference/kotlin/androidx/viewpager2/adapter/FragmentViewHolder` | Holder of the view being attached |

### onViewRecycled

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/viewpager2#1.0.0)

```
fun onViewRecycled(holder: FragmentViewHolder): Unit
```

Called when a view created by this adapter has been recycled.

A view is recycled when a `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.LayoutManager` decides that it no longer needs to be attached to its parent `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView`. This can be because it has fallen out of visibility or a set of cached views represented by views still attached to the parent RecyclerView. If an item view has large or expensive data bound to it such as large bitmaps, this may be a good place to release those resources.

RecyclerView calls this method right before clearing ViewHolder's internal data and sending it to RecycledViewPool. This way, if ViewHolder was holding valid information before being recycled, you can call `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.ViewHolder#getBindingAdapterPosition()` to get its adapter position.

| Parameters |
|---|---|
| `holder: https://developer.android.com/reference/kotlin/androidx/viewpager2/adapter/FragmentViewHolder` | The ViewHolder for the view being recycled |

### registerFragmentTransactionCallback

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/viewpager2#1.1.0)

```
fun registerFragmentTransactionCallback(
    callback: FragmentStateAdapter.FragmentTransactionCallback
): Unit
```

Registers a `https://developer.android.com/reference/kotlin/androidx/viewpager2/adapter/FragmentStateAdapter.FragmentTransactionCallback` to listen to fragment lifecycle changes that happen inside the adapter.

| Parameters |
|---|---|
| `callback: https://developer.android.com/reference/kotlin/androidx/viewpager2/adapter/FragmentStateAdapter.FragmentTransactionCallback` | Callback to register |

### restoreState

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/viewpager2#1.1.0)

```
fun restoreState(savedState: Parcelable): Unit
```

Restores adapter state

### saveState

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/viewpager2#1.1.0)

```
fun saveState(): Parcelable
```

Saves adapter state

### setHasStableIds

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/viewpager2#1.0.0)

```
fun setHasStableIds(hasStableIds: Boolean): Unit
```

Indicates whether each item in the data set can be represented with a unique identifier of type `https://developer.android.com/reference/java/lang/Long.html`.

| Parameters |
|---|---|
| `hasStableIds: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | Whether items in data set have unique identifiers or not. |

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Adapter#hasStableIds()` |   |
| `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Adapter#getItemId(int)` |   |

### unregisterFragmentTransactionCallback

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/viewpager2#1.1.0)

```
fun unregisterFragmentTransactionCallback(
    callback: FragmentStateAdapter.FragmentTransactionCallback
): Unit
```

Unregisters a `https://developer.android.com/reference/kotlin/androidx/viewpager2/adapter/FragmentStateAdapter.FragmentTransactionCallback`.

| Parameters |
|---|---|
| `callback: https://developer.android.com/reference/kotlin/androidx/viewpager2/adapter/FragmentStateAdapter.FragmentTransactionCallback` | Callback to unregister |

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/viewpager2/adapter/FragmentStateAdapter#registerFragmentTransactionCallback(androidx.viewpager2.adapter.FragmentStateAdapter.FragmentTransactionCallback)` |   |