Android APIs
public class

FragmentActivity

extends Activity
java.lang.Object
   ↳ android.content.Context
     ↳ android.content.ContextWrapper
       ↳ android.view.ContextThemeWrapper
         ↳ android.app.Activity
           ↳ android.support.v4.app.FragmentActivity
Known Direct 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()
Return the LoaderManager for this fragment, creating it if needed.
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)
Add support for inflating the <fragment> tag.
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.
Object onRetainCustomNonConfigurationInstance()
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 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 supportInvalidateOptionsMenu()
Support library version of invalidateOptionsMenu().
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.content.ComponentCallbacks
From interface android.content.ComponentCallbacks2
From interface android.view.KeyEvent.Callback
From interface android.view.LayoutInflater.Factory
From interface android.view.LayoutInflater.Factory2
From interface android.view.View.OnCreateContextMenuListener
From interface android.view.Window.Callback

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

public FragmentManager getSupportFragmentManager ()

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

public LoaderManager getSupportLoaderManager ()

Return the LoaderManager for this fragment, creating it if needed.

public void onAttachFragment (Fragment fragment)

Called when a fragment is attached to the activity.

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

Add support for inflating the <fragment> tag.

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 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
  • 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 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 You must return true for the panel to be displayed; if you return false it will not be shown.

public Object onRetainCustomNonConfigurationInstance ()

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

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.

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.

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.