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

FragmentActivity

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

class FragmentActivity


Base class for activities that want to use the support-based Fragments.

Known limitations:

Summary

Public constructors

Default constructor for FragmentActivity.

FragmentActivity(contentLayoutId: Int)

Alternate constructor that can be used to provide a default layout that will be inflated as part of super.onCreate(savedInstanceState).

Public functions

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

Print the Activity's state into the given stream.

FragmentManager

Return the FragmentManager for interacting with fragments associated with this activity.

LoaderManager

This function is deprecated.

Use LoaderManager.getInstance(this).

Unit

This function is deprecated.

The responsibility for listening for fragments being attached has been moved to FragmentManager.

Unit
onConfigurationChanged(newConfig: Configuration)

Dispatch configuration change to all fragments.

Boolean
onCreatePanelMenu(featureId: Int, menu: Menu)

Dispatch to Fragment.onCreateOptionsMenu().

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

Dispatch onLowMemory() to all fragments.

Boolean
onMenuItemSelected(featureId: Int, item: MenuItem)

Dispatch context and options menu to fragments.

Unit
onMultiWindowModeChanged(isInMultiWindowMode: Boolean)

Note: If you override this method you must call super.onMultiWindowModeChanged to correctly dispatch the event to support fragments attached to this activity.

Unit
onPanelClosed(featureId: Int, menu: Menu)

Call onOptionsMenuClosed() on fragments.

Unit
onPictureInPictureModeChanged(isInPictureInPictureMode: Boolean)

Note: If you override this method you must call super.onPictureInPictureModeChanged to correctly dispatch the event to support fragments attached to this activity.

Boolean
onPreparePanel(featureId: Int, view: View?, menu: Menu)

Dispatch onPrepareOptionsMenu() to fragments.

Unit
onRequestPermissionsResult(
    requestCode: Int,
    permissions: Array<String>,
    grantResults: Array<Int>
)
Unit

Hook in to note that fragment state is no longer saved.

Unit
setEnterSharedElementCallback(callback: SharedElementCallback?)

When makeSceneTransitionAnimation was used to start an Activity, callbackwill be called to handle shared elements on the launched Activity.

Unit
setExitSharedElementCallback(listener: SharedElementCallback?)

When makeSceneTransitionAnimation was used to start an Activity, listenerwill be called to handle shared elements on the launching Activity.

Unit
startActivityFromFragment(
    fragment: Fragment,
    intent: Intent,
    requestCode: Int
)

Called by Fragment.startActivityForResult() to implement its behavior.

Unit
startActivityFromFragment(
    fragment: Fragment,
    intent: Intent,
    requestCode: Int,
    options: Bundle?
)

Called by Fragment.startActivityForResult() to implement its behavior.

Unit
startIntentSenderFromFragment(
    fragment: Fragment,
    intent: IntentSender,
    requestCode: Int,
    fillInIntent: Intent?,
    flagsMask: Int,
    flagsValues: Int,
    extraFlags: Int,
    options: Bundle?
)

This function is deprecated.

Fragments should use Fragment#registerForActivityResult(ActivityResultContract, ActivityResultCallback) with the StartIntentSenderForResult contract.

Unit

Reverses the Activity Scene entry Transition and triggers the calling Activity to reverse its exit Transition.

Unit

This function is deprecated.

Call Activity#invalidateOptionsMenu directly.

Unit

Support library version of postponeEnterTransition that works only on API 21 and later.

Unit

Support library version of startPostponedEnterTransition that only works with API 21 and later.

Unit

This function is deprecated.

there are no longer any restrictions on permissions requestCodes.

Public constructors

FragmentActivity

FragmentActivity()

Default constructor for FragmentActivity. All Activities must have a default constructor for API 27 and lower devices or when using the default android.app.AppComponentFactory.

FragmentActivity

FragmentActivity(contentLayoutId: Int)

Alternate constructor that can be used to provide a default layout that will be inflated as part of super.onCreate(savedInstanceState).

This should generally be called from your constructor that takes no parameters, as is required for API 27 and lower or when using the default android.app.AppComponentFactory.

See also
FragmentActivity

#FragmentActivity()

Public functions

dump

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

Print the Activity's state into the given stream. This gets invoked if you run "adb shell dumpsys activity ".

Parameters
prefix: String

Desired prefix to prepend at each line of output.

fd: FileDescriptor?

The raw file descriptor that the dump is being sent to.

writer: PrintWriter

The PrintWriter to which you should dump your state. This will be closed for you after you return.

args: Array<String>?

additional arguments to the dump request.

getSupportFragmentManager

fun getSupportFragmentManager(): FragmentManager

Return the FragmentManager for interacting with fragments associated with this activity.

getSupportLoaderManager

fun getSupportLoaderManager(): LoaderManager

onAttachFragment

fun onAttachFragment(fragment: Fragment): Unit

Called when a fragment is attached to the activity.

This is called after the attached fragment's onAttach and before the attached fragment's onCreate if the fragment has not yet had a previous call to onCreate.

onConfigurationChanged

fun onConfigurationChanged(newConfig: Configuration): Unit

Dispatch configuration change to all fragments.

onCreatePanelMenu

fun onCreatePanelMenu(featureId: Int, menu: Menu): Boolean

Dispatch to Fragment.onCreateOptionsMenu().

onCreateView

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

onCreateView

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

onLowMemory

fun onLowMemory(): Unit

Dispatch onLowMemory() to all fragments.

onMenuItemSelected

fun onMenuItemSelected(featureId: Int, item: MenuItem): Boolean

Dispatch context and options menu to fragments.

onMultiWindowModeChanged

fun onMultiWindowModeChanged(isInMultiWindowMode: Boolean): Unit

Note: If you override this method you must call super.onMultiWindowModeChanged to correctly dispatch the event to support fragments attached to this activity.

Parameters
isInMultiWindowMode: Boolean

True if the activity is in multi-window mode.

onPanelClosed

fun onPanelClosed(featureId: Int, menu: Menu): Unit

Call onOptionsMenuClosed() on fragments.

onPictureInPictureModeChanged

fun onPictureInPictureModeChanged(isInPictureInPictureMode: Boolean): Unit

Note: If you override this method you must call super.onPictureInPictureModeChanged to correctly dispatch the event to support fragments attached to this activity.

Parameters
isInPictureInPictureMode: Boolean

True if the activity is in picture-in-picture mode.

onPreparePanel

fun onPreparePanel(featureId: Int, view: View?, menu: Menu): Boolean

Dispatch onPrepareOptionsMenu() to fragments.

onRequestPermissionsResult

fun onRequestPermissionsResult(
    requestCode: Int,
    permissions: Array<String>,
    grantResults: Array<Int>
): Unit

onStateNotSaved

fun onStateNotSaved(): Unit

Hook in to note that fragment state is no longer saved.

setEnterSharedElementCallback

fun setEnterSharedElementCallback(callback: SharedElementCallback?): Unit

When makeSceneTransitionAnimation was used to start an Activity, callbackwill be called to handle shared elements on the launched Activity. This requires Window#FEATURE_CONTENT_TRANSITIONS.

Parameters
callback: SharedElementCallback?

Used to manipulate shared element transitions on the launched Activity.

setExitSharedElementCallback

fun setExitSharedElementCallback(listener: SharedElementCallback?): Unit

When makeSceneTransitionAnimation was used to start an Activity, listenerwill be called to handle shared elements on the launching Activity. Most calls will only come when returning from the started Activity. This requires Window#FEATURE_CONTENT_TRANSITIONS.

Parameters
listener: SharedElementCallback?

Used to manipulate shared element transitions on the launching Activity.

startActivityFromFragment

fun startActivityFromFragment(
    fragment: Fragment,
    intent: Intent,
    requestCode: Int
): Unit

Called by Fragment.startActivityForResult() to implement its behavior.

Parameters
fragment: Fragment

the Fragment to start the activity from.

intent: Intent

The intent to start.

requestCode: Int

The request code to be returned in Fragment#onActivityResult(int, int, Intent) when the activity exits. Must be between 0 and 65535 to be considered valid. If given requestCode is greater than 65535, an IllegalArgumentException would be thrown.

startActivityFromFragment

fun startActivityFromFragment(
    fragment: Fragment,
    intent: Intent,
    requestCode: Int,
    options: Bundle?
): Unit

Called by Fragment.startActivityForResult() to implement its behavior.

Parameters
fragment: Fragment

the Fragment to start the activity from.

intent: Intent

The intent to start.

requestCode: Int

The request code to be returned in Fragment#onActivityResult(int, int, Intent) when the activity exits. Must be between 0 and 65535 to be considered valid. If given requestCode is greater than 65535, an IllegalArgumentException would be thrown.

options: Bundle?

Additional options for how the Activity should be started. See Context#startActivity(Intent, Bundle) for more details. This value may be null.

startIntentSenderFromFragment

fun startIntentSenderFromFragment(
    fragment: Fragment,
    intent: IntentSender,
    requestCode: Int,
    fillInIntent: Intent?,
    flagsMask: Int,
    flagsValues: Int,
    extraFlags: Int,
    options: Bundle?
): Unit

Called by Fragment.startIntentSenderForResult() to implement its behavior.

Parameters
fragment: Fragment

the Fragment to start the intent sender from.

intent: IntentSender

The IntentSender to launch.

requestCode: Int

The request code to be returned in Fragment#onActivityResult(int, int, Intent) when the activity exits. Must be between 0 and 65535 to be considered valid. If given requestCode is greater than 65535, an IllegalArgumentException would be thrown.

fillInIntent: Intent?

If non-null, this will be provided as the intent parameter to IntentSender#sendIntent(Context, int, Intent, IntentSender.OnFinished, Handler). This value may be null.

flagsMask: Int

Intent flags in the original IntentSender that you would like to change.

flagsValues: Int

Desired values for any bits set in flagsMask.

extraFlags: Int

Always set to 0.

options: Bundle?

Additional options for how the Activity should be started. See Context#startActivity(Intent, Bundle) for more details. This value may be null.

Throws
android.content.IntentSender.SendIntentException

if the call fails to execute.

supportFinishAfterTransition

fun supportFinishAfterTransition(): Unit

Reverses the Activity Scene entry Transition and triggers the calling Activity to reverse its exit Transition. When the exit Transition completes, finish is called. If no entry Transition was used, finish() is called immediately and the Activity exit Transition is run.

On Android 4.4 or lower, this method only finishes the Activity with no special exit transition.

supportInvalidateOptionsMenu

fun supportInvalidateOptionsMenu(): Unit

Support library version of Activity#invalidateOptionsMenu.

Invalidate the activity's options menu. This will cause relevant presentations of the menu to fully update via calls to onCreateOptionsMenu and onPrepareOptionsMenu the next time the menu is requested.

supportPostponeEnterTransition

fun supportPostponeEnterTransition(): Unit

Support library version of postponeEnterTransition that works only on API 21 and later.

supportStartPostponedEnterTransition

fun supportStartPostponedEnterTransition(): Unit

Support library version of startPostponedEnterTransition that only works with API 21 and later.

validateRequestPermissionsRequestCode

fun validateRequestPermissionsRequestCode(requestCode: Int): Unit