public
class
FragmentActivity
extends Activity
implements
ActivityCompat.OnRequestPermissionsResultCallback,
ActivityCompatApi23.RequestPermissionsRequestCodeValidator
java.lang.Object | |||||
↳ | android.content.Context | ||||
↳ | android.content.ContextWrapper | ||||
↳ | android.view.ContextThemeWrapper | ||||
↳ | android.app.Activity | ||||
↳ | android.support.v4.app.FragmentActivity |
Known Direct Subclasses |
Known Indirect Subclasses |
Base class for activities that want to use the support-based
Fragment
and
Loader
APIs.
When using this class as opposed to new platform's built-in fragment
and loader support, you must use the getSupportFragmentManager()
and getSupportLoaderManager()
methods respectively to access
those features.
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>
.
Prior to Honeycomb (3.0), an activity's state was saved before pausing. Fragments are a significant amount of new state, and dynamic enough that one often wants them to change between pausing and stopping. These classes throw an exception if you try to change the fragment state after it has been saved, to avoid accidental loss of UI state. However this is too restrictive prior to Honeycomb, where the state is saved before pausing. To address this, when running on platforms prior to Honeycomb an exception will not be thrown if you change fragments between the state save and the activity being stopped. This means that in some cases if the activity is restored from its last saved state, this may be a snapshot slightly before what the user last saw.
Inherited constants |
---|
From
class
android.app.Activity
|
From
class
android.content.Context
|
From
interface
android.content.ComponentCallbacks2
|
Inherited fields |
---|
From
class
android.app.Activity
|
Public constructors | |
---|---|
FragmentActivity()
|
Public methods | |
---|---|
void
|
dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args)
Print the Activity's state into the given stream. |
Object
|
getLastCustomNonConfigurationInstance()
Return the value previously returned from
|
FragmentManager
|
getSupportFragmentManager()
Return the FragmentManager for interacting with fragments associated with this activity. |
LoaderManager
|
getSupportLoaderManager()
|
final
MediaControllerCompat
|
getSupportMediaController()
Retrieves the current |
void
|
onAttachFragment(Fragment fragment)
Called when a fragment is attached to the activity. |
void
|
onBackPressed()
Take care of popping the fragment back stack or finishing the activity as appropriate. |
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)
Standard implementation of
|
View
|
onCreateView(String name, Context context, AttributeSet attrs)
Standard implementation of
|
boolean
|
onKeyDown(int keyCode, KeyEvent event)
Take care of calling onBackPressed() for pre-Eclair platforms. |
void
|
onLowMemory()
Dispatch onLowMemory() to all fragments. |
boolean
|
onMenuItemSelected(int featureId, MenuItem item)
Dispatch context and options menu to fragments. |
void
|
onMultiWindowModeChanged(boolean isInMultiWindowMode)
Called by the system when the activity changes from fullscreen mode to multi-window mode and visa-versa. Note: If you override this method you must call
|
void
|
onPanelClosed(int featureId, Menu menu)
Call onOptionsMenuClosed() on fragments. |
void
|
onPictureInPictureModeChanged(boolean isInPictureInPictureMode)
Called by the system when the activity changes to and from picture-in-picture mode. 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)
Callback for the result from requesting permissions. |
Object
|
onRetainCustomNonConfigurationInstance()
Use this instead of |
final
Object
|
onRetainNonConfigurationInstance()
Retain all appropriate fragment and loader state. |
void
|
onStateNotSaved()
Hook in to note that fragment state is no longer saved. |
void
|
setEnterSharedElementCallback(SharedElementCallback callback)
When |
void
|
setExitSharedElementCallback(SharedElementCallback listener)
When |
final
void
|
setSupportMediaController(MediaControllerCompat mediaController)
Sets a |
void
|
startActivityForResult(Intent intent, int requestCode, Bundle options)
Launch an activity for which you would like a result when it finished. |
void
|
startActivityForResult(Intent intent, int requestCode)
Modifies the standard behavior to allow results to be delivered to fragments. |
void
|
startActivityFromFragment(Fragment fragment, Intent intent, int requestCode)
Called by Fragment.startActivityForResult() to implement its behavior. |
void
|
startActivityFromFragment(Fragment fragment, Intent intent, int requestCode, Bundle options)
Called by Fragment.startActivityForResult() to implement its behavior. |
void
|
startIntentSenderForResult(IntentSender intent, int requestCode, Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
Same as calling |
void
|
startIntentSenderForResult(IntentSender intent, int requestCode, Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options)
Like |
void
|
startIntentSenderFromFragment(Fragment fragment, IntentSender intent, int requestCode, Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options)
Called by Fragment.startIntentSenderForResult() to implement its behavior. |
void
|
supportFinishAfterTransition()
Reverses the Activity Scene entry Transition and triggers the calling Activity to reverse its exit Transition. |
void
|
supportInvalidateOptionsMenu()
Support library version of |
void
|
supportPostponeEnterTransition()
Support library version of |
void
|
supportStartPostponedEnterTransition()
Support library version of |
final
void
|
validateRequestPermissionsRequestCode(int requestCode)
|
Protected methods | |
---|---|
void
|
onActivityResult(int requestCode, int resultCode, Intent data)
Dispatch incoming result to the correct fragment. |
void
|
onCreate(Bundle savedInstanceState)
Perform initialization of all fragments and loaders. |
void
|
onDestroy()
Destroy all fragments and loaders. |
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
|
onSaveInstanceState(Bundle outState)
Save all appropriate fragment state. |
void
|
onStart()
Dispatch onStart() to all fragments. |
void
|
onStop()
Dispatch onStop() to all fragments. |
Inherited methods | |
---|---|
From
class
android.app.Activity
| |
From
class
android.view.ContextThemeWrapper
| |
From
class
android.content.ContextWrapper
| |
From
class
android.content.Context
| |
From
class
java.lang.Object
| |
From
interface
android.view.LayoutInflater.Factory2
| |
From
interface
android.view.Window.Callback
| |
From
interface
android.view.KeyEvent.Callback
| |
From
interface
android.view.View.OnCreateContextMenuListener
| |
From
interface
android.content.ComponentCallbacks2
| |
From
interface
android.support.v4.app.ActivityCompat.OnRequestPermissionsResultCallback
| |
From
interface
android.support.v4.app.ActivityCompatApi23.RequestPermissionsRequestCodeValidator
| |
From
interface
android.view.LayoutInflater.Factory
| |
From
interface
android.content.ComponentCallbacks
|
FragmentActivity ()
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.
|
Object getLastCustomNonConfigurationInstance ()
Return the value previously returned from
onRetainCustomNonConfigurationInstance()
.
Returns | |
---|---|
Object |
FragmentManager getSupportFragmentManager ()
Return the FragmentManager for interacting with fragments associated with this activity.
Returns | |
---|---|
FragmentManager |
MediaControllerCompat getSupportMediaController ()
Retrieves the current MediaControllerCompat
for sending media key and volume events.
Returns | |
---|---|
MediaControllerCompat |
The controller which should receive events. |
void onAttachFragment (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
.
Parameters | |
---|---|
fragment |
Fragment
|
void onBackPressed ()
Take care of popping the fragment back stack or finishing the activity as appropriate.
void onConfigurationChanged (Configuration newConfig)
Dispatch configuration change to all fragments.
Parameters | |
---|---|
newConfig |
Configuration :
The new device configuration.
|
boolean onCreatePanelMenu (int featureId, Menu menu)
Dispatch to Fragment.onCreateOptionsMenu().
Parameters | |
---|---|
featureId |
int :
The panel being created. |
menu |
Menu :
The menu inside the panel. |
Returns | |
---|---|
boolean |
boolean You must return true for the panel to be displayed; if you return false it will not be shown. |
View onCreateView (View parent, String name, Context context, AttributeSet attrs)
Standard implementation of
onCreateView(View, String, Context, AttributeSet)
used when inflating with the LayoutInflater returned by getSystemService(Class
.
This implementation handles
Parameters | |
---|---|
parent |
View :
The parent that the created view will be placed
in; note that this may be null. |
name |
String :
Tag name to be inflated. |
context |
Context :
The context the view is being created in. |
attrs |
AttributeSet :
Inflation attributes as specified in XML file. |
Returns | |
---|---|
View |
View Newly created view. Return null for the default behavior. |
View onCreateView (String name, Context context, AttributeSet attrs)
Standard implementation of
onCreateView(String, Context, AttributeSet)
used when
inflating with the LayoutInflater returned by getSystemService(Class
.
This implementation does nothing and is for
pre-HONEYCOMB
apps. Newer apps
should use onCreateView(View, String, Context, AttributeSet)
.
Parameters | |
---|---|
name |
String :
Tag name to be inflated. |
context |
Context :
The context the view is being created in. |
attrs |
AttributeSet :
Inflation attributes as specified in XML file. |
Returns | |
---|---|
View |
View Newly created view. Return null for the default behavior. |
boolean onKeyDown (int keyCode, KeyEvent event)
Take care of calling onBackPressed() for pre-Eclair platforms.
Parameters | |
---|---|
keyCode |
int :
The value in event.getKeyCode(). |
event |
KeyEvent :
Description of the key event. |
Returns | |
---|---|
boolean |
Return true to prevent this event from being propagated
further, or false to indicate that you have not handled
this event and it should continue to be propagated. |
void onLowMemory ()
Dispatch onLowMemory() to all fragments.
boolean onMenuItemSelected (int featureId, MenuItem item)
Dispatch context and options menu to fragments.
Parameters | |
---|---|
featureId |
int :
The panel that the menu is in. |
item |
MenuItem :
The menu item that was selected. |
Returns | |
---|---|
boolean |
boolean Return true to finish processing of selection, or false to perform the normal menu handling (calling its Runnable or sending a Message to its target Handler). |
void onMultiWindowModeChanged (boolean isInMultiWindowMode)
Called by the system when the activity changes from fullscreen mode to multi-window mode and visa-versa.
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.
|
void onPanelClosed (int featureId, Menu menu)
Call onOptionsMenuClosed() on fragments.
Parameters | |
---|---|
featureId |
int :
The panel that is being displayed. |
menu |
Menu :
If onCreatePanelView() returned null, this is the Menu
being displayed in the panel.
|
void onPictureInPictureModeChanged (boolean isInPictureInPictureMode)
Called by the system when the activity changes to and from picture-in-picture mode.
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.
|
boolean onPreparePanel (int featureId, View view, Menu menu)
Dispatch onPrepareOptionsMenu() to fragments.
Parameters | |
---|---|
featureId |
int :
The panel that is being displayed. |
view |
View :
The View that was returned by onCreatePanelView(). |
menu |
Menu :
If onCreatePanelView() returned null, this is the Menu
being displayed in the panel. |
Returns | |
---|---|
boolean |
boolean You must return true for the panel to be displayed; if you return false it will not be shown. |
void onRequestPermissionsResult (int requestCode, String[] permissions, int[] grantResults)
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 |
String :
The requested permissions. Never null. |
grantResults |
int :
The grant results for the corresponding permissions
which is either PERMISSION_GRANTED
or PERMISSION_DENIED . Never null. |
See also:
Object onRetainCustomNonConfigurationInstance ()
Use this instead of onRetainNonConfigurationInstance()
.
Retrieve later with getLastCustomNonConfigurationInstance()
.
Returns | |
---|---|
Object |
Object onRetainNonConfigurationInstance ()
Retain all appropriate fragment and loader state. You can NOT
override this yourself! Use onRetainCustomNonConfigurationInstance()
if you want to retain your own state.
Returns | |
---|---|
Object |
any Object holding the desired state to propagate to the next activity instance |
void onStateNotSaved ()
Hook in to note that fragment state is no longer saved.
void setEnterSharedElementCallback (SharedElementCallback callback)
When 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
FEATURE_CONTENT_TRANSITIONS
.
Parameters | |
---|---|
callback |
SharedElementCallback :
Used to manipulate shared element transitions on the launched Activity.
|
void setExitSharedElementCallback (SharedElementCallback listener)
When 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 FEATURE_CONTENT_TRANSITIONS
.
Parameters | |
---|---|
listener |
SharedElementCallback :
Used to manipulate shared element transitions on the launching Activity.
|
void setSupportMediaController (MediaControllerCompat mediaController)
Sets a MediaControllerCompat
for later retrieval via
getSupportMediaController()
.
On API 21 and later, this controller will be tied to the window of the activity and
media key and volume events which are received while the Activity is in the foreground
will be forwarded to the controller and used to invoke transport controls or adjust the
volume. Prior to API 21, the global handling of media key and volume events through an
active MediaSessionCompat
and media button receiver
will still be respected.
Parameters | |
---|---|
mediaController |
MediaControllerCompat :
The controller for the session which should receive
media keys and volume changes on API 21 and later. |
void startActivityForResult (Intent intent, int requestCode, Bundle options)
Launch an activity for which you would like a result when it finished.
When this activity exits, your
onActivityResult() method will be called with the given requestCode.
Using a negative requestCode is the same as calling
startActivity(Intent)
(the activity is not launched as a sub-activity).
Note that this method should only be used with Intent protocols
that are defined to return a result. In other protocols (such as
ACTION_MAIN
or ACTION_VIEW
), you may
not get the result when you expect. For example, if the activity you
are launching uses the singleTask launch mode, it will not run in your
task and thus you will immediately receive a cancel result.
As a special case, if you call startActivityForResult() with a requestCode >= 0 during the initial onCreate(Bundle savedInstanceState)/onResume() of your activity, then your window will not be displayed until a result is returned back from the started activity. This is to avoid visible flickering when redirecting to another activity.
This method throws ActivityNotFoundException
if there was no Activity found to run the given Intent.
Parameters | |
---|---|
intent |
Intent :
The intent to start. |
requestCode |
int :
If >= 0, this code will be returned in
onActivityResult() when the activity exits. |
options |
Bundle :
Additional options for how the Activity should be started.
See Context.startActivity(Intent, Bundle) for more details. |
void startActivityForResult (Intent intent, int requestCode)
Modifies the standard behavior to allow results to be delivered to fragments. This imposes a restriction that requestCode be <= 0xffff.
Parameters | |
---|---|
intent |
Intent :
The intent to start. |
requestCode |
int :
If >= 0, this code will be returned in
onActivityResult() when the activity exits. |
void startActivityFromFragment (Fragment fragment, Intent intent, int requestCode)
Called by Fragment.startActivityForResult() to implement its behavior.
Parameters | |
---|---|
fragment |
Fragment
|
intent |
Intent
|
requestCode |
int
|
void startActivityFromFragment (Fragment fragment, Intent intent, int requestCode, Bundle options)
Called by Fragment.startActivityForResult() to implement its behavior.
Parameters | |
---|---|
fragment |
Fragment
|
intent |
Intent
|
requestCode |
int
|
options |
Bundle
|
void startIntentSenderForResult (IntentSender intent, int requestCode, Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
Same as calling startIntentSenderForResult(IntentSender, int, Intent, int, int, int, Bundle)
with no options.
Parameters | |
---|---|
intent |
IntentSender :
The IntentSender to launch. |
requestCode |
int :
If >= 0, this code will be returned in
onActivityResult() when the activity exits. |
fillInIntent |
Intent :
If non-null, this will be provided as the
intent parameter to sendIntent(Context, int, Intent, IntentSender.OnFinished, Handler) . |
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.
|
Throws | |
---|---|
IntentSender.SendIntentException |
void startIntentSenderForResult (IntentSender intent, int requestCode, Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options)
Like startActivityForResult(Intent, int)
, but allowing you
to use a IntentSender to describe the activity to be started. If
the IntentSender is for an activity, that activity will be started
as if you had called the regular startActivityForResult(Intent, int)
here; otherwise, its associated action will be executed (such as
sending a broadcast) as if you had called
IntentSender.sendIntent
on it.
Parameters | |
---|---|
intent |
IntentSender :
The IntentSender to launch. |
requestCode |
int :
If >= 0, this code will be returned in
onActivityResult() when the activity exits. |
fillInIntent |
Intent :
If non-null, this will be provided as the
intent parameter to sendIntent(Context, int, Intent, IntentSender.OnFinished, Handler) . |
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. If options
have also been supplied by the IntentSender, options given here will
override any that conflict with those given by the IntentSender.
|
Throws | |
---|---|
IntentSender.SendIntentException |
void startIntentSenderFromFragment (Fragment fragment, IntentSender intent, int requestCode, Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options)
Called by Fragment.startIntentSenderForResult() to implement its behavior.
Parameters | |
---|---|
fragment |
Fragment
|
intent |
IntentSender
|
requestCode |
int
|
fillInIntent |
Intent
|
flagsMask |
int
|
flagsValues |
int
|
extraFlags |
int
|
options |
Bundle
|
Throws | |
---|---|
IntentSender.SendIntentException |
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.
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.
void supportPostponeEnterTransition ()
Support library version of postponeEnterTransition()
that works
only on API 21 and later.
void supportStartPostponedEnterTransition ()
Support library version of startPostponedEnterTransition()
that only works with API 21 and later.
void validateRequestPermissionsRequestCode (int requestCode)
Parameters | |
---|---|
requestCode |
int
|
void onActivityResult (int requestCode, int resultCode, Intent data)
Dispatch incoming result to the correct fragment.
Parameters | |
---|---|
requestCode |
int :
The integer request code originally supplied to
startActivityForResult(), allowing you to identify who this
result came from. |
resultCode |
int :
The integer result code returned by the child activity
through its setResult(). |
data |
Intent :
An Intent, which can return result data to the caller
(various data can be attached to Intent "extras"). |
void onCreate (Bundle savedInstanceState)
Perform initialization of all fragments and loaders.
Parameters | |
---|---|
savedInstanceState |
Bundle :
If the activity is being re-initialized after
previously being shut down then this Bundle contains the data it most
recently supplied in onSaveInstanceState(Bundle) . Note: Otherwise it is null. |
void onDestroy ()
Destroy all fragments and loaders.
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 :
The new intent that was started for the activity. |
void onPause ()
Dispatch onPause() to fragments.
void onPostResume ()
Dispatch onResume() to fragments.
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. This means
that in some cases the previous state may still be saved, not allowing
fragment transactions that modify the state. To correctly interact
with fragments in their proper state, you should instead override
onResumeFragments()
.
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.
void onSaveInstanceState (Bundle outState)
Save all appropriate fragment state.
Parameters | |
---|---|
outState |
Bundle :
Bundle in which to place your saved state. |
void onStart ()
Dispatch onStart() to all fragments. Ensure any created loaders are now started.
void onStop ()
Dispatch onStop() to all fragments. Ensure all loaders are stopped.