Fragment

Static library support version of the framework's android.app.Fragment. 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 android.app.Fragment documentation for a class overview.

The main differences when using this support version instead of the framework version are:

Inheritors

Constructors

Link copied to clipboard
constructor()
Constructor used by the default FragmentFactory.
constructor(@LayoutRes contentLayoutId: Int)
Alternate constructor that can be called from your default, no argument constructor to provide a default layout that will be inflated by onCreateView.

Types

Link copied to clipboard
Thrown by instantiate when there is an instantiation failure.
Link copied to clipboard
open class SavedState : Parcelable
State information that has been retrieved from a fragment instance through FragmentManager.saveFragmentInstanceState.
Link copied to clipboard
Callback interface for monitoring SESL fragment transition lifecycle events.

Properties

Link copied to clipboard
open var mPreviousWho: @Nullable String

Functions

Link copied to clipboard
inline fun <VM : ViewModel> Fragment.activityViewModels(noinline extrasProducer: () -> CreationExtras? = null, noinline factoryProducer: () -> ViewModelProvider.Factory? = null): Lazy<VM>

Returns a property delegate to access parent activity's ViewModel, if factoryProducer is specified then ViewModelProvider.Factory returned by it will be used to create ViewModel first time. Otherwise, the activity's default factory will be used.

Link copied to clipboard
open fun addOnContextAvailableListener(@NonNull listener: @NonNull OnContextAvailableListener)
Link copied to clipboard

Clears the stored result for the given requestKey.

Link copied to clipboard

Clears the stored FragmentResultListener for the given requestKey.

Link copied to clipboard
fun <VM : ViewModel> Fragment.createViewModelLazy(viewModelClass: KClass<VM>, storeProducer: () -> ViewModelStore, extrasProducer: () -> CreationExtras = { defaultViewModelCreationExtras }, factoryProducer: () -> ViewModelProvider.Factory? = null): Lazy<VM>

Helper method for creation of ViewModelLazy, that resolves null passed as factoryProducer to default factory.

Link copied to clipboard
open fun dump(@NonNull prefix: @NonNull String, @Nullable fd: @Nullable FileDescriptor, @NonNull writer: @NonNull PrintWriter, @Nullable args: Array<@Nullable String>)
Print the Fragments's state into the given stream.
Link copied to clipboard
fun equals(@Nullable o: @Nullable Any): Boolean
Subclasses can not override equals().
Link copied to clipboard
@Nullable
fun getActivity(): @Nullable FragmentActivity
Return the FragmentActivity this fragment is currently associated with.
Link copied to clipboard
Returns whether the the exit transition and enter transition overlap or not.
Link copied to clipboard
Returns whether the the return transition and reenter transition overlap or not.
Link copied to clipboard
@Nullable
fun getArguments(): @Nullable Bundle
Return the arguments supplied when the fragment was instantiated, if any.
Link copied to clipboard
@NonNull
fun getChildFragmentManager(): @NonNull FragmentManager
Return a private FragmentManager for placing and managing Fragments inside of this Fragment.
Link copied to clipboard
@Nullable
open fun getContext(): @Nullable Context
Return the Context this fragment is currently associated with.
Link copied to clipboard
The Fragment's arguments when this is first called will be used as the defaults to any androidx.lifecycle.SavedStateHandle passed to a view model created using this extra.
Link copied to clipboard
@Nullable
open fun getEnterTransition(): @Nullable Any
Returns the Transition that will be used to move Views into the initial scene.
Link copied to clipboard
@Nullable
open fun getExitTransition(): @Nullable Any
Returns the Transition that will be used to move Views out of the scene when the fragment is removed, hidden, or detached when not popping the back stack.
Link copied to clipboard
@Nullable
fun getFragmentManager(): @Nullable FragmentManager
Return the FragmentManager for interacting with fragments associated with this fragment's activity.
Link copied to clipboard
@Nullable
fun getHost(): @Nullable Any
Return the host object of this fragment.
Link copied to clipboard
fun getId(): Int
Return the identifier this fragment is known by.
Link copied to clipboard
@NonNull
fun getLayoutInflater(): @NonNull LayoutInflater
Returns the cached LayoutInflater used to inflate Views of this Fragment.
open fun getLayoutInflater(@Nullable savedFragmentState: @Nullable Bundle): @NonNull LayoutInflater
Override onGetLayoutInflater when you need to change the LayoutInflater or call getLayoutInflater when you want to retrieve the current LayoutInflater.
Link copied to clipboard
@NonNull
open fun getLifecycle(): @NonNull Lifecycle
Overriding this method is no longer supported and this method will be made final in a future version of Fragment.
Link copied to clipboard
@NonNull
open fun getLoaderManager(): @NonNull LoaderManager
Return the LoaderManager for this fragment.
Link copied to clipboard
@Nullable
fun getParentFragment(): @Nullable Fragment
Returns the parent Fragment containing this Fragment.
Link copied to clipboard
@NonNull
fun getParentFragmentManager(): @NonNull FragmentManager
Return the FragmentManager for interacting with fragments associated with this fragment's activity.
Link copied to clipboard
open fun getProgress(progress: Float): Float
Maps and clamps gesture progress for predictive back fragment transitions.
Link copied to clipboard
@Nullable
open fun getReenterTransition(): @Nullable Any
Returns the Transition that will be used to move Views in to the scene when returning due to popping a back stack.
Link copied to clipboard
@NonNull
fun getResources(): @NonNull Resources
Return requireActivity().getResources().
Link copied to clipboard
Returns true if this fragment instance's state will be retained across configuration changes, and false if it will not.
Link copied to clipboard
@Nullable
open fun getReturnTransition(): @Nullable Any
Returns the Transition that will be used to move Views out of the scene when the Fragment is preparing to be removed, hidden, or detached because of popping the back stack.
Link copied to clipboard
@NonNull
fun getSavedStateRegistry(): @NonNull SavedStateRegistry
Link copied to clipboard
@Nullable
open fun getSharedElementEnterTransition(): @Nullable Any
Returns the Transition that will be used for shared elements transferred into the content Scene.
Link copied to clipboard
@Nullable
open fun getSharedElementReturnTransition(): @Nullable Any
Return the Transition that will be used for shared elements transferred back during a pop of the back stack.
Link copied to clipboard
@NonNull
fun getString(@StringRes resId: Int): @NonNull String
Return a localized string from the application's package's default string table.
@NonNull
fun getString(@StringRes resId: Int, @Nullable formatArgs: Array<@Nullable Any>): @NonNull String
Return a localized formatted string from the application's package's default string table, substituting the format arguments as defined in java.util.Formatter and format.
Link copied to clipboard
@Nullable
fun getTag(): @Nullable String
Get the tag name of the fragment, if specified.
Link copied to clipboard
@Nullable
fun getTargetFragment(): @Nullable Fragment
Return the target fragment set by setTargetFragment.
Link copied to clipboard
Return the target request code set by setTargetFragment.
Link copied to clipboard
@NonNull
fun getText(@StringRes resId: Int): @NonNull CharSequence
Return a localized, styled CharSequence from the application's package's default string table.
Link copied to clipboard
Link copied to clipboard
@Nullable
open fun getView(): @Nullable View
Get the root view for the fragment's layout (the one returned by onCreateView), if provided.
Link copied to clipboard
@NonNull
open fun getViewLifecycleOwner(): @NonNull LifecycleOwner
Get a LifecycleOwner that represents the Fragment's View lifecycle.
Link copied to clipboard
Retrieve a LiveData which allows you to observe the lifecycle of the Fragment's View.
Link copied to clipboard
@NonNull
open fun getViewModelStore(): @NonNull ViewModelStore
Returns the ViewModelStore associated with this Fragment Overriding this method is no longer supported and this method will be made final in a future version of Fragment.
Link copied to clipboard
fun hashCode(): Int
Subclasses can not override hashCode().
Link copied to clipboard
Link copied to clipboard
open fun initTransition()
Resets view translation properties at the start of a fragment transition.
Link copied to clipboard
@NonNull
open fun instantiate(@NonNull context: @NonNull Context, @NonNull fname: @NonNull String): @NonNull Fragment
Like instantiate but with a null argument Bundle.
@NonNull
open fun instantiate(@NonNull context: @NonNull Context, @NonNull fname: @NonNull String, @Nullable args: @Nullable Bundle): @NonNull Fragment
Create a new instance of a Fragment with the given class name.
Link copied to clipboard
Return true if the fragment is currently added to its activity.
Link copied to clipboard
Return true if the fragment has been explicitly detached from the UI.
Link copied to clipboard
Return true if the fragment has been hidden.
Link copied to clipboard
Return true if the layout is included as part of an activity view hierarchy via the tag.
Link copied to clipboard
Link copied to clipboard
Return true if this fragment is currently being removed from its activity.
Link copied to clipboard
Return true if the fragment is in the resumed state.
Link copied to clipboard
Returns true if this fragment is added and its state has already been saved by its host.
Link copied to clipboard
Return true if the fragment is currently visible to the user.
Link copied to clipboard
open fun onActivityCreated(@Nullable savedInstanceState: @Nullable Bundle)
Called when the fragment's activity has been created and this fragment's view hierarchy instantiated.
Link copied to clipboard
open fun onActivityResult(requestCode: Int, resultCode: Int, @Nullable data: @Nullable Intent)
Receive the result from a previous call to startActivityForResult.
Link copied to clipboard
open fun onAttach(@NonNull activity: @NonNull Activity)
Called when a fragment is first attached to its activity.
open fun onAttach(@NonNull context: @NonNull Context)
Called when a fragment is first attached to its context.
Link copied to clipboard
open fun onAttachFragment(@NonNull childFragment: @NonNull Fragment)
Called when a fragment is attached as a child of this fragment.
Link copied to clipboard
open fun onConfigurationChanged(@NonNull newConfig: @NonNull Configuration)
Link copied to clipboard
open fun onContextItemSelected(@NonNull item: @NonNull MenuItem): Boolean
This hook is called whenever an item in a context menu is selected.
Link copied to clipboard
open fun onCreate(@Nullable savedInstanceState: @Nullable Bundle)
Called to do initial creation of a fragment.
Link copied to clipboard
@Nullable
open fun onCreateAnimation(transit: Int, enter: Boolean, nextAnim: Int): @Nullable Animation
Called when a fragment loads an animation.
Link copied to clipboard
@Nullable
open fun onCreateAnimator(nextAnim: Int, enter: Boolean, isPop: Boolean): @Nullable Animator
Creates an animator for SESL fragment transitions, accounting for predictive back and pop-over window states.
@Nullable
open fun onCreateAnimator(transit: Int, enter: Boolean, nextAnim: Int): @Nullable Animator
Called when a fragment loads an animator.
Link copied to clipboard
open fun onCreateContextMenu(@NonNull menu: @NonNull ContextMenu, @NonNull v: @NonNull View, @Nullable menuInfo: @Nullable ContextMenu.ContextMenuInfo)
Called when a context menu for the view is about to be shown.
Link copied to clipboard
open fun onCreateOptionsMenu(@NonNull menu: @NonNull Menu, @NonNull inflater: @NonNull MenuInflater)
Initialize the contents of the Fragment host's standard options menu.
Link copied to clipboard
@Nullable
open fun onCreateView(@NonNull inflater: @NonNull LayoutInflater, @Nullable container: @Nullable ViewGroup, @Nullable savedInstanceState: @Nullable Bundle): @Nullable View
Called to have the fragment instantiate its user interface view.
Link copied to clipboard
Called when the fragment is no longer in use.
Link copied to clipboard
Called when this fragment's option menu items are no longer being included in the overall options menu.
Link copied to clipboard
Called when the view previously created by onCreateView has been detached from the fragment.
Link copied to clipboard
Called when the fragment is no longer attached to its activity.
Link copied to clipboard
@NonNull
open fun onGetLayoutInflater(@Nullable savedInstanceState: @Nullable Bundle): @NonNull LayoutInflater
Returns the LayoutInflater used to inflate Views of this Fragment.
Link copied to clipboard
Called when the hidden state (as returned by isHidden of the fragment or another fragment in its hierarchy has changed.
Link copied to clipboard
open fun onInflate(@NonNull activity: @NonNull Activity, @NonNull attrs: @NonNull AttributeSet, @Nullable savedInstanceState: @Nullable Bundle)
open fun onInflate(@NonNull context: @NonNull Context, @NonNull attrs: @NonNull AttributeSet, @Nullable savedInstanceState: @Nullable Bundle)
Called when a fragment is being created as part of a view layout inflation, typically from setting the content view of an activity.
Link copied to clipboard
Link copied to clipboard
open fun onMultiWindowModeChanged(isInMultiWindowMode: Boolean)
Called when the Fragment's activity changes from fullscreen mode to multi-window mode and visa-versa.
Link copied to clipboard
open fun onOptionsItemSelected(@NonNull item: @NonNull MenuItem): Boolean
This hook is called whenever an item in your options menu is selected.
Link copied to clipboard
open fun onOptionsMenuClosed(@NonNull menu: @NonNull Menu)
This hook is called whenever the options menu is being closed (either by the user canceling the menu with the back/menu button, or when an item is selected).
Link copied to clipboard
Called when the Fragment is no longer resumed.
Link copied to clipboard
open fun onPictureInPictureModeChanged(isInPictureInPictureMode: Boolean)
Called by the system when the activity changes to and from picture-in-picture mode.
Link copied to clipboard
open fun onPrepareOptionsMenu(@NonNull menu: @NonNull Menu)
Prepare the Fragment host's standard options menu to be displayed.
Link copied to clipboard
open fun onPrimaryNavigationFragmentChanged(isPrimaryNavigationFragment: Boolean)
Callback for when the primary navigation state of this Fragment has changed.
Link copied to clipboard
open fun onRequestPermissionsResult(requestCode: Int, @NonNull permissions: Array<@NonNull String>, grantResults: @NonNull Array<Int>)
Callback for the result from requesting permissions.
Link copied to clipboard
Called when the fragment is visible to the user and actively running.
Link copied to clipboard
open fun onSaveInstanceState(@NonNull outState: @NonNull Bundle)
Called to ask the fragment to save its current dynamic state, so it can later be reconstructed in a new instance if its process is restarted.
Link copied to clipboard
Called when the Fragment is visible to the user.
Link copied to clipboard
Called when the Fragment is no longer started.
Link copied to clipboard
open fun onViewCreated(@NonNull view: @NonNull View, @Nullable savedInstanceState: @Nullable Bundle)
Called immediately after onCreateView has returned, but before any saved state has been restored in to the view.
Link copied to clipboard
open fun onViewStateRestored(@Nullable savedInstanceState: @Nullable Bundle)
Called when all saved state has been restored into the view hierarchy of the fragment.
Link copied to clipboard
@Nullable
open fun peekAvailableContext(): @Nullable Context
Link copied to clipboard
Postpone the entering Fragment transition until startPostponedEnterTransition or executePendingTransactions has been called.
fun postponeEnterTransition(duration: Long, @NonNull timeUnit: @NonNull TimeUnit)
Postpone the entering Fragment transition for a given amount of time and then call startPostponedEnterTransition.
Link copied to clipboard
@NonNull
fun <I, O> registerForActivityResult(@NonNull contract: @NonNull ActivityResultContract<I, O>, @NonNull callback: @NonNull ActivityResultCallback<O>): @NonNull ActivityResultLauncher<I>
If the host of this fragment is an ActivityResultRegistryOwner the ActivityResultRegistry of the host will be used.
@NonNull
fun <I, O> registerForActivityResult(@NonNull contract: @NonNull ActivityResultContract<I, O>, @NonNull registry: @NonNull ActivityResultRegistry, @NonNull callback: @NonNull ActivityResultCallback<O>): @NonNull ActivityResultLauncher<I>
Link copied to clipboard
open fun registerForContextMenu(@NonNull view: @NonNull View)
Registers a context menu to be shown for the given view (multiple views can show the context menu).
Link copied to clipboard
open fun removeOnContextAvailableListener(@NonNull listener: @NonNull OnContextAvailableListener)
Link copied to clipboard
fun requestPermissions(@NonNull permissions: Array<@NonNull String>, requestCode: Int)
Requests permissions to be granted to this application.
Link copied to clipboard
@NonNull
fun requireActivity(): @NonNull FragmentActivity
Return the FragmentActivity this fragment is currently associated with.
Link copied to clipboard
@NonNull
fun requireArguments(): @NonNull Bundle
Return the arguments supplied when the fragment was instantiated.
Link copied to clipboard
@NonNull
fun requireContext(): @NonNull Context
Return the Context this fragment is currently associated with.
Link copied to clipboard
@NonNull
fun requireFragmentManager(): @NonNull FragmentManager
Return the FragmentManager for interacting with fragments associated with this fragment's activity.
Link copied to clipboard
@NonNull
fun requireHost(): @NonNull Any
Return the host object of this fragment.
Link copied to clipboard
@NonNull
fun requireParentFragment(): @NonNull Fragment
Returns the parent Fragment containing this Fragment.
Link copied to clipboard
@NonNull
fun requireView(): @NonNull View
Get the root view for the fragment's layout (the one returned by onCreateView).
Link copied to clipboard
Returns the registered SeslOnTransitionCallback, or null if none is set.
Link copied to clipboard
Returns whether predictive back transitions are enabled for this fragment.
Link copied to clipboard
Returns whether the fragment transition helper is initialized for predictive back.
Link copied to clipboard
open fun seslSetOnTransitionCallback(@Nullable callback: @Nullable Fragment.SeslOnTransitionCallback)
Registers a SeslOnTransitionCallback to receive transition lifecycle events.
Link copied to clipboard
Enables or disables predictive back transitions after checking system and gesture navigation prerequisites.
open fun seslSetPredictiveBackEnabled(enabled: Boolean, isBgCustomized: Boolean)
Enables or disables predictive back transitions with custom background flag.
Link copied to clipboard
Sets whether the the exit transition and enter transition overlap or not.
Link copied to clipboard
Sets whether the the return transition and reenter transition overlap or not.
Link copied to clipboard
open fun setArguments(@Nullable args: @Nullable Bundle)
Supply the construction arguments for this fragment.
Link copied to clipboard
open fun setEnterSharedElementCallback(@Nullable callback: @Nullable SharedElementCallback)
When custom transitions are used with Fragments, the enter transition callback is called when this Fragment is attached or detached when not popping the back stack.
Link copied to clipboard
open fun setEnterTransition(@Nullable transition: @Nullable Any)
Sets the Transition that will be used to move Views into the initial scene.
Link copied to clipboard
open fun setExitSharedElementCallback(@Nullable callback: @Nullable SharedElementCallback)
When custom transitions are used with Fragments, the exit transition callback is called when this Fragment is attached or detached when popping the back stack.
Link copied to clipboard
open fun setExitTransition(@Nullable transition: @Nullable Any)
Sets the Transition that will be used to move Views out of the scene when the fragment is removed, hidden, or detached when not popping the back stack.
Link copied to clipboard
fun Fragment.setFragmentResult(requestKey: String, result: Bundle)

Sets the given result for the requestKey. This result will be delivered to a FragmentResultListener that is called given to setFragmentResultListener with the same requestKey. If no FragmentResultListener with the same key is set or the Lifecycle associated with the listener is not at least androidx.lifecycle.Lifecycle.State.STARTED, the result is stored until one becomes available, or clearFragmentResult is called with the same requestKey.

Link copied to clipboard
fun Fragment.setFragmentResultListener(requestKey: String, listener: (requestKey: String, bundle: Bundle) -> Unit)

Sets the FragmentResultListener for a given requestKey. Once this Fragment is at least in the androidx.lifecycle.Lifecycle.State.STARTED state, any results set by setFragmentResult using the same requestKey will be delivered to the FragmentResultListener.onFragmentResult callback. The callback will remain active until this Fragment reaches the androidx.lifecycle.Lifecycle.State.DESTROYED state or clearFragmentResultListener is called with the same requestKey.

Link copied to clipboard
open fun setHasOptionsMenu(hasMenu: Boolean)
Report that this fragment would like to participate in populating the options menu by receiving a call to onCreateOptionsMenu and related methods.
Link copied to clipboard
open fun setInitialSavedState(@Nullable state: @Nullable Fragment.SavedState)
Set the initial saved state that this Fragment should restore itself from when first being constructed, as returned by FragmentManager.saveFragmentInstanceState.
Link copied to clipboard
open fun setMenuVisibility(menuVisible: Boolean)
Set a hint for whether this fragment's menu should be visible.
Link copied to clipboard
open fun setReenterTransition(@Nullable transition: @Nullable Any)
Sets the Transition that will be used to move Views in to the scene when returning due to popping a back stack.
Link copied to clipboard
open fun setRetainInstance(retain: Boolean)
Control whether a fragment instance is retained across Activity re-creation (such as from a configuration change).
Link copied to clipboard
open fun setReturnTransition(@Nullable transition: @Nullable Any)
Sets the Transition that will be used to move Views out of the scene when the Fragment is preparing to be removed, hidden, or detached because of popping the back stack.
Link copied to clipboard
open fun setSharedElementEnterTransition(@Nullable transition: @Nullable Any)
Sets the Transition that will be used for shared elements transferred into the content Scene.
Link copied to clipboard
open fun setSharedElementReturnTransition(@Nullable transition: @Nullable Any)
Sets the Transition that will be used for shared elements transferred back during a pop of the back stack.
Link copied to clipboard
open fun setTargetFragment(@Nullable fragment: @Nullable Fragment, requestCode: Int)
Optional target for this fragment.
Link copied to clipboard
open fun setUserVisibleHint(isVisibleToUser: Boolean)
Set a hint to the system about whether this fragment's UI is currently visible to the user.
Link copied to clipboard
open fun shouldShowRequestPermissionRationale(@NonNull permission: @NonNull String): Boolean
Gets whether you should show UI with rationale before requesting a permission.
Link copied to clipboard
open fun startActivity(@NonNull intent: @NonNull Intent)
open fun startActivity(@NonNull intent: @NonNull Intent, @Nullable options: @Nullable Bundle)
Call startActivity from the fragment's containing Activity.
Link copied to clipboard
open fun startActivityForResult(@NonNull intent: @NonNull Intent, requestCode: Int)
Call startActivityForResult from the fragment's containing Activity.
open fun startActivityForResult(@NonNull intent: @NonNull Intent, requestCode: Int, @Nullable options: @Nullable Bundle)
Call startActivityForResult from the fragment's containing Activity.
Link copied to clipboard
open fun startIntentSenderForResult(@NonNull intent: @NonNull IntentSender, requestCode: Int, @Nullable fillInIntent: @Nullable Intent, flagsMask: Int, flagsValues: Int, extraFlags: Int, @Nullable options: @Nullable Bundle)
Call startIntentSenderForResult from the fragment's containing Activity.
Link copied to clipboard
Begin postponed transitions after postponeEnterTransition was called.
Link copied to clipboard
@NonNull
open fun toString(): @NonNull String
Link copied to clipboard
open fun unregisterForContextMenu(@NonNull view: @NonNull View)
Prevents a context menu to be shown for the given view.
Link copied to clipboard
inline fun <VM : ViewModel> Fragment.viewModels(noinline ownerProducer: () -> ViewModelStoreOwner = { this }, noinline extrasProducer: () -> CreationExtras? = null, noinline factoryProducer: () -> ViewModelProvider.Factory? = null): Lazy<VM>

Returns a property delegate to access ViewModel by default scoped to this Fragment: