Fragment

open class Fragment : ComponentCallbacks, OnCreateContextMenuListener, LifecycleOwner, ViewModelStoreOwner, HasDefaultViewModelProviderFactory, SavedStateRegistryOwner, ActivityResultCaller
kotlin.Any
   ↳ androidx.fragment.app.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:

Summary

Nested classes
open

Thrown by FragmentFactory#instantiate(ClassLoader, String) when there is an instantiation failure.

open

State information that has been retrieved from a fragment instance through FragmentManager.saveFragmentInstanceState.

Public constructors

Constructor used by the default FragmentFactory.

<init>(@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(LayoutInflater, ViewGroup, Bundle).

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

Print the Fragments's state into the given stream.

Boolean
equals(@Nullable other: Any?)

Subclasses can not override equals().

FragmentActivity?

Return the FragmentActivity this fragment is currently associated with.

open Boolean

Returns whether the the exit transition and enter transition overlap or not.

open Boolean

Returns whether the the return transition and reenter transition overlap or not.

Bundle?

Return the arguments supplied when the fragment was instantiated, if any.

FragmentManager

Return a private FragmentManager for placing and managing Fragments inside of this Fragment.

open Context?

Return the Context this fragment is currently associated with.

open ViewModelProvider.Factory

Returns the default ViewModelProvider.Factory that should be used when no custom Factory is provided to the ViewModelProvider constructors.

open Any?

Returns the Transition that will be used to move Views into the initial scene.

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

FragmentManager?

Return the FragmentManager for interacting with fragments associated with this fragment's activity.

Any?

Return the host object of this fragment.

Int

Return the identifier this fragment is known by.

LayoutInflater

Returns the cached LayoutInflater used to inflate Views of this Fragment.

open Lifecycle

Returns the Lifecycle of the provider.

open LoaderManager

Return the LoaderManager for this fragment.

Fragment?

Returns the parent Fragment containing this Fragment.

FragmentManager

Return the FragmentManager for interacting with fragments associated with this fragment's activity.

open Any?

Returns the Transition that will be used to move Views in to the scene when returning due to popping a back stack.

Resources

Return requireActivity().getResources().

Boolean

Returns true if this fragment instance's state will be retained across configuration changes, and false if it will not.

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

SavedStateRegistry

open Any?

Returns the Transition that will be used for shared elements transferred into the content Scene.

open Any?

Return the Transition that will be used for shared elements transferred back during a pop of the back stack.

String
getString(@StringRes resId: Int)

Return a localized string from the application's package's default string table.

String
getString(@StringRes resId: Int, @Nullable vararg formatArgs: Any!)

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

String?

Get the tag name of the fragment, if specified.

Fragment?

Return the target fragment set by setTargetFragment.

Int

Return the target request code set by setTargetFragment.

CharSequence
getText(@StringRes resId: Int)

Return a localized, styled CharSequence from the application's package's default string table.

open Boolean

open View?

Get the root view for the fragment's layout (the one returned by onCreateView), if provided.

open LifecycleOwner

Get a LifecycleOwner that represents the Fragment's View lifecycle.

open LiveData<LifecycleOwner!>

Retrieve a LiveData which allows you to observe the lifecycle of the Fragment's View.

open ViewModelStore

Returns the ViewModelStore associated with this Fragment

Int

Subclasses can not override hashCode().

open static Fragment
instantiate(@NonNull context: Context, @NonNull fname: String)

Like instantiate(Context, String, Bundle) but with a null argument Bundle.

open static Fragment
instantiate(@NonNull context: Context, @NonNull fname: String, @Nullable args: Bundle?)

Create a new instance of a Fragment with the given class name.

Boolean

Return true if the fragment is currently added to its activity.

Boolean

Return true if the fragment has been explicitly detached from the UI.

Boolean

Return true if the fragment has been hidden.

Boolean

Return true if the layout is included as part of an activity view hierarchy via the <fragment> tag.

Boolean

Return true if this fragment is currently being removed from its activity.

Boolean

Return true if the fragment is in the resumed state.

Boolean

Returns true if this fragment is added and its state has already been saved by its host.

Boolean

Return true if the fragment is currently visible to the user.

open Unit
onActivityCreated(@Nullable savedInstanceState: Bundle?)

Called when the fragment's activity has been created and this fragment's view hierarchy instantiated.

open Unit
onActivityResult(requestCode: Int, resultCode: Int, @Nullable data: Intent?)

Receive the result from a previous call to startActivityForResult(Intent, int).

open Unit
onAttach(@NonNull context: Context)

Called when a fragment is first attached to its context.

open Unit
onAttach(@NonNull activity: Activity)

Called when a fragment is first attached to its activity.

open Unit
onAttachFragment(@NonNull childFragment: Fragment)

Called when a fragment is attached as a child of this fragment.

open Unit

open Boolean

This hook is called whenever an item in a context menu is selected.

open Unit
onCreate(@Nullable savedInstanceState: Bundle?)

Called to do initial creation of a fragment.

open Animation?
onCreateAnimation(transit: Int, enter: Boolean, nextAnim: Int)

Called when a fragment loads an animation.

open Animator?
onCreateAnimator(transit: Int, enter: Boolean, nextAnim: Int)

Called when a fragment loads an animator.

open Unit
onCreateContextMenu(@NonNull menu: ContextMenu, @NonNull v: View, @Nullable menuInfo: ContextMenuInfo?)

Called when a context menu for the view is about to be shown.

open Unit
onCreateOptionsMenu(@NonNull menu: Menu, @NonNull inflater: MenuInflater)

Initialize the contents of the Fragment host's standard options menu.

open View?
onCreateView(@NonNull inflater: LayoutInflater, @Nullable container: ViewGroup?, @Nullable savedInstanceState: Bundle?)

Called to have the fragment instantiate its user interface view.

open Unit

Called when the fragment is no longer in use.

open Unit

Called when this fragment's option menu items are no longer being included in the overall options menu.

open Unit

Called when the view previously created by onCreateView has been detached from the fragment.

open Unit

Called when the fragment is no longer attached to its activity.

open LayoutInflater
onGetLayoutInflater(@Nullable savedInstanceState: Bundle?)

Returns the LayoutInflater used to inflate Views of this Fragment.

open Unit

Called when the hidden state (as returned by isHidden() of the fragment has changed.

open Unit
onInflate(@NonNull context: Context, @NonNull attrs: AttributeSet, @Nullable savedInstanceState: Bundle?)

Called when a fragment is being created as part of a view layout inflation, typically from setting the content view of an activity.

open Unit
onInflate(@NonNull activity: Activity, @NonNull attrs: AttributeSet, @Nullable savedInstanceState: Bundle?)

Called when a fragment is being created as part of a view layout inflation, typically from setting the content view of an activity.

open Unit

open Unit
onMultiWindowModeChanged(isInMultiWindowMode: Boolean)

Called when the Fragment's activity changes from fullscreen mode to multi-window mode and visa-versa.

open Boolean

This hook is called whenever an item in your options menu is selected.

open Unit
onOptionsMenuClosed(@NonNull menu: 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).

open Unit

Called when the Fragment is no longer resumed.

open Unit
onPictureInPictureModeChanged(isInPictureInPictureMode: Boolean)

Called by the system when the activity changes to and from picture-in-picture mode.

open Unit
onPrepareOptionsMenu(@NonNull menu: Menu)

Prepare the Fragment host's standard options menu to be displayed.

open Unit
onPrimaryNavigationFragmentChanged(isPrimaryNavigationFragment: Boolean)

Callback for when the primary navigation state of this Fragment has changed.

open Unit
onRequestPermissionsResult(requestCode: Int, @NonNull permissions: Array<String!>, @NonNull grantResults: IntArray)

Callback for the result from requesting permissions.

open Unit

Called when the fragment is visible to the user and actively running.

open Unit
onSaveInstanceState(@NonNull outState: 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.

open Unit

Called when the Fragment is visible to the user.

open Unit

Called when the Fragment is no longer started.

open Unit
onViewCreated(@NonNull view: View, @Nullable savedInstanceState: Bundle?)

Called immediately after onCreateView(LayoutInflater, ViewGroup, Bundle) has returned, but before any saved state has been restored in to the view.

open Unit
onViewStateRestored(@Nullable savedInstanceState: Bundle?)

Called when all saved state has been restored into the view hierarchy of the fragment.

open Unit

Postpone the entering Fragment transition until startPostponedEnterTransition() or FragmentManager#executePendingTransactions() has been called.

Unit
postponeEnterTransition(duration: Long, @NonNull timeUnit: TimeUnit)

Postpone the entering Fragment transition for a given amount of time and then call startPostponedEnterTransition().

ActivityResultLauncher<I>
registerForActivityResult(@NonNull contract: ActivityResultContract<I, O>, @NonNull callback: ActivityResultCallback<O>)

Register a request to Activity#startActivityForResult, designated by the given contract.

ActivityResultLauncher<I>
registerForActivityResult(@NonNull contract: ActivityResultContract<I, O>, @NonNull registry: ActivityResultRegistry, @NonNull callback: ActivityResultCallback<O>)

open Unit
registerForContextMenu(@NonNull view: View)

Registers a context menu to be shown for the given view (multiple views can show the context menu).

Unit
requestPermissions(@NonNull permissions: Array<String!>, requestCode: Int)

Requests permissions to be granted to this application.

FragmentActivity

Return the FragmentActivity this fragment is currently associated with.

Bundle

Return the arguments supplied when the fragment was instantiated.

Context

Return the Context this fragment is currently associated with.

FragmentManager

Return the FragmentManager for interacting with fragments associated with this fragment's activity.

Any

Return the host object of this fragment.

Fragment

Returns the parent Fragment containing this Fragment.

View

Get the root view for the fragment's layout (the one returned by onCreateView).

open