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

FragmentActivity

Added in 1.1.0

public class FragmentActivity extends ComponentActivity implements ActivityCompat.OnRequestPermissionsResultCallback, ActivityCompat.RequestPermissionsRequestCodeValidator

java.lang.Object
   ↳ android.content.Context
     ↳ android.content.ContextWrapper
       ↳ android.view.ContextThemeWrapper
         ↳ android.app.Activity
           ↳ androidx.core.app.ComponentActivity
             ↳ androidx.activity.ComponentActivity
               ↳ androidx.fragment.app.FragmentActivity

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

Known limitations:

Summary

Public constructors

Default constructor for FragmentActivity.

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

void
dump(
    @NonNull String prefix,
    @Nullable FileDescriptor fd,
    @NonNull PrintWriter writer,
    @Nullable 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).

void

This method is deprecated.

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

@Nullable View
onCreateView(
    @NonNull String name,
    @NonNull Context context,
    @NonNull AttributeSet attrs
)
@Nullable View
onCreateView(
    @Nullable View parent,
    @NonNull String name,
    @NonNull Context context,
    @NonNull AttributeSet attrs
)
boolean
onMenuItemSelected(int featureId, @NonNull MenuItem item)
void
@CallSuper
onRequestPermissionsResult(
    int requestCode,
    @NonNull String[] permissions,
    @NonNull int[] grantResults
)

This method is deprecated.

void

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

void

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

void

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

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

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

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

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

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

This method is deprecated.

Fragments should use registerForActivityResult with the StartIntentSenderForResult contract.

void

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

void

This method is deprecated.

Call invalidateOptionsMenu directly.

void

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

void

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

final void

This method is deprecated.

there are no longer any restrictions on permissions requestCodes.

Protected methods

void
@CallSuper
onActivityResult(int requestCode, int resultCode, @Nullable Intent data)

This method is deprecated.

void
onCreate(@Nullable Bundle savedInstanceState)

Perform initialization of all fragments.

void

Destroy all fragments.

void

Dispatch onPause() to fragments.

void

Dispatch onResume() to fragments.

void

Dispatch onResume() to fragments.

void

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.

void

Dispatch onStart() to all fragments.

void

Dispatch onStop() to all fragments.

Inherited Constants

From android.content.ComponentCallbacks2
static final int
static final int
static final int
static final int
static final int
static final int
static final int

Inherited fields

From android.app.Activity
From android.content.Context
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static int
static int
static int
static int

This field is deprecated.

static int
static int
static int
static int
static int
static int
static int
static int
static int
static int
static int
static int
static int
static int
static int
static int
static int
static int
static int
static int
static int
static int
static int
static int

This field is deprecated.

static int
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static int
static int
static int
static int
static int
static int
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static int
static int
static int

This field is deprecated.

static int
static int
static int

This field is deprecated.

static int

This field is deprecated.

static String
static String
static String
static String
static String
static String

This field is deprecated.

static String
static String
static String
static String
static String
static String
static String
static String
static long
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String

This field is deprecated.

static String
static String
static String
static int
static int

This field is deprecated.

static int
static int
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String

This field is deprecated.

static String
static String
static String
static String
static String
static String
static String
static String
static String
static String
static String

This field is deprecated.

static String
static String
static String
From android.content.ContextWrapper

Inherited methods

From android.app.Activity
boolean
void
boolean
void
void
ComponentName
void
boolean
PendingIntent
createPendingResult(int p, Intent p1, int p2)
void

This method is deprecated.

void
void
boolean
boolean
boolean
boolean
void
dumpInternal(String p, FileDescriptor p1, PrintWriter p2, String[] p3)
void

This method is deprecated.

boolean
T
<T extends View> findViewById(int p)
void
void
void

This method is deprecated.

void
void
void
void

This method is deprecated.

ActionBar
ActivityInfo
ActivityThread
IBinder
Application
IBinder
AutofillManager.AutofillClient
ComponentName
String
int
ComponentName
ContentCaptureManager.ContentCaptureClient
Scene
TransitionManager
View
FragmentManager

This method is deprecated.

Intent
Object
String
int
LayoutInflater
LoaderManager

This method is deprecated.

String
int
MediaController
MenuInflater
int
OnBackInvokedDispatcher
Activity
Intent
SharedPreferences
Uri
int
SearchEvent
IBinder
SplashScreen
Object
int
CharSequence
int
VoiceInteractor
int
Window
WindowManager
boolean
void
boolean
boolean

This method is deprecated.

boolean
boolean
boolean
boolean
boolean
boolean
boolean
boolean
boolean
boolean
boolean
boolean
boolean
boolean
boolean
Cursor
managedQuery(Uri p, String[] p1, String p2, String p3)

This method is deprecated.

boolean
moveTaskToBack(boolean p)
boolean
boolean

This method is deprecated.

void
void
void
void
onApplyThemeResource(Resources.Theme p, int p1, boolean p2)
void

This method is deprecated.

void
void

This method is deprecated.

void
void
boolean
void
void
CharSequence
Dialog

This method is deprecated.

void
boolean
View
boolean

This method is deprecated.

void
void
boolean
void
boolean
onKeyDown(int p, KeyEvent p1)
boolean
onKeyLongPress(int p, KeyEvent p1)
boolean
onKeyMultiple(int p, int p1, KeyEvent p2)
boolean
onKeyShortcut(int p, KeyEvent p1)
boolean
onKeyUp(int p, KeyEvent p1)
void
void
void
boolean
onMenuOpened(int p, Menu p1)
void
boolean
boolean

This method is deprecated.

void
boolean
void
void
onPerformDirectAction(
    String p,
    Bundle p1,
    CancellationSignal p2,
    Consumer<Bundle> p3
)
boolean
void
void
void
onPrepareDialog(int p, Dialog p1)

This method is deprecated.

void
boolean
void
void
void
onProvideKeyboardShortcuts(
    List<KeyboardShortcutGroup> p,
    Menu p1,
    int p2
)
Uri
void
void
boolean
void
void
boolean
boolean
void
void
void

This method is deprecated.

void
void
onWindowDismissed(boolean p, boolean p1)
void
ActionMode
void
void
void
overridePendingTransition(int p, int p1)
void
void
void
void
void
void
boolean
void
removeDialog(int p)

This method is deprecated.

DragAndDropPermissions
void
requestPermissions(String[] p, int p1)
void
boolean

This method is deprecated.

boolean
T
<T extends View> requireViewById(int p)
void
void
void
void
void
void
void
void
void
setFeatureDrawableAlpha(int p, int p1)
void
setFeatureDrawableResource(int p, int p1)
void
void
void
setImmersive(boolean p)
void
void
void
void
void
void
setPersistent(boolean p)

This method is deprecated.

void
void
setProgress(int p)

This method is deprecated.

void

This method is deprecated.

void

This method is deprecated.

void

This method is deprecated.

void
void
void
setResult(int p)
void

This method is deprecated.

void
void
setShowWhenLocked(boolean p)
void
void
setTheme(int p)
void
void

This method is deprecated.

boolean
setTranslucent(boolean p)
void
setTurnScreenOn(boolean p)
void
setVisible(boolean p)
void
void
boolean
boolean
boolean
boolean
void
showDialog(int p)

This method is deprecated.

boolean
showDialog(int p, Bundle p1)

This method is deprecated.

void
ActionMode
void
startActivities(Intent[] p)
void
void
startActivityAsCaller(Intent p, Bundle p1, boolean p2, int p3)
void
void
void

This method is deprecated.

void

This method is deprecated.

boolean
void
startIntentSender(IntentSender p, Intent p1, int p2, int p3, int p4)
void
startIntentSenderForResultInner(
    IntentSender p,
    String p1,
    int p2,
    Intent p3,
    int p4,
    int p5,
    Bundle p6
)
void
startIntentSenderFromChild(
    Activity p,
    IntentSender p1,
    int p2,
    Intent p3,
    int p4,
    int p5,
    int p6
)

This method is deprecated.

void
void
void

This method is deprecated.

boolean
void
void
startSearch(String p, boolean p1, Bundle p2, boolean p3)
void
void
void

This method is deprecated.

void
takeKeyEvents(boolean p)
void
void
void
void
void
void
updateUiTranslationState(
    int p,
    TranslationSpec p1,
    TranslationSpec p2,
    List<AutofillId> p3,
    UiTranslationSpec p4
)
From androidx.activity.ComponentActivity
void
void
final void
final void
final void
final void
final void
final void
final ActivityResultRegistry
CreationExtras
ViewModelProvider.Factory
Object

This method is deprecated.

Lifecycle
final OnBackPressedDispatcher
final SavedStateRegistry
ViewModelStore
void
void

This method is deprecated.

void
boolean
onCreatePanelMenu(int featureId, Menu menu)
void
onMultiWindowModeChanged(boolean isInMultiWindowMode)

This method is deprecated.

void
void
onPanelClosed(int featureId, Menu menu)
void
onPictureInPictureModeChanged(boolean isInPictureInPictureMode)

This method is deprecated.

boolean
onPreparePanel(int featureId, View view, Menu menu)
Object

This method is deprecated.

final Object
void
void
onTrimMemory(int level)
Context
final ActivityResultLauncher<I>
<I, O> registerForActivityResult(
    ActivityResultContract<I, O> contract,
    ActivityResultRegistry registry,
    ActivityResultCallback<O> callback
)
void
final void
final void
final void
final void
final void
final void
void
void
setContentView(int layoutResID)
void
startActivityForResult(Intent intent, int requestCode)

This method is deprecated.

void
startIntentSenderForResult(
    IntentSender intent,
    int requestCode,
    Intent fillInIntent,
    int flagsMask,
    int flagsValues,
    int extraFlags
)

This method is deprecated.

From androidx.core.app.ComponentActivity
boolean
boolean
T
<T extends ComponentActivity.ExtraData> getExtraData(Class<T> extraDataClass)

This method is deprecated.

void

This method is deprecated.

final boolean
shouldDumpInternalState(String[] args)
boolean
From android.content.Context
From android.view.ContextThemeWrapper
From android.content.ContextWrapper
boolean
bindIsolatedService(
    Intent p,
    int p1,
    String p2,
    Executor p3,
    ServiceConnection p4
)
boolean
boolean
boolean
int
int
int[]
int
int
int[]
int
checkPermission(String p, int p1, int p2)
int
int
checkUriPermission(Uri p, int p1, int p2, int p3)
int[]
checkUriPermissions(List<Uri> p, int p1, int p2, int p3)
void

This method is deprecated.

Context
Context
Context
Context
Context
Context
Context
Context
Context
Context
Context
Context
Context
String[]
boolean
boolean
boolean
void
void
void
void
void
enforcePermission(String p, int p1, int p2, String p3)
void
enforceUriPermission(Uri p, int p1, int p2, int p3, String p4)
String[]
Context
ApplicationInfo
AttributionSource
String
AutofillOptions
Context
String
File
ClassLoader
File
ContentCaptureOptions
ContentResolver
File
File
File
File
getDir(String p, int p1)
Display
DisplayAdjustments
int
Display
File
File[]
File
File[]
File[]

This method is deprecated.

File
File
IApplicationThread
Executor
Looper
Handler
File
File
File[]
String
String
PackageManager
String
String
ContextParams
File
IServiceConnection
SharedPreferences
File
String
UserHandle
int
Drawable

This method is deprecated.

int

This method is deprecated.

int

This method is deprecated.

IBinder
void
grantUriPermission(String p, Uri p1, int p2)
boolean
boolean
boolean
boolean
boolean
boolean
boolean
FileInputStream
FileOutputStream
openFileOutput(String p, int p1)
SQLiteDatabase
Drawable

This method is deprecated.

Intent
Intent
registerReceiverAsUser(
    BroadcastReceiver p,
    UserHandle p1,
    IntentFilter p2,
    String p3,
    Handler p4
)
Intent
registerReceiverForAllUsers(
    BroadcastReceiver p,
    IntentFilter p1,
    String p2,
    Handler p3
)
void
void

This method is deprecated.

void

This method is deprecated.

void
void
revokeUriPermission(Uri p, int p1)
void
void
void
sendBroadcastAsUserMultiplePermissions(
    Intent p,
    UserHandle p1,
    String[] p2
)
void
void
void
sendOrderedBroadcastAsUser(
    Intent p,
    UserHandle p1,
    String p2,
    BroadcastReceiver p3,
    Handler p4,
    int p5,
    String p6,
    Bundle p7
)
void

This method is deprecated.

void

This method is deprecated.

void
sendStickyOrderedBroadcast(
    Intent p,
    BroadcastReceiver p1,
    Handler p2,
    int p3,
    String p4,
    Bundle p5
)

This method is deprecated.

void
sendStickyOrderedBroadcastAsUser(
    Intent p,
    UserHandle p1,
    BroadcastReceiver p2,
    Handler p3,
    int p4,
    String p5,
    Bundle p6
)

This method is deprecated.

void
void
void
void

This method is deprecated.

int
startActivitiesAsUser(Intent[] p, Bundle p1, UserHandle p2)
ComponentName
ComponentName
boolean
ComponentName
ComponentName
boolean
boolean
void
void
void
void
updateServiceGroup(ServiceConnection p, int p1, int p2)
From org.robolectric.internal.bytecode.ShadowedObject
abstract Object
From android.view.Window.Callback
void
onPointerCaptureChanged(boolean hasCapture)

Public constructors

FragmentActivity

Added in 1.1.0
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

Added in 1.1.0
@ContentView
public FragmentActivity(@LayoutRes 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

Public methods

dump

Added in 1.7.0-alpha02
public void dump(
    @NonNull String prefix,
    @Nullable FileDescriptor fd,
    @NonNull PrintWriter writer,
    @Nullable 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 String[] args

additional arguments to the dump request.

getSupportFragmentManager

Added in 1.1.0
public @NonNull FragmentManager getSupportFragmentManager()

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

getSupportLoaderManager

Added in 1.1.0
Deprecated in 1.1.0
public @NonNull LoaderManager getSupportLoaderManager()

onAttachFragment

Added in 1.1.0
Deprecated in 1.3.0
@MainThread
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.

onCreateView

Added in 1.7.0-alpha02
public @Nullable View onCreateView(
    @NonNull String name,
    @NonNull Context context,
    @NonNull AttributeSet attrs
)

onCreateView

Added in 1.7.0-alpha02
public @Nullable View onCreateView(
    @Nullable View parent,
    @NonNull String name,
    @NonNull Context context,
    @NonNull AttributeSet attrs
)

onMenuItemSelected

Added in 1.7.0-alpha02
public boolean onMenuItemSelected(int featureId, @NonNull MenuItem item)

onRequestPermissionsResult

Added in 1.7.0-alpha02
@CallSuper
public void onRequestPermissionsResult(
    int requestCode,
    @NonNull String[] permissions,
    @NonNull int[] grantResults
)

onStateNotSaved

Added in 1.1.0
public void onStateNotSaved()

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

setEnterSharedElementCallback

Added in 1.1.0
public void setEnterSharedElementCallback(@Nullable SharedElementCallback callback)

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

Parameters
@Nullable SharedElementCallback callback

Used to manipulate shared element transitions on the launched Activity.

setExitSharedElementCallback

Added in 1.1.0
public void setExitSharedElementCallback(@Nullable SharedElementCallback listener)

When makeSceneTransitionAnimation 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 FEATURE_CONTENT_TRANSITIONS.

Parameters
@Nullable SharedElementCallback listener

Used to manipulate shared element transitions on the launching Activity.

startActivityFromFragment

Added in 1.1.0
public void startActivityFromFragment(
    @NonNull Fragment fragment,
    @NonNull Intent intent,
    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.

int requestCode

The request code to be returned in onActivityResult 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

Added in 1.1.0
public void startActivityFromFragment(
    @NonNull Fragment fragment,
    @NonNull Intent intent,
    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.

int requestCode

The request code to be returned in onActivityResult 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 startActivity for more details. This value may be null.

startIntentSenderFromFragment

Added in 1.1.0
Deprecated in 1.3.0
public void startIntentSenderFromFragment(
    @NonNull Fragment fragment,
    @NonNull IntentSender intent,
    int requestCode,
    @Nullable Intent fillInIntent,
    int flagsMask,
    int flagsValues,
    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.

int requestCode

The request code to be returned in onActivityResult 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 sendIntent. This value may be null.

int flagsMask

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

int flagsValues

Desired values for any bits set in flagsMask.

int extraFlags

Always set to 0.

@Nullable Bundle options

Additional options for how the Activity should be started. See startActivity for more details. This value may be null.

Throws
android.content.IntentSender.SendIntentException

if the call fails to execute.

supportFinishAfterTransition

Added in 1.1.0
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

Added in 1.1.0
Deprecated in 1.1.0
public void supportInvalidateOptionsMenu()

Support library version of 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

Added in 1.1.0
public void supportPostponeEnterTransition()

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

supportStartPostponedEnterTransition

Added in 1.1.0
public void supportStartPostponedEnterTransition()

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

validateRequestPermissionsRequestCode

Added in 1.1.0
Deprecated in 1.3.0
public final void validateRequestPermissionsRequestCode(int requestCode)

Protected methods

onActivityResult

Added in 1.7.0-alpha02
@CallSuper
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data)

onCreate

Added in 1.7.0-alpha02
protected void onCreate(@Nullable Bundle savedInstanceState)

Perform initialization of all fragments.

onDestroy

Added in 1.7.0-alpha02
protected void onDestroy()

Destroy all fragments.

onPause

Added in 1.7.0-alpha02
protected void onPause()

Dispatch onPause() to fragments.

onPostResume

Added in 1.7.0-alpha02
protected void onPostResume()

Dispatch onResume() to fragments.

onResume

Added in 1.7.0-alpha02
protected void onResume()

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

Added in 1.1.0
protected void onResumeFragments()

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.

onStart

Added in 1.7.0-alpha02
protected void onStart()

Dispatch onStart() to all fragments.

onStop

Added in 1.7.0-alpha02
protected void onStop()

Dispatch onStop() to all fragments.