public class FragmentController

Provides integration points with a FragmentManager for a fragment host.

It is the responsibility of the host to take care of the Fragment's lifecycle. The methods provided by FragmentController are for that purpose.

Summary

Public methods

void

Attaches the host to the FragmentManager for this controller.

static @NonNull FragmentController

Returns a FragmentController.

void

Moves all Fragments managed by the controller's FragmentManager into the activity created state.

void

This method is deprecated.

Have your FragmentHostCallback implement androidx.core.content.OnConfigurationChangedProvider to automatically dispatch configuration changes to fragments.

boolean

Sends a context item selection event to the Fragments managed by the controller's FragmentManager.

void

Moves all Fragments managed by the controller's FragmentManager into the create state.

boolean

dispatchCreateOptionsMenu(
    @NonNull Menu menu,
    @NonNull MenuInflater inflater
)

This method is deprecated.

Have your FragmentHostCallback implement MenuHost to automatically dispatch menu changes to fragments.

void

Moves Fragments managed by the controller's FragmentManager into the destroy state.

void

Moves all Fragments managed by the controller's FragmentManager into the destroy view state.

void

This method is deprecated.

Have your FragmentHostCallback implement androidx.core.content.OnTrimMemoryProvider to automatically dispatch low memory callbacks to fragments.

void

dispatchMultiWindowModeChanged(boolean isInMultiWindowMode)

This method is deprecated.

Have your FragmentHostCallback implement androidx.core.app.OnMultiWindowModeChangedProvider to automatically dispatch multi-window mode changes to fragments.

boolean

This method is deprecated.

Have your FragmentHostCallback implement MenuHost to automatically dispatch menu changes to fragments.

void

This method is deprecated.

Have your FragmentHostCallback implement MenuHost to automatically dispatch menu changes to fragments.

void

Moves all Fragments managed by the controller's FragmentManager into the pause state.

void

dispatchPictureInPictureModeChanged(boolean isInPictureInPictureMode)

This method is deprecated.

Have your FragmentHostCallback implement androidx.core.app.OnPictureInPictureModeChangedProvider to automatically dispatch picture-in-picture mode changes to fragments.

boolean

This method is deprecated.

Have your FragmentHostCallback implement MenuHost to automatically dispatch menu changes to fragments.

void

This method is deprecated.

This functionality has been rolled into dispatchStop.

void

Moves all Fragments managed by the controller's FragmentManager into the resume state.

void

Moves all Fragments managed by the controller's FragmentManager into the start state.

void

Moves all Fragments managed by the controller's FragmentManager into the stop state.

void

This method is deprecated.

Loaders are managed separately from FragmentController

void

This method is deprecated.

Loaders are managed separately from FragmentController

void

This method is deprecated.

Loaders are managed separately from FragmentController

void

doLoaderStop(boolean retain)

This method is deprecated.

Loaders are managed separately from FragmentController

void

dumpLoaders(
    @NonNull String prefix,
    @Nullable FileDescriptor fd,
    @NonNull PrintWriter writer,
    @Nullable String[] args
)

This method is deprecated.

Loaders are managed separately from FragmentController

boolean

Execute any pending actions for the Fragments managed by the controller's FragmentManager.

@Nullable Fragment

Returns a fragment with the given identifier.

@NonNull List<Fragment>

Returns the list of active fragments.

int

Returns the number of active fragments.

@NonNull FragmentManager

Returns a FragmentManager for this controller.

LoaderManager

This method is deprecated.

Loaders are managed separately from FragmentController and this now throws an UnsupportedOperationException.

void

Marks the fragment state as unsaved.

@Nullable View

onCreateView(
    @Nullable View parent,
    @NonNull String name,
    @NonNull Context context,
    @NonNull AttributeSet attrs
)

Instantiates a Fragment's view.

void

This method is deprecated.

Loaders are managed separately from FragmentController

void

This method is deprecated.

Have your FragmentHostCallback implement ViewModelStoreOwner to automatically restore the Fragment's non configuration state and use restoreSaveState to restore the Fragment's save state.

void

restoreAllState(
    @Nullable Parcelable state,
    @Nullable List<Fragment> nonConfigList
)

This method is deprecated.

Have your FragmentHostCallback implement ViewModelStoreOwner to automatically restore the Fragment's non configuration state and use restoreSaveState to restore the Fragment's save state.

void

This method is deprecated.

Loaders are managed separately from FragmentController

void

This method is deprecated.

Have your FragmentHostCallback implement androidx.savedstate.SavedStateRegistryOwner to automatically restore the saved state via the FragmentManager.

@Nullable SimpleArrayMap<StringLoaderManager>

This method is deprecated.

Loaders are managed separately from FragmentController

@Nullable FragmentManagerNonConfig

This method is deprecated.

Have your FragmentHostCallback implement ViewModelStoreOwner to automatically retain the Fragment's non configuration state.

@Nullable List<Fragment>

This method is deprecated.

Have your FragmentHostCallback implement ViewModelStoreOwner to automatically retain the Fragment's non configuration state.

@Nullable Parcelable

This method is deprecated.

Have your FragmentHostCallback implement androidx.savedstate.SavedStateRegistryOwner to automatically save state via the FragmentManager.

Public methods

attachHost

public void attachHost(@Nullable Fragment parent)

Attaches the host to the FragmentManager for this controller. The host must be attached before the FragmentManager can be used to manage Fragments.

dispatchActivityCreated

public void dispatchActivityCreated()

Moves all Fragments managed by the controller's FragmentManager into the activity created state.

Call when Fragments should be informed their host has been created.

dispatchConfigurationChanged

public void dispatchConfigurationChanged(@NonNull Configuration newConfig)

Lets all Fragments managed by the controller's FragmentManager know a configuration change occurred.

Call when there is a configuration change.

dispatchContextItemSelected

public boolean dispatchContextItemSelected(@NonNull MenuItem item)

Sends a context item selection event to the Fragments managed by the controller's FragmentManager. Once the event has been consumed, no additional handling will be performed.

Call immediately after an options menu item has been selected

Returns
boolean

true if the context menu selection event was consumed

dispatchCreate

public void dispatchCreate()

Moves all Fragments managed by the controller's FragmentManager into the create state.

Call when Fragments should be created.

dispatchCreateOptionsMenu

public boolean dispatchCreateOptionsMenu(
    @NonNull Menu menu,
    @NonNull MenuInflater inflater
)

Lets all Fragments managed by the controller's FragmentManager know they should create an options menu.

Call when the Fragment should create an options menu.

Returns
boolean

true if the options menu contains items to display

dispatchDestroyView

public void dispatchDestroyView()

Moves all Fragments managed by the controller's FragmentManager into the destroy view state.

Call when the Fragment's views should be destroyed.

dispatchLowMemory

public void dispatchLowMemory()

Lets all Fragments managed by the controller's FragmentManager know the device is in a low memory condition.

Call when the device is low on memory and Fragment's should trim their memory usage.

dispatchMultiWindowModeChanged

public void dispatchMultiWindowModeChanged(boolean isInMultiWindowMode)

Lets all Fragments managed by the controller's FragmentManager know the multi-window mode of the activity changed.

Call when the multi-window mode of the activity changed.

dispatchOptionsItemSelected

public boolean dispatchOptionsItemSelected(@NonNull MenuItem item)

Sends an option item selection event to the Fragments managed by the controller's FragmentManager. Once the event has been consumed, no additional handling will be performed.

Call immediately after an options menu item has been selected

Returns
boolean

true if the options menu selection event was consumed

dispatchOptionsMenuClosed

public void dispatchOptionsMenuClosed(@NonNull Menu menu)

Lets all Fragments managed by the controller's FragmentManager know their options menu has closed.

Call immediately after closing the Fragment's options menu.

dispatchPause

public void dispatchPause()

Moves all Fragments managed by the controller's FragmentManager into the pause state.

Call when Fragments should be paused.

dispatchPictureInPictureModeChanged

public void dispatchPictureInPictureModeChanged(boolean isInPictureInPictureMode)

Lets all Fragments managed by the controller's FragmentManager know the picture-in-picture mode of the activity changed.

Call when the picture-in-picture mode of the activity changed.

dispatchPrepareOptionsMenu

public boolean dispatchPrepareOptionsMenu(@NonNull Menu menu)

Lets all Fragments managed by the controller's FragmentManager know they should prepare their options menu for display.

Call immediately before displaying the Fragment's options menu.

Returns
boolean

true if the options menu contains items to display

dispatchResume

public void dispatchResume()

Moves all Fragments managed by the controller's FragmentManager into the resume state.

Call when Fragments should be resumed.

dispatchStart

public void dispatchStart()

Moves all Fragments managed by the controller's FragmentManager into the start state.

Call when Fragments should be started.

dispatchStop

public void dispatchStop()

Moves all Fragments managed by the controller's FragmentManager into the stop state.

Call when Fragments should be stopped.

doLoaderDestroy

public void doLoaderDestroy()

Destroys the loaders and, if their state is not being retained, removes them.

doLoaderRetain

public void doLoaderRetain()

Retains the state of each of the loaders.

doLoaderStop

public void doLoaderStop(boolean retain)

Stops the loaders, optionally retaining their state. This is useful for keeping the loader state across configuration changes.

Parameters
boolean retain

When true, the loaders aren't stopped, but, their instances are retained in a started state

execPendingActions

public boolean execPendingActions()

Execute any pending actions for the Fragments managed by the controller's FragmentManager.

Call when queued actions can be performed [eg when the Fragment moves into a start or resume state].

Returns
boolean

true if queued actions were performed

noteStateNotSaved

public void noteStateNotSaved()

Marks the fragment state as unsaved. This allows for "state loss" detection.

reportLoaderStart

public void reportLoaderStart()

Lets the loaders know the host is ready to receive notifications.

Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.

Last updated 2025-05-15 UTC.