Register now for Android Dev Summit 2019!

FragmentActivity

open class FragmentActivity : ComponentActivity, ActivityCompat.OnRequestPermissionsResultCallback, RequestPermissionsRequestCodeValidator
androidx.activity.ComponentActivity
   ↳ androidx.fragment.app.FragmentActivity

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

Known limitations:

  • When using the <fragment> tag, this implementation can not use the parent view's ID as the new fragment's ID. You must explicitly specify an ID (or tag) in the <fragment>.

Summary

Public constructors

Default constructor for FragmentActivity.

<init>(@LayoutRes contentLayoutId: Int)

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

Public methods

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

Print the Activity's state into the given stream.

open FragmentManager

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

open LoaderManager

open Unit
onAttachFragment(@NonNull fragment: Fragment)

Called when a fragment is attached to the activity.

open Unit

Dispatch configuration change to all fragments.

open Boolean
onCreatePanelMenu(featureId: Int, @NonNull menu: Menu)

Dispatch to Fragment.

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

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

open Unit

Dispatch onLowMemory() to all fragments.

open Boolean
onMenuItemSelected(featureId: Int, @NonNull item: MenuItem)

Dispatch context and options menu to fragments.

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

open Unit
onPanelClosed(featureId: Int, @NonNull menu: Menu)

Call onOptionsMenuClosed() on fragments.

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

open Boolean
onPreparePanel(featureId: Int, @Nullable view: View?, @NonNull menu: Menu)

Dispatch onPrepareOptionsMenu() to fragments.

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

Callback for the result from requesting permissions.

open Unit

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

open Unit

When android.app.ActivityOptions#makeSceneTransitionAnimation(Activity, * android.view.View, String) was used to start an Activity, callback will be called to handle shared elements on the launched Activity.

open Unit

When android.app.ActivityOptions#makeSceneTransitionAnimation(Activity, * android.view.View, String) was used to start an Activity, listener will be called to handle shared elements on the launching Activity.

open Unit
startActivityForResult(intent: Intent!, requestCode: Int)

Modifies the standard behavior to allow results to be delivered to fragments.

open Unit
startActivityForResult(intent: Intent!, requestCode: Int, @Nullable options: Bundle?)

open Unit
startActivityFromFragment(@NonNull fragment: Fragment, intent: Intent!, requestCode: Int)

Called by Fragment.

open Unit
startActivityFromFragment(@NonNull fragment: Fragment, intent: Intent!, requestCode: Int, @Nullable options: Bundle?)

Called by Fragment.

open Unit
startIntentSenderForResult(intent: IntentSender!, requestCode: Int, @Nullable fillInIntent: Intent?, flagsMask: Int, flagsValues: Int, extraFlags: Int)

open Unit
startIntentSenderForResult(intent: IntentSender!, requestCode: Int, @Nullable fillInIntent: Intent?, flagsMask: Int, flagsValues: Int, extraFlags: Int, @Nullable options: Bundle?)

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

Called by Fragment.

open Unit

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

open Unit

Support library version of Activity#invalidateOptionsMenu.

open Unit

Support library version of android.app.Activity#postponeEnterTransition() that works only on API 21 and later.

open Unit

Support library version of android.app.Activity#startPostponedEnterTransition() that only works with API 21 and later.

Unit

Protected methods

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

Dispatch incoming result to the correct fragment.

open Unit
onCreate(@Nullable savedInstanceState: Bundle?)

Perform initialization of all fragments.

open Unit

Destroy all fragments.

open Unit
onNewIntent(intent: Intent!)

Handle onNewIntent() to inform the fragment manager that the state is not saved.

open Unit

Dispatch onPause() to fragments.

open Unit

Dispatch onResume() to fragments.

open Unit

Dispatch onResume() to fragments.

open Unit

This is the fragment-orientated version of onResume() that you can override to perform operations in the Activity at the same point where its fragments are resumed.

open Unit
onSaveInstanceState(@NonNull outState: Bundle)

Save all appropriate fragment state.

open Unit

Dispatch onStart() to all fragments.

open Unit

Dispatch onStop() to all fragments.

Inherited functions

Public constructors

<init>

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.

<init>

FragmentActivity(@LayoutRes 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.

Public methods

dump

open fun dump(@NonNull prefix: String, @Nullable fd: FileDescriptor?, @NonNull writer: PrintWriter, @Nullable 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 String: The raw file descriptor that the dump is being sent to.
writer String: The PrintWriter to which you should dump your state. This will be closed for you after you return.
args String: additional arguments to the dump request.

getSupportFragmentManager

@NonNull open fun getSupportFragmentManager(): FragmentManager

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

getSupportLoaderManager

@NonNull open fun getSupportLoaderManager(): LoaderManager

Deprecated: Use LoaderManager.getInstance(this).

onAttachFragment

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

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

Dispatch configuration change to all fragments.

onCreatePanelMenu

open fun onCreatePanelMenu(featureId: Int, @NonNull menu: Menu): Boolean

Dispatch to Fragment.onCreateOptionsMenu().

onCreateView

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

onCreateView

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

onLowMemory

open fun onLowMemory(): Unit

Dispatch onLowMemory() to all fragments.

onMenuItemSelected

open fun onMenuItemSelected(featureId: Int, @NonNull item: MenuItem): Boolean

Dispatch context and options menu to fragments.

onMultiWindowModeChanged

@CallSuper open 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

open fun onPanelClosed(featureId: Int, @NonNull menu: Menu): Unit

Call onOptionsMenuClosed() on fragments.

onPictureInPictureModeChanged

@CallSuper open 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

open fun onPreparePanel(featureId: Int, @Nullable view: View?, @NonNull menu: Menu): Boolean

Dispatch onPrepareOptionsMenu() to fragments.

onRequestPermissionsResult

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

Callback for the result from requesting permissions. This method is invoked for every call on requestPermissions(String[], int).

Note: It is possible that the permissions request interaction with the user is interrupted. In this case you will receive empty permissions and results arrays which should be treated as a cancellation.

Parameters
requestCode Int: The request code passed in requestPermissions(String[], int).
permissions Int: The requested permissions. Never null.
grantResults Int: The grant results for the corresponding permissions which is either android.content.pm.PackageManager#PERMISSION_GRANTED or android.content.pm.PackageManager#PERMISSION_DENIED. Never null.

onStateNotSaved

open fun onStateNotSaved(): Unit

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

setEnterSharedElementCallback

open fun setEnterSharedElementCallback(@Nullable callback: SharedElementCallback?): Unit

When android.app.ActivityOptions#makeSceneTransitionAnimation(Activity, * android.view.View, String) was used to start an Activity, callback will 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

open fun setExitSharedElementCallback(@Nullable listener: SharedElementCallback?): Unit

When android.app.ActivityOptions#makeSceneTransitionAnimation(Activity, * android.view.View, String) was used to start an Activity, listener will 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.

startActivityForResult

open fun startActivityForResult(intent: Intent!, requestCode: Int): Unit

Modifies the standard behavior to allow results to be delivered to fragments. This imposes a restriction that requestCode be <= 0xffff.

startActivityForResult

open fun startActivityForResult(intent: Intent!, requestCode: Int, @Nullable options: Bundle?): Unit

startActivityFromFragment

open fun startActivityFromFragment(@NonNull fragment: Fragment, intent: Intent!, requestCode: Int): Unit

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

startActivityFromFragment

open fun startActivityFromFragment(@NonNull fragment: Fragment, intent: Intent!, requestCode: Int, @Nullable options: Bundle?): Unit

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

startIntentSenderForResult

open fun startIntentSenderForResult(intent: IntentSender!, requestCode: Int, @Nullable fillInIntent: Intent?, flagsMask: Int, flagsValues: Int, extraFlags: Int): Unit

startIntentSenderForResult

open fun startIntentSenderForResult(intent: IntentSender!, requestCode: Int, @Nullable fillInIntent: Intent?, flagsMask: Int, flagsValues: Int, extraFlags: Int, @Nullable options: Bundle?): Unit

startIntentSenderFromFragment

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

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

supportFinishAfterTransition

open 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

open fun supportInvalidateOptionsMenu(): Unit

Deprecated: Call Activity#invalidateOptionsMenu directly.

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

open fun supportPostponeEnterTransition(): Unit

Support library version of android.app.Activity#postponeEnterTransition() that works only on API 21 and later.

supportStartPostponedEnterTransition

open fun supportStartPostponedEnterTransition(): Unit

Support library version of android.app.Activity#startPostponedEnterTransition() that only works with API 21 and later.

validateRequestPermissionsRequestCode

fun validateRequestPermissionsRequestCode(requestCode: Int): Unit

Protected methods

onActivityResult

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

Dispatch incoming result to the correct fragment.

onCreate

protected open fun onCreate(@Nullable savedInstanceState: Bundle?): Unit

Perform initialization of all fragments.

onDestroy

protected open fun onDestroy(): Unit

Destroy all fragments.

onNewIntent

@CallSuper protected open fun onNewIntent(intent: Intent!): Unit

Handle onNewIntent() to inform the fragment manager that the state is not saved. If you are handling new intents and may be making changes to the fragment state, you want to be sure to call through to the super-class here first. Otherwise, if your state is saved but the activity is not stopped, you could get an onNewIntent() call which happens before onResume() and trying to perform fragment operations at that point will throw IllegalStateException because the fragment manager thinks the state is still saved.

onPause

protected open fun onPause(): Unit

Dispatch onPause() to fragments.

onPostResume

protected open fun onPostResume(): Unit

Dispatch onResume() to fragments.

onResume

protected open fun onResume(): Unit

Dispatch onResume() to fragments. Note that for better inter-operation with older versions of the platform, at the point of this call the fragments attached to the activity are not resumed.

onResumeFragments

protected open fun onResumeFragments(): Unit

This is the fragment-orientated version of onResume() that you can override to perform operations in the Activity at the same point where its fragments are resumed. Be sure to always call through to the super-class.

onSaveInstanceState

protected open fun onSaveInstanceState(@NonNull outState: Bundle): Unit

Save all appropriate fragment state.

onStart

protected open fun onStart(): Unit

Dispatch onStart() to all fragments.

onStop

protected open fun onStop(): Unit

Dispatch onStop() to all fragments.