Save the date! Android Dev Summit is coming to Sunnyvale, CA on Oct 23-24, 2019.

FragmentController

open class FragmentController
kotlin.Any
   ↳ androidx.fragment.app.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
open Unit

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

open Unit

Starts the loaders.

open Unit

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

open Fragment?
findFragmentByWho(@NonNull who: String)

Returns a fragment with the given identifier.

open Boolean

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

open Unit

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

open MutableList<Fragment!>

Returns the list of active fragments.

open Parcelable?

Saves the state for all Fragments.

open Unit

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

open MutableList<Fragment!>?

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

open Unit

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

open Unit
dispatchPictureInPictureModeChanged(isInPictureInPictureMode: Boolean)

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

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

Dumps the current state of the loaders.

open Unit

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

open Unit

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

open Boolean

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

open Unit

Stops the loaders, optionally retaining their state.

open Unit

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

open Unit

Restores the saved state for all LoaderManagers.

open Boolean

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

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

Instantiates a Fragment's view.

open Int

Returns the number of active fragments.

open Unit

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

open SimpleArrayMap<String!, LoaderManager!>?

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

open Unit

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

open Unit

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

open Unit

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

open LoaderManager!

Returns a LoaderManager.

open Unit
restoreSaveState(@Nullable state: Parcelable?)

Restores the saved state for all Fragments.

open FragmentManagerNonConfig?

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

open Unit

Retains the state of each of the loaders.

open Boolean
dispatchCreateOptionsMenu(@NonNull menu: Menu, @NonNull inflater: MenuInflater)

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

open Unit

open Unit

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

open Unit
restoreAllState(@Nullable state: Parcelable?, @Nullable nonConfigList: MutableList<Fragment!>?)

Restores the saved state for all Fragments.

open Unit
restoreAllState(@Nullable state: Parcelable?, @Nullable nonConfig: FragmentManagerNonConfig?)

Restores the saved state for all Fragments.

open FragmentManager

Returns a FragmentManager for this controller.

open Unit
attachHost(@Nullable parent: Fragment?)

Attaches the host to the FragmentManager for this controller.

open Unit

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

open Unit

Marks the fragment state as unsaved.

open Boolean

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

open static FragmentController
createController(@NonNull callbacks: FragmentHostCallback<*>)

Returns a FragmentController.

Public methods

dispatchCreate

open fun dispatchCreate(): Unit

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

Call when Fragments should be created.

See Also

  • Fragment#onCreate(Bundle)

doLoaderStart

open fun doLoaderStart(): Unit

Starts the loaders.

dispatchMultiWindowModeChanged

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

findFragmentByWho

@Nullable open fun findFragmentByWho(@NonNull who: String): Fragment?

Returns a fragment with the given identifier.

dispatchOptionsItemSelected

open fun dispatchOptionsItemSelected(@NonNull 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

Return
Boolean: true if the options menu selection event was consumed

dispatchStop

open fun dispatchStop(): Unit

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

Call when Fragments should be stopped.

getActiveFragments

@NonNull open fun getActiveFragments(actives: MutableList<Fragment!>!): MutableList<Fragment!>

Returns the list of active fragments.

saveAllState

@Nullable open fun saveAllState(): Parcelable?

Saves the state for all Fragments.

dispatchActivityCreated

open 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

  • Fragment#onActivityCreated(Bundle)

retainNonConfig

@Nullable open fun retainNonConfig(): MutableList<Fragment!>?

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

dispatchOptionsMenuClosed

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

dispatchPictureInPictureModeChanged

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

dumpLoaders

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

Dumps the current state of the loaders.

dispatchStart

open fun dispatchStart(): Unit

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

Call when Fragments should be started.

dispatchConfigurationChanged

open fun dispatchConfigurationChanged(@NonNull newConfig: Configuration): Unit

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

Call when there is a configuration change.

execPendingActions

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

Return
Boolean: true if queued actions were performed

doLoaderStop

open 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 true, the loaders aren't stopped, but, their instances are retained in a started state

dispatchResume

open fun dispatchResume(): Unit

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

Call when Fragments should be resumed.

restoreLoaderNonConfig

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

dispatchPrepareOptionsMenu

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

Return
Boolean: true if the options menu contains items to display

onCreateView

@Nullable open fun onCreateView(@Nullable parent: View?, @NonNull name: String, @NonNull context: Context, @NonNull 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 View?: Tag name to be inflated.
context View?: The context the view is being created in.
attrs View?: Inflation attributes as specified in XML file.
Return
View?: view the newly created view

getActiveFragmentsCount

open fun getActiveFragmentsCount(): Int

Returns the number of active fragments.

dispatchLowMemory

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

retainLoaderNonConfig

@Nullable open fun retainLoaderNonConfig(): SimpleArrayMap<String!, LoaderManager!>?

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

dispatchPause

open fun dispatchPause(): Unit

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

Call when Fragments should be paused.

dispatchDestroyView

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

dispatchDestroy

open 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 androidx.lifecycle.ViewModelStore#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.

getSupportLoaderManager

open fun getSupportLoaderManager(): LoaderManager!

Returns a LoaderManager.

restoreSaveState

open fun restoreSaveState(@Nullable state: Parcelable?): Unit

Restores the saved state for all Fragments.

Parameters
state Parcelable?: the saved state containing the Parcelable returned by saveAllState()

See Also

retainNestedNonConfig

@Nullable open fun retainNestedNonConfig(): FragmentManagerNonConfig?

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

doLoaderRetain

open fun doLoaderRetain(): Unit

Retains the state of each of the loaders.

dispatchCreateOptionsMenu

open fun dispatchCreateOptionsMenu(@NonNull menu: Menu, @NonNull 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.

Return
Boolean: true if the options menu contains items to display

dispatchReallyStop

open fun dispatchReallyStop(): Unit

reportLoaderStart

open fun reportLoaderStart(): Unit

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

restoreAllState

open fun restoreAllState(@Nullable state: Parcelable?, @Nullable nonConfigList: MutableList<Fragment!>?): Unit

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

restoreAllState

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

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

getSupportFragmentManager

@NonNull open fun getSupportFragmentManager(): FragmentManager

Returns a FragmentManager for this controller.

attachHost

open fun attachHost(@Nullable 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.

doLoaderDestroy

open fun doLoaderDestroy(): Unit

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

noteStateNotSaved

open fun noteStateNotSaved(): Unit

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

dispatchContextItemSelected

open fun dispatchContextItemSelected(@NonNull 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

Return
Boolean: true if the context menu selection event was consumed

createController

@NonNull open static fun createController(@NonNull callbacks: FragmentHostCallback<*>): FragmentController

Returns a FragmentController.