Google is committed to advancing racial equity for Black communities. See how.

FragmentManager

abstract class FragmentManager : FragmentResultOwner
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

Static library support version of the framework's android.app.FragmentManager.

Public methods
open Unit

Add a FragmentOnAttachListener that should receive a call to FragmentOnAttachListener#onAttachFragment(FragmentManager, Fragment) when a new Fragment is attached to this FragmentManager.

open Unit

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

open FragmentTransaction

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

Unit
clearFragmentResult(@NonNull requestKey: String)

Unit
clearFragmentResultListener(@NonNull requestKey: String)

open Unit
dump(@NonNull prefix: String, @Nullable fd: FileDescriptor?, @NonNull writer: PrintWriter, @Nullable args: Array<String!>?)

Print the FragmentManager's state into the given stream.

open static Unit

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

open static Unit

Control whether FragmentManager uses the new state manager that is responsible for:

  • Moving Fragments through their lifecycle methods
  • Running animations and transitions
  • Handling postponed transactions
This must only be changed before any fragment transactions are done (i.e., in your

open Boolean

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

open static F
findFragment(@NonNull view: View)

Find a Fragment associated with the given View.

open Fragment?
findFragmentById(@IdRes id: Int)

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.

open Fragment?
findFragmentByTag(@Nullable tag: String?)

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

open FragmentManager.BackStackEntry

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

open Int

Return the number of entries currently in the back stack.

open Fragment?
getFragment(@NonNull bundle: Bundle, @NonNull key: String)

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

open FragmentFactory

Gets the current FragmentFactory used to instantiate new Fragment instances.

open MutableList<Fragment!>

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

open Fragment?

Return the currently active primary navigation fragment for this FragmentManager.

open Boolean

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

open Boolean

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

open Unit

Pop the top state off the back stack.

open Unit
popBackStack(@Nullable name: String?, flags: Int)

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

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

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

open Boolean

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

open Boolean
popBackStackImmediate(@Nullable name: String?, flags: Int)

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

open Boolean

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

open Unit
putFragment(@NonNull bundle: Bundle, @NonNull key: String, @NonNull fragment: Fragment)

Put a reference to a fragment in a Bundle.

open Unit

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

open Unit

Remove a FragmentOnAttachListener that was previously added via addFragmentOnAttachListener(FragmentOnAttachListener).

open Unit

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

open Fragment.SavedState?
saveFragmentInstanceState(@NonNull fragment: Fragment)

Save the current instance state of the given Fragment.

open Unit
setFragmentFactory(@NonNull fragmentFactory: FragmentFactory)

Set a FragmentFactory for this FragmentManager that will be used to create new Fragment instances from this point onward.

Unit
setFragmentResult(@NonNull requestKey: String, @NonNull result: Bundle)

Unit
setFragmentResultListener(@NonNull requestKey: String, @NonNull lifecycleOwner: LifecycleOwner, @NonNull listener: FragmentResultListener)

open String

open Unit

Unregisters a previously registered FragmentLifecycleCallbacks.

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

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

Unit
FragmentManager.commitNow(allowStateLoss: Boolean = false, body: FragmentTransaction.() -> Unit)

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

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

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.

Public methods

addFragmentOnAttachListener

open fun addFragmentOnAttachListener(@NonNull listener: FragmentOnAttachListener): Unit

Add a FragmentOnAttachListener that should receive a call to FragmentOnAttachListener#onAttachFragment(FragmentManager, Fragment) when a new Fragment is attached to this FragmentManager.

Parameters
listener FragmentOnAttachListener: Listener to add

addOnBackStackChangedListener

open fun addOnBackStackChangedListener(@NonNull listener: FragmentManager.OnBackStackChangedListener): Unit

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

beginTransaction

@NonNull open 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.

clearFragmentResult

fun clearFragmentResult(@NonNull requestKey: String): Unit

clearFragmentResultListener

fun clearFragmentResultListener(@NonNull requestKey: String): Unit

dump

open fun dump(
    @NonNull prefix: String,
    @Nullable fd: FileDescriptor?,
    @NonNull writer: PrintWriter,
    @Nullable 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 FileDescriptor?: The raw file descriptor that the dump is being sent to.
writer PrintWriter: A PrintWriter to which the dump is to be set.
args Array<String!>?: Additional arguments to the dump request.

enableDebugLogging

open static fun enableDebugLogging(enabled: Boolean): Unit

Deprecated: FragmentManager now respects Log#isLoggable(String, int) for debug logging, allowing you to use adb shell setprop log.tag.FragmentManager VERBOSE.

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.

enableNewStateManager

open static fun enableNewStateManager(enabled: Boolean): Unit

Control whether FragmentManager uses the new state manager that is responsible for:

  • Moving Fragments through their lifecycle methods
  • Running animations and transitions
  • Handling postponed transactions
This must only be changed before any fragment transactions are done (i.e., in your Application class or prior to super.onCreate() in every activity with the same value for all activities). Changing it after that point is not supported and can result in fragments not moving to their expected state.

This is enabled by default. Disabling it should only be used in cases where you are debugging a potential regression and as part of filing an issue to verify and fix the regression.

Parameters
enabled Boolean: Whether the new state manager should be enabled.

executePendingTransactions

open 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.

findFragment

@NonNull open static fun <F : Fragment!> findFragment(@NonNull view: View): F

Find a Fragment associated with the given View. This method will locate the Fragment associated with this view. This is automatically populated for the View returned by Fragment#onCreateView and its children.

Parameters
view View: the view to search from
Return
F the locally scoped Fragment to the given view