FragmentStatePagerAdapter

abstract class FragmentStatePagerAdapter : PagerAdapter
kotlin.Any
   ↳ androidx.viewpager.widget.PagerAdapter
   ↳ androidx.fragment.app.FragmentStatePagerAdapter

Implementation of PagerAdapter that uses a Fragment to manage each page. This class also handles saving and restoring of fragment's state.

This version of the pager is more useful when there are a large number of pages, working more like a list view. When pages are not visible to the user, their entire fragment may be destroyed, only keeping the saved state of that fragment. This allows the pager to hold on to much less memory associated with each visited page as compared to FragmentPagerAdapter at the cost of potentially more overhead when switching between pages.

When using FragmentPagerAdapter the host ViewPager must have a valid ID set.

Subclasses only need to implement getItem(int) and getCount() to have a working adapter.

Here is an example implementation of a pager containing fragments of lists:

The R.layout.fragment_pager resource of the top-level fragment is:

The R.layout.fragment_pager_list resource containing each individual fragment's layout is:

Summary

Constants

static Int

Indicates that only the current fragment will be in the Lifecycle.State#RESUMED state.

static Int

Indicates that Fragment#setUserVisibleHint(boolean) will be called when the current fragment changes.

Inherited constants

Public constructors

<init>(@NonNull fm: FragmentManager)

Constructor for FragmentStatePagerAdapter that sets the fragment manager for the adapter.

<init>(@NonNull fm: FragmentManager, behavior: Int)

Constructor for FragmentStatePagerAdapter.

Public methods

open Unit
destroyItem(@NonNull container: ViewGroup, position: Int, @NonNull object: Any)

open Unit
finishUpdate(@NonNull container: ViewGroup)

abstract Fragment
getItem(position: Int)

Return the Fragment associated with a specified position.

open Any
instantiateItem(@NonNull container: ViewGroup, position: Int)

open Boolean
isViewFromObject(@NonNull view: View, @NonNull object: Any)

open Unit
restoreState(@Nullable state: Parcelable?, @Nullable loader: ClassLoader?)

open Parcelable?

open Unit
setPrimaryItem(@NonNull container: ViewGroup, position: Int, @NonNull object: Any)

open Unit
startUpdate(@NonNull container: ViewGroup)

Inherited functions

Constants

BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT

static val BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT: Int

Indicates that only the current fragment will be in the Lifecycle.State#RESUMED state. All other Fragments are capped at Lifecycle.State#STARTED.

Value: 1

BEHAVIOR_SET_USER_VISIBLE_HINT

static val BEHAVIOR_SET_USER_VISIBLE_HINT: Int

Deprecated: This behavior relies on the deprecated Fragment#setUserVisibleHint(boolean) API. Use BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT to switch to its replacement, FragmentTransaction#setMaxLifecycle.

Indicates that Fragment#setUserVisibleHint(boolean) will be called when the current fragment changes.

Value: 0

Public constructors

<init>

FragmentStatePagerAdapter(@NonNull fm: FragmentManager)

Deprecated: use FragmentStatePagerAdapter(FragmentManager, int) with BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT

Constructor for FragmentStatePagerAdapter that sets the fragment manager for the adapter. This is the equivalent of calling FragmentStatePagerAdapter(FragmentManager, int) and passing in BEHAVIOR_SET_USER_VISIBLE_HINT.

Fragments will have Fragment#setUserVisibleHint(boolean) called whenever the current Fragment changes.

Parameters
fm FragmentManager: fragment manager that will interact with this adapter

<init>

FragmentStatePagerAdapter(@NonNull fm: FragmentManager, behavior: Int)

Constructor for FragmentStatePagerAdapter. If BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT is passed in, then only the current Fragment is in the Lifecycle.State#RESUMED state, while all other fragments are capped at Lifecycle.State#STARTED. If BEHAVIOR_SET_USER_VISIBLE_HINT is passed, all fragments are in the Lifecycle.State#RESUMED state and there will be callbacks to Fragment#setUserVisibleHint(boolean).

Parameters
fm FragmentManager: fragment manager that will interact with this adapter
behavior FragmentManager: determines if only current fragments are in a resumed state

Public methods

destroyItem

open fun destroyItem(@NonNull container: ViewGroup, position: Int, @NonNull object: Any): Unit

finishUpdate

open fun finishUpdate(@NonNull container: ViewGroup): Unit

getItem

@NonNull abstract fun getItem(position: Int): Fragment

Return the Fragment associated with a specified position.

instantiateItem

@NonNull open fun instantiateItem(@NonNull container: ViewGroup, position: Int): Any

isViewFromObject

open fun isViewFromObject(@NonNull view: View, @NonNull object: Any): Boolean

restoreState

open fun restoreState(@Nullable state: Parcelable?, @Nullable loader: ClassLoader?): Unit

saveState

@Nullable open fun saveState(): Parcelable?

setPrimaryItem

open fun setPrimaryItem(@NonNull container: ViewGroup, position: Int, @NonNull object: Any): Unit

startUpdate

open fun startUpdate(@NonNull container: ViewGroup): Unit