FragmentManager

public abstract class FragmentManager implements FragmentResultOwner


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

Summary

Nested types

FragmentManager.BackStackEntry

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

FragmentManager.FragmentLifecycleCallbacks

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

FragmentManager.OnBackStackChangedListener

Interface to watch for changes to the back stack.

Constants

static final @NonNull int

Flag for popBackStack and popBackStack: 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 methods

@NonNull void

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

@NonNull void

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

@NonNull FragmentTransaction

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

@NonNull void

Clears the back stack previously saved via saveBackStack.

final @Override @NonNull void

Clears the stored result for the given requestKey.

final @Override @NonNull void

Clears the stored FragmentResultListener for the given requestKey.

@NonNull void
dump(
    @NonNull String prefix,
    @Nullable FileDescriptor fd,
    @NonNull PrintWriter writer,
    @Nullable Array<@NonNull String> args
)

Print the FragmentManager's state into the given stream.

static @NonNull void
enableDebugLogging(@NonNull boolean enabled)

This method is deprecated.

FragmentManager now respects isLoggable for debug logging, allowing you to use adb shell setprop log.tag.FragmentManager VERBOSE.

@NonNull boolean

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

static @NonNull F
<F extends Fragment> findFragment(@NonNull View view)

Find a Fragment associated with the given View.

@Nullable 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.

@Nullable Fragment

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

@NonNull FragmentManager.BackStackEntry

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

@NonNull int

Return the number of entries currently in the back stack.

@Nullable Fragment

Retrieve the current Fragment instance for a reference previously placed with putFragment.

@NonNull FragmentFactory

Gets the current FragmentFactory used to instantiate new Fragment instances.

@NonNull List<@NonNull Fragment>

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

@Nullable Fragment

Return the currently active primary navigation fragment for this FragmentManager.

@Nullable FragmentStrictMode.Policy

Returns the current policy for this FragmentManager.

@NonNull boolean

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

@NonNull boolean

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

@NonNull void

Pop the top state off the back stack.

@NonNull void
popBackStack(@Nullable String name, @NonNull int flags)

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

@NonNull void
popBackStack(@NonNull int id, @NonNull int flags)

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

@NonNull boolean

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

@NonNull boolean

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

@NonNull boolean
popBackStackImmediate(@NonNull int id, @NonNull int flags)

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

@NonNull void
putFragment(
    @NonNull Bundle bundle,
    @NonNull String key,
    @NonNull Fragment fragment
)

Put a reference to a fragment in a Bundle.

@NonNull void

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

@NonNull void

Remove a FragmentOnAttachListener that was previously added via addFragmentOnAttachListener.

@NonNull void

Remove a listener that was previously added with addOnBackStackChangedListener.

@NonNull void

Restores the back stack previously saved via saveBackStack.

@NonNull void

Save the back stack.

@Nullable Fragment.SavedState

Save the current instance state of the given Fragment.

@NonNull void

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

final @Override @NonNull void
setFragmentResult(@NonNull String requestKey, @NonNull Bundle result)

Sets the given result for the requestKey.

final @Override @NonNull void
setFragmentResultListener(
    @NonNull String requestKey,
    @NonNull LifecycleOwner lifecycleOwner,
    @NonNull FragmentResultListener listener
)

Sets the FragmentResultListener for a given requestKey.

@NonNull void

Sets the policy for what actions should be detected, as well as the penalty if such actions occur.

@NonNull @Override String
@NonNull void

Unregisters a previously registered FragmentLifecycleCallbacks.

Extension functions

final void
FragmentManagerKt.commit(
    @NonNull FragmentManager receiver,
    boolean allowStateLoss,
    @NonNull Function1<@NonNull FragmentTransactionUnit> body
)

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

final void
FragmentManagerKt.commitNow(
    @NonNull FragmentManager receiver,
    boolean allowStateLoss,
    @NonNull Function1<@NonNull FragmentTransactionUnit> body
)

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

final void
FragmentManagerKt.transaction(
    @NonNull FragmentManager receiver,
    boolean now,
    boolean allowStateLoss,
    @NonNull Function1<@NonNull FragmentTransactionUnit> body
)

This method is deprecated. Use commit { ..

Constants

POP_BACK_STACK_INCLUSIVE

@NonNull
public static final @NonNull int POP_BACK_STACK_INCLUSIVE

Flag for popBackStack and popBackStack: 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.

Public methods

addFragmentOnAttachListener

@NonNull
public void addFragmentOnAttachListener(@NonNull FragmentOnAttachListener listener)

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

Parameters
@NonNull FragmentOnAttachListener listener

Listener to add

addOnBackStackChangedListener

@NonNull
public void addOnBackStackChangedListener(
    @NonNull FragmentManager.OnBackStackChangedListener listener
)

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

beginTransaction

@NonNull
public FragmentTransaction beginTransaction()

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.

clearBackStack

@NonNull
public void clearBackStack(@NonNull String name)

Clears the back stack previously saved via saveBackStack. This will result in all of the transactions that made up that back stack to be thrown away, thus destroying any fragments that were added through those transactions. All state of those fragments will be cleared as part of this process. If no state was previously saved with the given name, this operation does nothing.

This function is asynchronous -- it enqueues the request to clear, but the action will not be performed until the application returns to its event loop.

Parameters
@NonNull String name

The name of the back stack previously saved by saveBackStack.

clearFragmentResult