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

public class FragmentActivity


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

Known limitations:

Summary

Public constructors

Default constructor for FragmentActivity.

FragmentActivity(@NonNull int contentLayoutId)

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

Public methods

@NonNull void
dump(
    @NonNull String prefix,
    @Nullable FileDescriptor fd,
    @NonNull PrintWriter writer,
    @Nullable Array<@NonNull String> args
)

Print the Activity's state into the given stream.

@NonNull FragmentManager

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

@NonNull LoaderManager

This method is deprecated.

Use LoaderManager.getInstance(this).

@NonNull void

This method is deprecated.

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

@NonNull void
onConfigurationChanged(@NonNull Configuration newConfig)

Dispatch configuration change to all fragments.

@NonNull boolean
onCreatePanelMenu(@NonNull int featureId, @NonNull Menu menu)

Dispatch to Fragment.onCreateOptionsMenu().

@Nullable View
onCreateView(
    @Nullable View parent,
    @NonNull String name,
    @NonNull Context context,
    @NonNull AttributeSet attrs
)
@Nullable View
onCreateView(
    @NonNull String name,
    @NonNull Context context,
    @NonNull AttributeSet attrs
)
@NonNull void

Dispatch onLowMemory() to all fragments.

@NonNull boolean
onMenuItemSelected(@NonNull int featureId, @NonNull MenuItem item)

Dispatch context and options menu to fragments.

@NonNull void
onMultiWindowModeChanged(@NonNull boolean isInMultiWindowMode)

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

@NonNull void
onPanelClosed(@NonNull int featureId, @NonNull Menu menu)

Call onOptionsMenuClosed() on fragments.

@NonNull void
onPictureInPictureModeChanged(@NonNull boolean isInPictureInPictureMode)

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

@NonNull boolean
onPreparePanel(
    @NonNull int featureId,
    @Nullable View view,
    @NonNull Menu menu
)

Dispatch onPrepareOptionsMenu() to fragments.

@NonNull void
onRequestPermissionsResult(
    @NonNull int requestCode,
    @NonNull Array<@NonNull String> permissions,
    @NonNull Array<@NonNull int> grantResults
)
@NonNull void

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

@NonNull void
setEnterSharedElementCallback(@Nullable SharedElementCallback callback)

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

@NonNull void
setExitSharedElementCallback(@Nullable SharedElementCallback listener)

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

@NonNull void
startActivityFromFragment(
    @NonNull Fragment fragment,
    @NonNull Intent intent,
    @NonNull int requestCode
)

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

@NonNull void
startActivityFromFragment(
    @NonNull Fragment fragment,
    @NonNull Intent intent,
    @NonNull int requestCode,
    @Nullable Bundle options
)

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

@NonNull void
startIntentSenderFromFragment(
    @NonNull Fragment fragment,
    @NonNull IntentSender intent,
    @NonNull int requestCode,
    @Nullable Intent fillInIntent,
    @NonNull int flagsMask,
    @NonNull int flagsValues,
    @NonNull int extraFlags,
    @Nullable Bundle options
)

This method is deprecated.

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

@NonNull void

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

@NonNull void

This method is deprecated.

Call Activity#invalidateOptionsMenu directly.

@NonNull void

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

@NonNull void

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

final @NonNull void

This method is deprecated.

there are no longer any restrictions on permissions requestCodes.

Public constructors

FragmentActivity

@NonNull
public 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

@NonNull
public FragmentActivity(@NonNull int contentLayoutId)

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 methods

dump

@NonNull
public void dump(
    @NonNull String prefix,
    @Nullable FileDescriptor fd,
    @NonNull PrintWriter writer,
    @Nullable Array<@NonNull String> args
)

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

Parameters
@NonNull String prefix

Desired prefix to prepend at each line of output.

@Nullable FileDescriptor fd

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

@NonNull PrintWriter writer

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

@Nullable Array<@NonNull String> args

additional arguments to the dump request.

getSupportFragmentManager

@NonNull
public FragmentManager getSupportFragmentManager()

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

getSupportLoaderManager

@NonNull
public LoaderManager getSupportLoaderManager()

onAttachFragment

@NonNull
public void onAttachFragment(@NonNull Fragment fragment)

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

@NonNull
public void onConfigurationChanged(@NonNull Configuration newConfig)

Dispatch configuration change to all fragments.

onCreatePanelMenu

@NonNull
public boolean onCreatePanelMenu(@NonNull int featureId, @NonNull Menu menu)

Dispatch to Fragment.onCreateOptionsMenu().

onCreateView

@Nullable
@NonNull
public View onCreateView(
    @Nullable View parent,
    @NonNull String name,
    @NonNull Context context,
    @NonNull AttributeSet attrs
)

onCreateView

@Nullable
@NonNull
public View onCreateView(
    @NonNull String name,
    @NonNull Context context,
    @NonNull AttributeSet attrs
)

onLowMemory

@NonNull
public void onLowMemory()

Dispatch onLowMemory() to all fragments.

onMenuItemSelected

@NonNull
public boolean onMenuItemSelected(@NonNull int featureId, @NonNull MenuItem item)

Dispatch context and options menu to fragments.

onMultiWindowModeChanged

@NonNull
public void onMultiWindowModeChanged(@NonNull boolean isInMultiWindowMode)

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

Parameters
@NonNull boolean isInMultiWindowMode

True if the activity is in multi-window mode.

onPanelClosed

@NonNull
public void onPanelClosed(@NonNull int featureId, @NonNull Menu menu)

Call onOptionsMenuClosed() on fragments.

onPictureInPictureModeChanged

@NonNull
public void onPictureInPictureModeChanged(@NonNull boolean isInPictureInPictureMode)

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

Parameters
@NonNull boolean isInPictureInPictureMode

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

onPreparePanel

@NonNull
public boolean onPreparePanel(
    @NonNull int featureId,
    @Nullable View view,
    @NonNull Menu menu
)

Dispatch onPrepareOptionsMenu() to fragments.

onRequestPermissionsResult

@NonNull
public void onRequestPermissionsResult(
    @NonNull int requestCode,
    @NonNull Array<@NonNull String> permissions,
    @NonNull Array<@NonNull int> grantResults
)

onStateNotSaved

@NonNull
public void onStateNotSaved()

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

setEnterSharedElementCallback

@NonNull
public void setEnterSharedElementCallback(@Nullable SharedElementCallback callback)

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
@Nullable SharedElementCallback callback

Used to manipulate shared element transitions on the launched Activity.

setExitSharedElementCallback

@NonNull
public void setExitSharedElementCallback(@Nullable SharedElementCallback listener)

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
@Nullable SharedElementCallback listener

Used to manipulate shared element transitions on the launching Activity.

startActivityFromFragment

@NonNull
public void startActivityFromFragment(
    @NonNull Fragment fragment,
    @NonNull Intent intent,
    @NonNull int requestCode
)

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

Parameters
@NonNull Fragment fragment

the Fragment to start the activity from.

@NonNull Intent intent

The intent to start.

@NonNull int requestCode

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

@NonNull
public void startActivityFromFragment(
    @NonNull Fragment fragment,
    @NonNull Intent intent,
    @NonNull int requestCode,
    @Nullable Bundle options
)

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

Parameters
@NonNull Fragment fragment

the Fragment to start the activity from.

@NonNull Intent intent

The intent to start.

@NonNull int requestCode

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.

@Nullable Bundle options

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

startIntentSenderFromFragment

@NonNull
public void startIntentSenderFromFragment(
    @NonNull Fragment fragment,
    @NonNull IntentSender intent,
    @NonNull int requestCode,
    @Nullable Intent fillInIntent,
    @NonNull int flagsMask,
    @NonNull int flagsValues,
    @NonNull int extraFlags,
    @Nullable Bundle options
)

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

Parameters
@NonNull Fragment fragment

the Fragment to start the intent sender from.

@NonNull IntentSender intent

The IntentSender to launch.

@NonNull int requestCode

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.

@Nullable Intent fillInIntent

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.

@NonNull int flagsMask

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

@NonNull int flagsValues

Desired values for any bits set in flagsMask.

@NonNull int extraFlags

Always set to 0.

@Nullable Bundle options

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

@NonNull
public void supportFinishAfterTransition()

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

@NonNull
public void supportInvalidateOptionsMenu()

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

@NonNull
public void supportPostponeEnterTransition()

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

supportStartPostponedEnterTransition

@NonNull
public void supportStartPostponedEnterTransition()

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

validateRequestPermissionsRequestCode

@NonNull
public final void validateRequestPermissionsRequestCode(@NonNull int requestCode)