{% setvar book_path %}/reference/androidx/_book.yaml{% endsetvar %} {% include "_shared/_reference-head-tags.html" %}

FragmentController

{% setvar page_path %}androidx/fragment/app/FragmentController.html{% endsetvar %} {% setvar can_switch %}1{% endsetvar %} {% include "reference/_java_switcher2.md" %}

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

@NonNull void

Attaches the host to the FragmentManager for this controller.

static @NonNull FragmentController

Returns a FragmentController.

@NonNull void

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

@NonNull void
dispatchConfigurationChanged(@NonNull Configuration newConfig)

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

@NonNull boolean

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

@NonNull void

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

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

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

@NonNull void

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

@NonNull void

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

@NonNull void

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

@NonNull void
dispatchMultiWindowModeChanged(@NonNull boolean isInMultiWindowMode)

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

@NonNull boolean

Sends an option item selection event to the Fragments managed by the controller's FragmentManager.

@NonNull void

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

@NonNull void

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

@NonNull void
dispatchPictureInPictureModeChanged(
    @NonNull boolean isInPictureInPictureMode
)

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

@NonNull boolean

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

@NonNull void

This method is deprecated.

This functionality has been rolled into dispatchStop.

@NonNull void

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

@NonNull void

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

@NonNull void

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

@NonNull void

This method is deprecated.

Loaders are managed separately from FragmentController

@NonNull void

This method is deprecated.

Loaders are managed separately from FragmentController

@NonNull void

This method is deprecated.

Loaders are managed separately from FragmentController

@NonNull void
doLoaderStop(@NonNull boolean retain)

This method is deprecated.

Loaders are managed separately from FragmentController

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

This method is deprecated.

Loaders are managed separately from FragmentController

@NonNull 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<@NonNull Fragment>

Returns the list of active fragments.

@NonNull int

Returns the number of active fragments.

@NonNull FragmentManager

Returns a FragmentManager for this controller.

@NonNull LoaderManager

This method is deprecated.

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

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

@NonNull void

This method is deprecated.

Loaders are managed separately from FragmentController

@NonNull void
restoreAllState(
    @Nullable Parcelable state,
    @Nullable List<@NonNull 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.

@NonNull void
restoreAllState(
    @Nullable Parcelable state,
    @Nullable FragmentManagerNonConfig nonConfig
)

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.

@NonNull void
restoreLoaderNonConfig(
    @NonNull SimpleArrayMap<String, LoaderManager> loaderManagers
)

This method is deprecated.

Loaders are managed separately from FragmentController

@NonNull void
restoreSaveState(@Nullable Parcelable state)

Restores the saved state for all Fragments.

@Nullable SimpleArrayMap<String, LoaderManager>

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<@NonNull Fragment>

This method is deprecated.

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

@Nullable Parcelable

Saves the state for all Fragments.

Public methods

attachHost

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

createController

@NonNull
public static FragmentController createController(@NonNull FragmentHostCallback<@NonNull Object> callbacks)

Returns a FragmentController.

dispatchActivityCreated

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

See also
onActivityCreated

Fragment#onActivityCreated(Bundle)

dispatchConfigurationChanged

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

See also
onConfigurationChanged

androidx.fragment.app.Fragment#onConfigurationChanged(Configuration)

dispatchContextItemSelected

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

{@code true} if the context menu selection event was consumed

See also
onContextItemSelected

androidx.fragment.app.Fragment#onContextItemSelected(MenuItem)

dispatchCreate

@NonNull
public void dispatchCreate()

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

Call when Fragments should be created.

See also
onCreate

Fragment#onCreate(Bundle)

dispatchCreateOptionsMenu

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

{@code true} if the options menu contains items to display

See also
onCreateOptionsMenu

androidx.fragment.app.Fragment#onCreateOptionsMenu(Menu, MenuInflater)

dispatchDestroy

@NonNull
public void dispatchDestroy()

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

If the androidx.fragment.app.FragmentHostCallback is an instance of ViewModelStoreOwner, then retained Fragments and any other non configuration state such as any androidx.lifecycle.ViewModel attached to Fragments will only be destroyed if clear is called prior to this method.

Otherwise, the FragmentManager will look to see if the host's Context is an Activity and if Activity#isChangingConfigurations() returns true. In only that case will non configuration state be retained.

Call when Fragments should be destroyed.

See also
onDestroy

androidx.fragment.app.Fragment#onDestroy()

dispatchDestroyView

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

See also
onDestroyView

androidx.fragment.app.Fragment#onDestroyView()

dispatchLowMemory

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

See also
onLowMemory

androidx.fragment.app.Fragment#onLowMemory()

dispatchMultiWindowModeChanged

@NonNull
public void dispatchMultiWindowModeChanged(@NonNull 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.

See also
onMultiWindowModeChanged

androidx.fragment.app.Fragment#onMultiWindowModeChanged

dispatchOptionsItemSelected

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

{@code true} if the options menu selection event was consumed

See also
onOptionsItemSelected

androidx.fragment.app.Fragment#onOptionsItemSelected(MenuItem)

dispatchOptionsMenuClosed

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

See also
onOptionsMenuClosed

androidx.fragment.app.Fragment#onOptionsMenuClosed(Menu)

dispatchPause

@NonNull
public void dispatchPause()

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

Call when Fragments should be paused.

See also
onPause

androidx.fragment.app.Fragment#onPause()

dispatchPictureInPictureModeChanged

@NonNull
public void dispatchPictureInPictureModeChanged(
    @NonNull 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.

See also
onPictureInPictureModeChanged

androidx.fragment.app.Fragment#onPictureInPictureModeChanged

dispatchPrepareOptionsMenu

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

{@code true} if the options menu contains items to display

See also
onPrepareOptionsMenu

androidx.fragment.app.Fragment#onPrepareOptionsMenu(Menu)

dispatchReallyStop

@NonNull
public void dispatchReallyStop()

dispatchResume

@NonNull
public void dispatchResume()

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

Call when Fragments should be resumed.

See also
onResume

androidx.fragment.app.Fragment#onResume()

dispatchStart

@NonNull
public void dispatchStart()

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

Call when Fragments should be started.

See also
onStart

androidx.fragment.app.Fragment#onStart()

dispatchStop

@NonNull
public void dispatchStop()

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

Call when Fragments should be stopped.

See also
onStop

androidx.fragment.app.Fragment#onStop()

doLoaderDestroy

@NonNull
public void doLoaderDestroy()

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

doLoaderRetain

@NonNull
public void doLoaderRetain()

Retains the state of each of the loaders.

doLoaderStart

@NonNull
public void doLoaderStart()

Starts the loaders.

doLoaderStop

@NonNull
public void doLoaderStop(@NonNull boolean retain)

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

Parameters
@NonNull boolean retain

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

dumpLoaders

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

Dumps the current state of the loaders.

execPendingActions

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

{@code true} if queued actions were performed

findFragmentByWho

@Nullable
@NonNull
public Fragment findFragmentByWho(@NonNull String who)

Returns a fragment with the given identifier.

getActiveFragments

@NonNull
public List<@NonNull FragmentgetActiveFragments(@NonNull List<@NonNull Fragment> actives)

Returns the list of active fragments.

getActiveFragmentsCount

@NonNull
public int getActiveFragmentsCount()

Returns the number of active fragments.

getSupportFragmentManager

@NonNull
public FragmentManager getSupportFragmentManager()

Returns a FragmentManager for this controller.

getSupportLoaderManager

@NonNull
public LoaderManager getSupportLoaderManager()

Returns a LoaderManager.

See also
LoaderManager

androidx.loader.app.LoaderManager#getInstance

noteStateNotSaved

@NonNull
public void noteStateNotSaved()

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

onCreateView

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

Instantiates a Fragment's view.

Parameters
@Nullable View parent

The parent that the created view will be placed in; note that this may be null.

@NonNull String name

Tag name to be inflated.

@NonNull Context context

The context the view is being created in.

@NonNull AttributeSet attrs

Inflation attributes as specified in XML file.

Returns
View

view the newly created view

reportLoaderStart

@NonNull
public void reportLoaderStart()

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

restoreAllState

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

Restores the saved state for all Fragments. The given Fragment list are Fragment instances retained across configuration changes.

restoreAllState

@NonNull
public void restoreAllState(
    @Nullable Parcelable state,
    @Nullable FragmentManagerNonConfig nonConfig
)

Restores the saved state for all Fragments. The given FragmentManagerNonConfig are Fragment instances retained across configuration changes, including nested fragments

restoreLoaderNonConfig

@NonNull
public void restoreLoaderNonConfig(
    @NonNull SimpleArrayMap<String, LoaderManager> loaderManagers
)

Restores the saved state for all LoaderManagers. The given LoaderManager list are LoaderManager instances retained across configuration changes.

See also
retainLoaderNonConfig

#retainLoaderNonConfig()

restoreSaveState

@NonNull
public void restoreSaveState(@Nullable Parcelable state)

Restores the saved state for all Fragments.

Parameters
@Nullable Parcelable state

the saved state containing the Parcelable returned by saveAllState

See also
saveAllState

#saveAllState()

retainLoaderNonConfig

@Nullable
@NonNull
public SimpleArrayMap<String, LoaderManager> retainLoaderNonConfig()

Returns a list of LoaderManagers that have opted to retain their instance across configuration changes.

retainNestedNonConfig

@Nullable
@NonNull
public FragmentManagerNonConfig retainNestedNonConfig()

Returns a nested tree of Fragments that have opted to retain their instance across configuration changes.

retainNonConfig

@Nullable
@NonNull
public List<@NonNull FragmentretainNonConfig()

Returns a list of Fragments that have opted to retain their instance across configuration changes.

saveAllState

@Nullable
@NonNull
public Parcelable saveAllState()

Saves the state for all Fragments.

See also
restoreSaveState

#restoreSaveState(Parcelable)