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

Class Overview

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.

Note: If you want to implement an activity that includes an action bar, you should instead use the ActionBarActivity class, which is a subclass of this one, so allows you to use Fragment APIs on API level 7 and higher.

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.

Summary

[Expand]
Inherited Constants
From class android.app.Activity
From class android.content.Context
From interface android.content.ComponentCallbacks2
[Expand]
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 onRetainCustomNonConfigurationInstance().
FragmentManager getSupportFragmentManager()
Return the FragmentManager for interacting with fragments associated with this activity.
LoaderManager getSupportLoaderManager()
final MediaControllerCompat getSupportMediaController()
Retrieves the current MediaControllerCompat for sending media key and volume events.
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(String name, Context context, AttributeSet attrs)
Standard implementation of onCreateView(String, Context, AttributeSet) used when inflating with the LayoutInflater returned by getSystemService(Class).
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).
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 onPanelClosed(int featureId, Menu menu)
Call onOptionsMenuClosed() on fragments.
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 onRetainNonConfigurationInstance().
final Object onRetainNonConfigurationInstance()
This method is deprecated. Use the new Fragment API setRetainInstance(boolean) instead; this is also available on older platforms through the Android compatibility package.
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.
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.
final void setSupportMediaController(MediaControllerCompat mediaController)
Sets a MediaControllerCompat for later retrieval via getSupportMediaController().
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 supportFinishAfterTransition()
Reverses the Activity Scene entry Transition and triggers the calling Activity to reverse its exit Transition.
void supportInvalidateOptionsMenu()
Support library version of invalidateOptionsMenu().
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.
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 onResume() that you can override to perform operations in the Activity at the same point where its fragments are resumed.
void onSaveInstanceState(Bundle outState)
Save all appropriate fragment state.
void onStart()
Dispatch onStart() to all fragments.
void onStop()
Dispatch onStop() to all fragments.
[Expand]
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

Public Constructors

public FragmentActivity ()

Public Methods

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 Desired prefix to prepend at each line of output.
fd The raw file descriptor that the dump is being sent to.
writer The PrintWriter to which you should dump your state. This will be closed for you after you return.
args additional arguments to the dump request.

public Object getLastCustomNonConfigurationInstance ()

Return the value previously returned from onRetainCustomNonConfigurationInstance().

Returns
Object

public FragmentManager getSupportFragmentManager ()

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

Returns
FragmentManager

public LoaderManager getSupportLoaderManager ()

Returns
LoaderManager

public final MediaControllerCompat getSupportMediaController ()

Retrieves the current MediaControllerCompat for sending media key and volume events.

Returns
MediaControllerCompat The controller which should receive events.

public void onAttachFragment (Fragment fragment)

Called when a fragment is attached to the activity.

Parameters
fragment

public void onBackPressed ()

Take care of popping the fragment back stack or finishing the activity as appropriate.

public void onConfigurationChanged (Configuration newConfig)

Dispatch configuration change to all fragments.

Parameters
newConfig The new device configuration.

public boolean onCreatePanelMenu (int featureId, Menu menu)

Dispatch to Fragment.onCreateOptionsMenu().

Parameters
featureId The panel being created.
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.

public 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 Tag name to be inflated.
context The context the view is being created in.
attrs Inflation attributes as specified in XML file.
Returns
View View Newly created view. Return null for the default behavior.

public 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 tags to embed fragments inside of the activity.

Parameters
parent The parent that the created view will be placed in; note that this may be null.
name Tag name to be inflated.
context The context the view is being created in.
attrs Inflation attributes as specified in XML file.
Returns
View View Newly created view. Return null for the default behavior.

public boolean onKeyDown (int keyCode, KeyEvent event)

Take care of calling onBackPressed() for pre-Eclair platforms.

Parameters
keyCode The value in event.getKeyCode().
event 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.

public void onLowMemory ()

Dispatch onLowMemory() to all fragments.

public boolean onMenuItemSelected (int featureId, MenuItem item)

Dispatch context and options menu to fragments.

Parameters
featureId The panel that the menu is in.
item 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).

public void onPanelClosed (int featureId, Menu menu)

Call onOptionsMenuClosed() on fragments.

Parameters
featureId The panel that is being displayed.
menu If onCreatePanelView() returned null, this is the Menu being displayed in the panel.

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

Dispatch onPrepareOptionsMenu() to fragments.

Parameters
featureId The panel that is being displayed.
view The View that was returned by onCreatePanelView().
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.

public 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 The request code passed in requestPermissions(String[], int).
permissions The requested permissions. Never null.
grantResults The grant results for the corresponding permissions which is either PERMISSION_GRANTED or PERMISSION_DENIED. Never null.

public Object onRetainCustomNonConfigurationInstance ()

Use this instead of onRetainNonConfigurationInstance(). Retrieve later with getLastCustomNonConfigurationInstance().

Returns
Object

public final Object onRetainNonConfigurationInstance ()

This method is deprecated.
Use the new Fragment API setRetainInstance(boolean) instead; this is also available on older platforms through the Android compatibility package.

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 Return any Object holding the desired state to propagate to the next activity instance.

public void onStateNotSaved ()

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

public 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 Used to manipulate shared element transitions on the launched Activity.

public 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 Used to manipulate shared element transitions on the launching Activity.

public final 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 The controller for the session which should receive media keys and volume changes on API 21 and later.

public 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 The intent to start.
requestCode If >= 0, this code will be returned in onActivityResult() when the activity exits.

public void startActivityFromFragment (Fragment fragment, Intent intent, int requestCode)

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

Parameters
fragment
intent
requestCode

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.

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.

public void supportPostponeEnterTransition ()

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

public void supportStartPostponedEnterTransition ()

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

public final void validateRequestPermissionsRequestCode (int requestCode)

Parameters
requestCode

Protected Methods

protected void onActivityResult (int requestCode, int resultCode, Intent data)

Dispatch incoming result to the correct fragment.

Parameters
requestCode The integer request code originally supplied to startActivityForResult(), allowing you to identify who this result came from.
resultCode The integer result code returned by the child activity through its setResult().
data An Intent, which can return result data to the caller (various data can be attached to Intent "extras").

protected void onCreate (Bundle savedInstanceState)

Perform initialization of all fragments and loaders.

Parameters
savedInstanceState 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.

protected void onDestroy ()

Destroy all fragments and loaders.

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 The new intent that was started for the activity.

protected void onPause ()

Dispatch onPause() to fragments.

protected void onPostResume ()

Dispatch onResume() to fragments.

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. 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().

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.

protected void onSaveInstanceState (Bundle outState)

Save all appropriate fragment state.

Parameters
outState Bundle in which to place your saved state.

protected void onStart ()

Dispatch onStart() to all fragments. Ensure any created loaders are now started.

protected void onStop ()

Dispatch onStop() to all fragments. Ensure all loaders are stopped.