FragmentManager

abstract class FragmentManager
kotlin.Any
   ↳ androidx.fragment.app.FragmentManager

Static library support version of the framework's android.app.FragmentManager. Used to write apps that run on platforms prior to Android 3.0. When running on Android 3.0 or above, this implementation is still used; it does not try to switch to the framework's implementation. See the framework FragmentManager documentation for a class overview.

Your activity must derive from FragmentActivity to use this. From such an activity, you can acquire the FragmentManager by calling FragmentActivity#getSupportFragmentManager.

Summary

Nested classes
abstract

Representation of an entry on the fragment back stack, as created with FragmentTransaction.addToBackStack().

abstract

Callback interface for listening to fragment state changes that happen within a given FragmentManager.

abstract

Interface to watch for changes to the back stack.

Constants
static Int

Flag for #popBackStack(String, int) and #popBackStack(int, int): If set, and the name or ID of a back stack entry has been supplied, then all matching entries will be consumed until one that doesn't match is found or the bottom of the stack is reached.

Public constructors

Public methods
abstract Fragment.SavedState?

Save the current instance state of the given Fragment.

abstract Fragment?

Finds a fragment that was identified by the given id either when inflated from XML or as the container ID when added in a transaction.

abstract MutableList<Fragment!>

Get a list of all fragments that are currently added to the FragmentManager.

abstract FragmentTransaction

Start a series of edit operations on the Fragments associated with this FragmentManager.

abstract Unit
putFragment(bundle: Bundle, key: String, fragment: Fragment)

Put a reference to a fragment in a Bundle.

abstract Unit

Remove a listener that was previously added with #addOnBackStackChangedListener(OnBackStackChangedListener).

abstract Fragment?
getFragment(bundle: Bundle, key: String)

Retrieve the current Fragment instance for a reference previously placed with #putFragment(Bundle, String, Fragment).

abstract Unit

Unregisters a previously registered FragmentLifecycleCallbacks.

abstract Fragment?

Return the currently active primary navigation fragment for this FragmentManager.

abstract Int

Return the number of entries currently in the back stack.

abstract Boolean

Returns true if the final Activity.onDestroy() call has been made on the FragmentManager's Activity, so this instance is now dead.

abstract FragmentManager.BackStackEntry

Return the BackStackEntry at index index in the back stack; entries start index 0 being the bottom of the stack.

abstract Boolean

After a FragmentTransaction is committed with FragmentTransaction.commit(), it is scheduled to be executed asynchronously on the process's main thread.

abstract Boolean

Like #popBackStack(), but performs the operation immediately inside of the call.

abstract Boolean
popBackStackImmediate(name: String?, flags: Int)

Like #popBackStack(String, int), but performs the operation immediately inside of the call.

abstract Boolean

Like #popBackStack(int, int), but performs the operation immediately inside of the call.

abstract Fragment?

Finds a fragment that was identified by the given tag either when inflated from XML or as supplied when added in a transaction.

abstract Unit

Add a new listener for changes to the fragment back stack.

abstract Unit
dump(prefix: String!, fd: FileDescriptor!, writer: PrintWriter!, args: Array<String!>!)

Print the FragmentManager's state into the given stream.

abstract Boolean

Returns true if the FragmentManager's state has already been saved by its host.

abstract Unit

Pop the top state off the back stack.

abstract Unit
popBackStack(name: String?, flags: Int)

Pop the last fragment transition from the manager's fragment back stack.

abstract Unit
popBackStack(id: Int, flags: Int)

Pop all back stack states up to the one with the given identifier.

abstract Unit

Registers a FragmentLifecycleCallbacks to listen to fragment lifecycle events happening in this FragmentManager.

open static Unit

Control whether the framework's internal fragment manager debugging logs are turned on.

Extension functions
From androidx.fragment.app
Unit
FragmentManager.transaction(now: Boolean = false, allowStateLoss: Boolean = false, body: FragmentTransaction.() -> Unit)

Run body in a FragmentTransaction which is automatically committed if it completes without exception.

Constants

POP_BACK_STACK_INCLUSIVE

static val POP_BACK_STACK_INCLUSIVE: Int

Flag for #popBackStack(String, int) and #popBackStack(int, int): If set, and the name or ID of a back stack entry has been supplied, then all matching entries will be consumed until one that doesn't match is found or the bottom of the stack is reached. Otherwise, all entries up to but not including that entry will be removed.

Value: 1

Public constructors

<init>

FragmentManager()

Public methods

saveFragmentInstanceState

abstract fun saveFragmentInstanceState(f: Fragment!): Fragment.SavedState?

Save the current instance state of the given Fragment. This can be used later when creating a new instance of the Fragment and adding it to the fragment manager, to have it create itself to match the current state returned here. Note that there are limits on how this can be used:

  • The Fragment must currently be attached to the FragmentManager.
  • A new Fragment created using this saved state must be the same class type as the Fragment it was created from.
  • The saved state can not contain dependencies on other fragments -- that is it can't use #putFragment(Bundle, String, Fragment) to store a fragment reference because that reference may not be valid when this saved state is later used. Likewise the Fragment's target and result code are not included in this state.

Parameters
f Fragment!: The Fragment whose state is to be saved.
Return
Fragment.SavedState?: The generated state. This will be null if there was no interesting state created by the fragment.

findFragmentById

abstract fun findFragmentById(id: Int): Fragment?

Finds a fragment that was identified by the given id either when inflated from XML or as the container ID when added in a transaction. This first searches through fragments that are currently added to the manager's activity; if no such fragment is found, then all fragments currently on the back stack associated with this ID are searched.

Return
Fragment?: The fragment if found or null otherwise.

getFragments

abstract fun getFragments(): MutableList<Fragment!>

Get a list of all fragments that are currently added to the FragmentManager. This may include those that are hidden as well as those that are shown. This will not include any fragments only in the back stack, or fragments that are detached or removed.

The order of the fragments in the list is the order in which they were added or attached.

Return
MutableList<Fragment!>: A list of all fragments that are added to the FragmentManager.

beginTransaction

abstract fun beginTransaction(): FragmentTransaction

Start a series of edit operations on the Fragments associated with this FragmentManager.

Note: A fragment transaction can only be created/committed prior to an activity saving its state. If you try to commit a transaction after FragmentActivity.onSaveInstanceState() (and prior to a following FragmentActivity.onStart or FragmentActivity.onResume(), you will get an error. This is because the framework takes care of saving your current fragments in the state, and if changes are made after the state is saved then they will be lost.

putFragment

abstract fun putFragment(bundle: Bundle, key: String, fragment: Fragment): Unit

Put a reference to a fragment in a Bundle. This Bundle can be persisted as saved state, and when later restoring #getFragment(Bundle, String) will return the current instance of the same fragment.

Parameters
bundle Bundle: The bundle in which to put the fragment reference.
key Bundle: The name of the entry in the bundle.
fragment Bundle: The Fragment whose reference is to be stored.

removeOnBackStackChangedListener

abstract fun removeOnBackStackChangedListener(listener: FragmentManager.OnBackStackChangedListener): Unit

Remove a listener that was previously added with #addOnBackStackChangedListener(OnBackStackChangedListener).

getFragment

abstract fun getFragment(bundle: Bundle, key: String): Fragment?

Retrieve the current Fragment instance for a reference previously placed with #putFragment(Bundle, String, Fragment).

Parameters
bundle Bundle: The bundle from which to retrieve the fragment reference.
key Bundle: The name of the entry in the bundle.
Return
Fragment?: Returns the current Fragment instance that is associated with the given reference.

unregisterFragmentLifecycleCallbacks

abstract fun unregisterFragmentLifecycleCallbacks(cb: FragmentManager.FragmentLifecycleCallbacks): Unit

Unregisters a previously registered FragmentLifecycleCallbacks. If the callback was not previously registered this call has no effect. All registered callbacks will be automatically unregistered when this FragmentManager is destroyed.

Parameters
cb FragmentManager.FragmentLifecycleCallbacks: Callbacks to unregister

getPrimaryNavigationFragment

abstract fun getPrimaryNavigationFragment(): Fragment?

Return the currently active primary navigation fragment for this FragmentManager. The primary navigation fragment is set by fragment transactions using FragmentTransaction#setPrimaryNavigationFragment(Fragment).

The primary navigation fragment's child FragmentManager will be called first to process delegated navigation actions such as #popBackStack() if no ID or transaction name is provided to pop to.

Return
Fragment?: the fragment designated as the primary navigation fragment

getBackStackEntryCount

abstract fun getBackStackEntryCount(): Int

Return the number of entries currently in the back stack.

isDestroyed

abstract fun isDestroyed(): Boolean

Returns true if the final Activity.onDestroy() call has been made on the FragmentManager's Activity, so this instance is now dead.

getBackStackEntryAt

abstract fun getBackStackEntryAt(index: Int): FragmentManager.BackStackEntry

Return the BackStackEntry at index index in the back stack; entries start index 0 being the bottom of the stack.

executePendingTransactions

abstract fun executePendingTransactions(): Boolean

After a FragmentTransaction is committed with FragmentTransaction.commit(), it is scheduled to be executed asynchronously on the process's main thread. If you want to immediately executing any such pending operations, you can call this function (only from the main thread) to do so. Note that all callbacks and other related behavior will be done from within this call, so be careful about where this is called from.

If you are committing a single transaction that does not modify the fragment back stack, strongly consider using FragmentTransaction#commitNow() instead. This can help avoid unwanted side effects when other code in your app has pending committed transactions that expect different timing.

This also forces the start of any postponed Transactions where Fragment#postponeEnterTransition() has been called.

Return
Boolean: Returns true if there were any pending transactions to be executed.

popBackStackImmediate

abstract fun popBackStackImmediate(): Boolean

Like #popBackStack(), but performs the operation immediately inside of the call. This is like calling #executePendingTransactions() afterwards without forcing the start of postponed Transactions.

Return
Boolean: Returns true if there was something popped, else false.

popBackStackImmediate

abstract fun popBackStackImmediate(name: String?, flags: Int): Boolean

Like #popBackStack(String, int), but performs the operation immediately inside of the call. This is like calling #executePendingTransactions() afterwards without forcing the start of postponed Transactions.

Return
Boolean: Returns true if there was something popped, else false.

popBackStackImmediate

abstract fun popBackStackImmediate(id: Int, flags: Int): Boolean

Like #popBackStack(int, int), but performs the operation immediately inside of the call. This is like calling #executePendingTransactions() afterwards without forcing the start of postponed Transactions.

Return
Boolean: Returns true if there was something popped, else false.

findFragmentByTag

abstract fun findFragmentByTag(tag: String?): Fragment?

Finds a fragment that was identified by the given tag either when inflated from XML or as supplied when added in a transaction. This first searches through fragments that are currently added to the manager's activity; if no such fragment is found, then all fragments currently on the back stack are searched.

Return
Fragment?: The fragment if found or null otherwise.

addOnBackStackChangedListener

abstract fun addOnBackStackChangedListener(listener: FragmentManager.OnBackStackChangedListener): Unit

Add a new listener for changes to the fragment back stack.

dump

abstract fun dump(prefix: String!, fd: FileDescriptor!, writer: PrintWriter!, args: Array<String!>!): Unit

Print the FragmentManager's state into the given stream.

Parameters
prefix String!: Text to print at the front of each line.
fd String!: The raw file descriptor that the dump is being sent to.
writer String!: A PrintWriter to which the dump is to be set.
args String!: Additional arguments to the dump request.

isStateSaved

abstract fun isStateSaved(): Boolean

Returns true if the FragmentManager's state has already been saved by its host. Any operations that would change saved state should not be performed if this method returns true. For example, any popBackStack() method, such as #popBackStackImmediate() or any FragmentTransaction using FragmentTransaction#commit() instead of FragmentTransaction#commitAllowingStateLoss() will change the state and will result in an error.

Return
Boolean: true if this FragmentManager's state has already been saved by its host

popBackStack

abstract fun popBackStack(): Unit

Pop the top state off the back stack. Returns true if there was one to pop, else false. This function is asynchronous -- it enqueues the request to pop, but the action will not be performed until the application returns to its event loop.

popBackStack

abstract fun popBackStack(name: String?, flags: Int): Unit

Pop the last fragment transition from the manager's fragment back stack. If there is nothing to pop, false is returned. This function is asynchronous -- it enqueues the request to pop, but the action will not be performed until the application returns to its event loop.

Parameters
name String?: If non-null, this is the name of a previous back state to look for; if found, all states up to that state will be popped. The #POP_BACK_STACK_INCLUSIVE flag can be used to control whether the named state itself is popped. If null, only the top state is popped.
flags String?: Either 0 or #POP_BACK_STACK_INCLUSIVE.

popBackStack

abstract fun popBackStack(id: Int, flags: Int): Unit

Pop all back stack states up to the one with the given identifier. This function is asynchronous -- it enqueues the request to pop, but the action will not be performed until the application returns to its event loop.

Parameters
id Int: Identifier of the stated to be popped. If no identifier exists, false is returned. The identifier is the number returned by FragmentTransaction.commit(). The #POP_BACK_STACK_INCLUSIVE flag can be used to control whether the named state itself is popped.
flags Int: Either 0 or #POP_BACK_STACK_INCLUSIVE.

registerFragmentLifecycleCallbacks

abstract fun registerFragmentLifecycleCallbacks(cb: FragmentManager.FragmentLifecycleCallbacks, recursive: Boolean): Unit

Registers a FragmentLifecycleCallbacks to listen to fragment lifecycle events happening in this FragmentManager. All registered callbacks will be automatically unregistered when this FragmentManager is destroyed.

Parameters
cb FragmentManager.FragmentLifecycleCallbacks: Callbacks to register
recursive FragmentManager.FragmentLifecycleCallbacks: true to automatically register this callback for all child FragmentManagers

enableDebugLogging

open static fun enableDebugLogging(enabled: Boolean): Unit

Control whether the framework's internal fragment manager debugging logs are turned on. If enabled, you will see output in logcat as the framework performs fragment operations.