FragmentActivity
public
class
FragmentActivity
extends ComponentActivity
implements
ActivityCompat.OnRequestPermissionsResultCallback
java.lang.Object | ||||||
↳ | android.content.Context | |||||
↳ | android.content.ContextWrapper | |||||
↳ | android.view.ContextThemeWrapper | |||||
↳ | android.app.Activity | |||||
↳ | 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
Inherited constants |
---|
Inherited fields |
---|
Public constructors | |
---|---|
FragmentActivity()
Default constructor for FragmentActivity. |
|
FragmentActivity(int contentLayoutId)
Alternate constructor that can be used to provide a default layout
that will be inflated as part of |
Public methods | |
---|---|
void
|
dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args)
Print the Activity's state into the given stream. |
FragmentManager
|
getSupportFragmentManager()
Return the FragmentManager for interacting with fragments associated with this activity. |
LoaderManager
|
getSupportLoaderManager()
This method is deprecated.
Use
|
void
|
onAttachFragment(Fragment fragment)
This method is deprecated.
The responsibility for listening for fragments being attached has been moved
to FragmentManager. You can add a listener to
|
void
|
onConfigurationChanged(Configuration newConfig)
Dispatch configuration change to all fragments. |
boolean
|
onCreatePanelMenu(int featureId, Menu menu)
Dispatch to Fragment.onCreateOptionsMenu(). |
View
|
onCreateView(View parent, String name, Context context, AttributeSet attrs)
|
View
|
onCreateView(String name, Context context, AttributeSet attrs)
|
void
|
onLowMemory()
Dispatch onLowMemory() to all fragments. |
boolean
|
onMenuItemSelected(int featureId, MenuItem item)
Dispatch context and options menu to fragments. |
void
|
onMultiWindowModeChanged(boolean isInMultiWindowMode)
Note: If you override this method you must call
|
void
|
onPanelClosed(int featureId, Menu menu)
Call onOptionsMenuClosed() on fragments. |
void
|
onPictureInPictureModeChanged(boolean isInPictureInPictureMode)
Note: If you override this method you must call
|
boolean
|
onPreparePanel(int featureId, View view, Menu menu)
Dispatch onPrepareOptionsMenu() to fragments. |
void
|
onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults)
This method is deprecated.
use
|
void
|
onStateNotSaved()
Hook in to note that fragment state is no longer saved. |
void
|
setEnterSharedElementCallback(SharedElementCallback callback)
When |
void
|
setExitSharedElementCallback(SharedElementCallback listener)
When |
void
|
startActivityFromFragment(Fragment fragment, Intent intent, int requestCode, Bundle options)
Called by Fragment.startActivityForResult() to implement its behavior. |
void
|
startActivityFromFragment(Fragment fragment, Intent intent, int requestCode)
Called by Fragment.startActivityForResult() to implement its behavior. |
void
|
startIntentSenderFromFragment(Fragment fragment, IntentSender intent, int requestCode, Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options)
This method is deprecated.
Fragments should use
|
void
|
supportFinishAfterTransition()
Reverses the Activity Scene entry Transition and triggers the calling Activity to reverse its exit Transition. |
void
|
supportInvalidateOptionsMenu()
This method is deprecated.
Call |
void
|
supportPostponeEnterTransition()
Support library version of |
void
|
supportStartPostponedEnterTransition()
Support library version of |
final
void
|
validateRequestPermissionsRequestCode(int requestCode)
This method is deprecated. there are no longer any restrictions on permissions requestCodes. |
Protected methods | |
---|---|
void
|
onActivityResult(int requestCode, int resultCode, Intent data)
This method is deprecated.
use
|
void
|
onCreate(Bundle savedInstanceState)
Perform initialization of all fragments. |
void
|
onDestroy()
Destroy all fragments. |
void
|
onNewIntent(Intent intent)
Handle onNewIntent() to inform the fragment manager that the state is not saved. |
void
|
onPause()
Dispatch onPause() to fragments. |
void
|
onPostResume()
Dispatch onResume() to fragments. |
void
|
onResume()
Dispatch onResume() to fragments. |
void
|
onResumeFragments()
This is the fragment-orientated version of |
void
|
onStart()
Dispatch onStart() to all fragments. |
void
|
onStop()
Dispatch onStop() to all fragments. |
Inherited methods | |
---|---|
Public constructors
FragmentActivity
public FragmentActivity ()
Default constructor for FragmentActivity. All Activities must have a default constructor
for API 27 and lower devices or when using the default
AppComponentFactory
.
FragmentActivity
public FragmentActivity (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
AppComponentFactory
.
Parameters | |
---|---|
contentLayoutId |
int |
See also:
Public methods
dump
public void dump (String prefix, FileDescriptor fd, PrintWriter writer, String[] args)
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 |
String : additional arguments to the dump request.
|
getSupportFragmentManager
public FragmentManager getSupportFragmentManager ()
Return the FragmentManager for interacting with fragments associated with this activity.
Returns | |
---|---|
FragmentManager |
getSupportLoaderManager
public LoaderManager getSupportLoaderManager ()
This method is deprecated.
Use
LoaderManager.getInstance(this)
.
Returns | |
---|---|
LoaderManager |
onAttachFragment
public void onAttachFragment (Fragment fragment)
This method is deprecated.
The responsibility for listening for fragments being attached has been moved
to FragmentManager. You can add a listener to
this Activity's FragmentManager
by calling
FragmentManager.addFragmentOnAttachListener(FragmentOnAttachListener)
in your constructor to get callbacks when a fragment is attached directly to
the activity's FragmentManager.
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
.
Parameters | |
---|---|
fragment |
Fragment |
onConfigurationChanged
public void onConfigurationChanged (Configuration newConfig)
Dispatch configuration change to all fragments.
Parameters | |
---|---|
newConfig |
Configuration |
onCreatePanelMenu
public boolean onCreatePanelMenu (int featureId, Menu menu)
Dispatch to Fragment.onCreateOptionsMenu().
Parameters | |
---|---|
featureId |
int |
menu |
Menu |
Returns | |
---|---|
boolean |
onCreateView
public View onCreateView (View parent, String name, Context context, AttributeSet attrs)
Parameters | |
---|---|
parent |
View |
name |
String |
context |
Context |
attrs |
AttributeSet |
Returns | |
---|---|
View |
onCreateView
public View onCreateView (String name, Context context, AttributeSet attrs)
Parameters | |
---|---|
name |
String |
context |
Context |
attrs |
AttributeSet |
Returns | |
---|---|
View |
onLowMemory
public void onLowMemory ()
Dispatch onLowMemory() to all fragments.
onMenuItemSelected
public boolean onMenuItemSelected (int featureId, MenuItem item)
Dispatch context and options menu to fragments.
Parameters | |
---|---|
featureId |
int |
item |
MenuItem |
Returns | |
---|---|
boolean |
onMultiWindowModeChanged
public void onMultiWindowModeChanged (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 | |
---|---|
isInMultiWindowMode |
boolean : True if the activity is in multi-window mode.
|
onPanelClosed
public void onPanelClosed (int featureId, Menu menu)
Call onOptionsMenuClosed() on fragments.
Parameters | |
---|---|
featureId |
int |
menu |
Menu |
onPictureInPictureModeChanged
public void onPictureInPictureModeChanged (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 | |
---|---|
isInPictureInPictureMode |
boolean : True if the activity is in picture-in-picture mode.
|
onPreparePanel
public boolean onPreparePanel (int featureId, View view, Menu menu)
Dispatch onPrepareOptionsMenu() to fragments.
Parameters | |
---|---|
featureId |
int |
view |
View |
menu |
Menu |
Returns | |
---|---|
boolean |
onRequestPermissionsResult
public void onRequestPermissionsResult (int requestCode, String[] permissions, int[] grantResults)
This method is deprecated.
use
registerForActivityResult(ActivityResultContract, ActivityResultCallback)
passing
in a ActivityResultContracts.RequestMultiplePermissions
object for the ActivityResultContract
and
handling the result in the callback
.
Parameters | |
---|---|
requestCode |
int |
permissions |
String |
grantResults |
int |
onStateNotSaved
public void onStateNotSaved ()
Hook in to note that fragment state is no longer saved.
setEnterSharedElementCallback
public void setEnterSharedElementCallback (SharedElementCallback callback)
When 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
public void setExitSharedElementCallback (SharedElementCallback listener)
When 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.
|
startActivityFromFragment
public void startActivityFromFragment (Fragment fragment, Intent intent, int requestCode, Bundle options)
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.
|
startActivityFromFragment
public void startActivityFromFragment (Fragment fragment, Intent intent, int requestCode)
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.
|
startIntentSenderFromFragment
public void startIntentSenderFromFragment (Fragment fragment, IntentSender intent, int requestCode, Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options)
This method is deprecated.
Fragments should use
Fragment.registerForActivityResult(ActivityResultContract, ActivityResultCallback)
with the ActivityResultContracts.StartIntentSenderForResult
contract. This method will still be called when
Fragments call the deprecated startIntentSenderForResult()
method.
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 | |
---|---|
|
if the call fails to execute. |
IntentSender.SendIntentException |
supportFinishAfterTransition
public void supportFinishAfterTransition ()
Reverses the Activity Scene entry Transition and triggers the calling Activity
to reverse its exit Transition. When the exit Transition completes,
Activity.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
public void supportInvalidateOptionsMenu ()
This method is 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
public void supportPostponeEnterTransition ()
Support library version of Activity.postponeEnterTransition()
that works
only on API 21 and later.
supportStartPostponedEnterTransition
public void supportStartPostponedEnterTransition ()
Support library version of Activity.startPostponedEnterTransition()
that only works with API 21 and later.
validateRequestPermissionsRequestCode
public final void validateRequestPermissionsRequestCode (int requestCode)
This method is deprecated.
there are no longer any restrictions on permissions requestCodes.
Parameters | |
---|---|
requestCode |
int |
Protected methods
onActivityResult
protected void onActivityResult (int requestCode, int resultCode, Intent data)
This method is deprecated.
use
registerForActivityResult(ActivityResultContract, ActivityResultCallback)
with the appropriate ActivityResultContract
and handling the result in the
callback
.
Parameters | |
---|---|
requestCode |
int |
resultCode |
int |
data |
Intent |
onCreate
protected void onCreate (Bundle savedInstanceState)
Perform initialization of all fragments.
Parameters | |
---|---|
savedInstanceState |
Bundle |
onDestroy
protected void onDestroy ()
Destroy all fragments.
onNewIntent
protected void onNewIntent (Intent intent)
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.
Parameters | |
---|---|
intent |
Intent |
onPause
protected void onPause ()
Dispatch onPause() to fragments.
onPostResume
protected void onPostResume ()
Dispatch onResume() to fragments.
onResume
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
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
protected void onStart ()
Dispatch onStart() to all fragments.
onStop
protected void onStop ()
Dispatch onStop() to all fragments.