Save the date! Android Dev Summit is coming to Sunnyvale, CA on Oct 23-24, 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

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

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

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 Boolean
onPreparePanel(featureId: Int, @Nullable view: View?, @NonNull menu: Menu)

Dispatch onPrepareOptionsMenu() to fragments.

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

open Unit

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

open Unit
onAttachFragment(@NonNull fragment: Fragment)

Called when a fragment is attached to the activity.

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

Call onOptionsMenuClosed() on fragments.

open Unit

Dispatch configuration change to all fragments.

open Unit

Support library version of Activity#invalidateOptionsMenu.

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 Unit

Dispatch onLowMemory() to all 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
startIntentSenderFromFragment(@NonNull fragment: Fragment, intent: IntentSender!, requestCode: Int, @Nullable fillInIntent: Intent?, flagsMask: Int, flagsValues: Int, extraFlags: Int, @Nullable options: Bundle?)

Called by 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 LoaderManager

open Unit

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

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 Boolean
onCreatePanelMenu(featureId: Int, @NonNull menu: Menu)

Dispatch to Fragment.

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
onMenuItemSelected(featureId: Int, @NonNull item: MenuItem)

Dispatch context and options menu to fragments.

open FragmentManager

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

Protected methods
open Unit

Dispatch onPause() to fragments.

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

Dispatch incoming result to the correct fragment.

open Unit

Dispatch onResume() to 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
onCreate(@Nullable savedInstanceState: Bundle?)

Perform initialization of all fragments.

open Unit

Dispatch onStart() to all 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

Dispatch onResume() to fragments.

open Unit
onSaveInstanceState(@NonNull outState: Bundle)

Save all appropriate fragment state.

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

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.

supportPostponeEnterTransition

open fun supportPostponeEnterTransition(): Unit

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

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

onPreparePanel

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

Dispatch onPrepareOptionsMenu() to fragments.

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.

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.

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.

validateRequestPermissionsRequestCode

fun validateRequestPermissionsRequestCode(requestCode: Int): Unit

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.

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.

onPanelClosed

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

Call onOptionsMenuClosed() on fragments.

onConfigurationChanged

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

Dispatch configuration change to all fragments.

supportInvalidateOptionsMenu

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

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.

onLowMemory

open fun onLowMemory(): Unit

Dispatch onLowMemory() to all 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.

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.

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?

getSupportLoaderManager

@NonNull open fun getSupportLoaderManager(): LoaderManager

supportStartPostponedEnterTransition

open fun supportStartPostponedEnterTransition(): Unit

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

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

onCreatePanelMenu

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

Dispatch to Fragment.onCreateOptionsMenu().

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.

onMenuItemSelected

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

Dispatch context and options menu to fragments.

getSupportFragmentManager

@NonNull open fun getSupportFragmentManager(): FragmentManager

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

Protected methods

onPause

protected open fun onPause(): Unit

Dispatch onPause() to fragments.

onActivityResult

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

Dispatch incoming result to the correct fragment.

onPostResume

protected open fun onPostResume(): Unit

Dispatch onResume() to 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.

onCreate

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

Perform initialization of all fragments.

onStart

protected open fun onStart(): Unit

Dispatch onStart() to all fragments.

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.

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.

onSaveInstanceState

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

Save all appropriate fragment state.

onStop

protected open fun onStop(): Unit

Dispatch onStop() to all fragments.