{% setvar book_path %}/reference/kotlin/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/_kotlin_switcher2.md" %}

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 functions

Unit
attachHost(parent: Fragment?)

Attaches the host to the FragmentManager for this controller.

FragmentController

Returns a FragmentController.

Unit

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

Unit
dispatchConfigurationChanged(newConfig: Configuration)

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

Boolean

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

Unit

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

Boolean
dispatchCreateOptionsMenu(menu: Menu, inflater: MenuInflater)

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

Unit

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

Unit

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

Unit

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

Unit

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

Boolean

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

Unit

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

Unit

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

Unit
dispatchPictureInPictureModeChanged(isInPictureInPictureMode: Boolean)

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

Boolean

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

Unit

This function is deprecated.

This functionality has been rolled into dispatchStop.

Unit

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

Unit

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

Unit

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

Unit

This function is deprecated.

Loaders are managed separately from FragmentController

Unit

This function is deprecated.

Loaders are managed separately from FragmentController

Unit

This function is deprecated.

Loaders are managed separately from FragmentController

Unit

This function is deprecated.

Loaders are managed separately from FragmentController

Unit
dumpLoaders(
    prefix: String,
    fd: FileDescriptor?,
    writer: PrintWriter,
    args: Array<String>?
)

This function is deprecated.

Loaders are managed separately from FragmentController

Boolean

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

Fragment?

Returns a fragment with the given identifier.

List<Fragment>

Returns the list of active fragments.

Int

Returns the number of active fragments.

FragmentManager

Returns a FragmentManager for this controller.

LoaderManager

This function is deprecated.

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

Unit

Marks the fragment state as unsaved.

View?
onCreateView(
    parent: View?,
    name: String,
    context: Context,
    attrs: AttributeSet
)

Instantiates a Fragment's view.

Unit

This function is deprecated.

Loaders are managed separately from FragmentController

Unit
restoreAllState(state: Parcelable?, nonConfigList: List<Fragment>?)

This function 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.

Unit
restoreAllState(state: Parcelable?, nonConfig: FragmentManagerNonConfig?)

This function 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.

Unit
restoreLoaderNonConfig(
    loaderManagers: SimpleArrayMap<String, LoaderManager>
)

This function is deprecated.

Loaders are managed separately from FragmentController

Unit
restoreSaveState(state: Parcelable?)

Restores the saved state for all Fragments.

SimpleArrayMap<String, LoaderManager>?

This function is deprecated.

Loaders are managed separately from FragmentController

FragmentManagerNonConfig?

This function is deprecated.

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

List<Fragment>?

This function is deprecated.

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

Parcelable?

Saves the state for all Fragments.

Public functions

attachHost

fun attachHost(parent: Fragment?): Unit

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

createController

fun createController(callbacks: FragmentHostCallback<Any>): FragmentController

Returns a FragmentController.

dispatchActivityCreated

fun dispatchActivityCreated(): Unit

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

fun dispatchConfigurationChanged(newConfig: Configuration): Unit

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

fun dispatchContextItemSelected(item: MenuItem): Boolean

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

fun dispatchCreate(): Unit

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

fun dispatchCreateOptionsMenu(menu: Menu, inflater: MenuInflater): Boolean

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

fun dispatchDestroy(): Unit

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

fun dispatchDestroyView(): Unit

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

fun dispatchLowMemory(): Unit

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

fun dispatchMultiWindowModeChanged(isInMultiWindowMode: Boolean): Unit

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

fun dispatchOptionsItemSelected(item: MenuItem): Boolean

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

fun dispatchOptionsMenuClosed(menu: Menu): Unit

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

fun dispatchPause(): Unit

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

fun dispatchPictureInPictureModeChanged(isInPictureInPictureMode: Boolean): Unit

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

fun dispatchPrepareOptionsMenu(menu: Menu): Boolean

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

fun dispatchReallyStop(): Unit

dispatchResume

fun dispatchResume(): Unit

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

fun dispatchStart(): Unit

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

fun dispatchStop(): Unit

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

fun doLoaderDestroy(): Unit

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

doLoaderRetain

fun doLoaderRetain(): Unit

Retains the state of each of the loaders.

doLoaderStart

fun doLoaderStart(): Unit

Starts the loaders.

doLoaderStop

fun doLoaderStop(retain: Boolean): Unit

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

Parameters
retain: Boolean

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

dumpLoaders

fun dumpLoaders(
    prefix: String,
    fd: FileDescriptor?,
    writer: PrintWriter,
    args: Array<String>?
): Unit

Dumps the current state of the loaders.

execPendingActions

fun execPendingActions(): Boolean

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

fun findFragmentByWho(who: String): Fragment?

Returns a fragment with the given identifier.

getActiveFragments

fun getActiveFragments(actives: List<Fragment>): List<Fragment>

Returns the list of active fragments.

getActiveFragmentsCount

fun getActiveFragmentsCount(): Int

Returns the number of active fragments.

getSupportFragmentManager

fun getSupportFragmentManager(): FragmentManager

Returns a FragmentManager for this controller.

getSupportLoaderManager

fun getSupportLoaderManager(): LoaderManager

Returns a LoaderManager.

See also
LoaderManager

androidx.loader.app.LoaderManager#getInstance

noteStateNotSaved

fun noteStateNotSaved(): Unit

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

onCreateView

fun onCreateView(
    parent: View?,
    name: String,
    context: Context,
    attrs: AttributeSet
): View?

Instantiates a Fragment's view.

Parameters
parent: View?

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

name: String

Tag name to be inflated.

context: Context

The context the view is being created in.

attrs: AttributeSet

Inflation attributes as specified in XML file.

Returns
View?

view the newly created view

reportLoaderStart

fun reportLoaderStart(): Unit

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

restoreAllState

fun restoreAllState(state: Parcelable?, nonConfigList: List<Fragment>?): Unit

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

restoreAllState

fun restoreAllState(state: Parcelable?, nonConfig: FragmentManagerNonConfig?): Unit

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

restoreLoaderNonConfig

fun restoreLoaderNonConfig(
    loaderManagers: SimpleArrayMap<String, LoaderManager>
): Unit

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

See also
retainLoaderNonConfig

#retainLoaderNonConfig()

restoreSaveState

fun restoreSaveState(state: Parcelable?): Unit

Restores the saved state for all Fragments.

Parameters
state: Parcelable?

the saved state containing the Parcelable returned by saveAllState

See also
saveAllState

#saveAllState()

retainLoaderNonConfig

fun retainLoaderNonConfig(): SimpleArrayMap<String, LoaderManager>?

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

retainNestedNonConfig

fun retainNestedNonConfig(): FragmentManagerNonConfig?

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

retainNonConfig

fun retainNonConfig(): List<Fragment>?

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

saveAllState

fun saveAllState(): Parcelable?

Saves the state for all Fragments.

See also
restoreSaveState

#restoreSaveState(Parcelable)