public abstract class

FragmentManager

extends Object
java.lang.Object
   ↳ android.app.FragmentManager

Class Overview

Interface for interacting with Fragment objects inside of an Activity

Summary

Nested Classes
interface FragmentManager.BackStackEntry Representation of an entry on the fragment back stack, as created with FragmentTransaction.addToBackStack()
interface FragmentManager.OnBackStackChangedListener Interface to watch for changes to the back stack. 
Constants
int POP_BACK_STACK_INCLUSIVE Flag for popBackStack(String, int) and popBackStack(int, int): If set, and the name or ID of a back stack entry has been supplied, then all matching entries will be consumed until one that doesn't match is found or the bottom of the stack is reached.
Public Constructors
FragmentManager()
Public Methods
abstract void addOnBackStackChangedListener(FragmentManager.OnBackStackChangedListener listener)
Add a new listener for changes to the fragment back stack.
abstract FragmentTransaction beginTransaction()
Start a series of edit operations on the Fragments associated with this FragmentManager.
abstract void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args)
Print the FragmentManager's state into the given stream.
static void enableDebugLogging(boolean enabled)
Control whether the framework's internal fragment manager debugging logs are turned on.
abstract boolean executePendingTransactions()
After a FragmentTransaction is committed with FragmentTransaction.commit(), it is scheduled to be executed asynchronously on the process's main thread.
abstract Fragment findFragmentById(int id)
Finds a fragment that was identified by the given id either when inflated from XML or as the container ID when added in a transaction.
abstract Fragment findFragmentByTag(String tag)
Finds a fragment that was identified by the given tag either when inflated from XML or as supplied when added in a transaction.
abstract FragmentManager.BackStackEntry getBackStackEntryAt(int index)
Return the BackStackEntry at index index in the back stack; entries start index 0 being the bottom of the stack.
abstract int getBackStackEntryCount()
Return the number of entries currently in the back stack.
abstract Fragment getFragment(Bundle bundle, String key)
Retrieve the current Fragment instance for a reference previously placed with putFragment(Bundle, String, Fragment).
abstract void popBackStack()
Pop the top state off the back stack.
abstract void popBackStack(String name, int flags)
Pop the last fragment transition from the manager's fragment back stack.
abstract void popBackStack(int id, int flags)
Pop all back stack states up to the one with the given identifier.
abstract boolean popBackStackImmediate(int id, int flags)
Like popBackStack(int, int), but performs the operation immediately inside of the call.
abstract boolean popBackStackImmediate(String name, int flags)
Like popBackStack(String, int), but performs the operation immediately inside of the call.
abstract boolean popBackStackImmediate()
Like popBackStack(), but performs the operation immediately inside of the call.
abstract void putFragment(Bundle bundle, String key, Fragment fragment)
Put a reference to a fragment in a Bundle.
abstract void removeOnBackStackChangedListener(FragmentManager.OnBackStackChangedListener listener)
Remove a listener that was previously added with addOnBackStackChangedListener(OnBackStackChangedListener).
[Expand]
Inherited Methods
From class java.lang.Object

Constants

public static final int POP_BACK_STACK_INCLUSIVE

Since: API Level 11

Flag for popBackStack(String, int) and popBackStack(int, int): If set, and the name or ID of a back stack entry has been supplied, then all matching entries will be consumed until one that doesn't match is found or the bottom of the stack is reached. Otherwise, all entries up to but not including that entry will be removed.

Constant Value: 1 (0x00000001)

Public Constructors

public FragmentManager ()

Since: API Level 11

Public Methods

public abstract void addOnBackStackChangedListener (FragmentManager.OnBackStackChangedListener listener)

Since: API Level 11

Add a new listener for changes to the fragment back stack.

public abstract FragmentTransaction beginTransaction ()

Since: API Level 11

Start a series of edit operations on the Fragments associated with this FragmentManager.

Note: A fragment transaction can only be created/committed prior to an activity saving its state. If you try to commit a transaction after Activity.onSaveInstanceState() (and prior to a following Activity.onStart or Activity.onResume(), you will get an error. This is because the framework takes care of saving your current fragments in the state, and if changes are made after the state is saved then they will be lost.

public abstract void dump (String prefix, FileDescriptor fd, PrintWriter writer, String[] args)

Since: API Level 11

Print the FragmentManager's state into the given stream.

Parameters
prefix Text to print at the front of each line.
fd The raw file descriptor that the dump is being sent to.
writer A PrintWriter to which the dump is to be set.
args Additional arguments to the dump request.

public static void enableDebugLogging (boolean enabled)

Since: API Level 11

Control whether the framework's internal fragment manager debugging logs are turned on. If enabled, you will see output in logcat as the framework performs fragment operations.

public abstract boolean executePendingTransactions ()

Since: API Level 11

After a FragmentTransaction is committed with FragmentTransaction.commit(), it is scheduled to be executed asynchronously on the process's main thread. If you want to immediately executing any such pending operations, you can call this function (only from the main thread) to do so. Note that all callbacks and other related behavior will be done from within this call, so be careful about where this is called from.

Returns
  • Returns true if there were any pending transactions to be executed.

public abstract Fragment findFragmentById (int id)

Since: API Level 11

Finds a fragment that was identified by the given id either when inflated from XML or as the container ID when added in a transaction. This first searches through fragments that are currently added to the manager's activity; if no such fragment is found, then all fragments currently on the back stack associated with this ID are searched.

Returns
  • The fragment if found or null otherwise.

public abstract Fragment findFragmentByTag (String tag)

Since: API Level 11

Finds a fragment that was identified by the given tag either when inflated from XML or as supplied when added in a transaction. This first searches through fragments that are currently added to the manager's activity; if no such fragment is found, then all fragments currently on the back stack are searched.

Returns
  • The fragment if found or null otherwise.

public abstract FragmentManager.BackStackEntry getBackStackEntryAt (int index)

Since: API Level 11

Return the BackStackEntry at index index in the back stack; entries start index 0 being the bottom of the stack.

public abstract int getBackStackEntryCount ()

Since: API Level 11

Return the number of entries currently in the back stack.

public abstract Fragment getFragment (Bundle bundle, String key)

Since: API Level 11

Retrieve the current Fragment instance for a reference previously placed with putFragment(Bundle, String, Fragment).

Parameters
bundle The bundle from which to retrieve the fragment reference.
key The name of the entry in the bundle.
Returns
  • Returns the current Fragment instance that is associated with the given reference.

public abstract void popBackStack ()

Since: API Level 11

Pop the top state off the back stack. Returns true if there was one to pop, else false. This function is asynchronous -- it enqueues the request to pop, but the action will not be performed until the application returns to its event loop.

public abstract void popBackStack (String name, int flags)

Since: API Level 11

Pop the last fragment transition from the manager's fragment back stack. If there is nothing to pop, false is returned. This function is asynchronous -- it enqueues the request to pop, but the action will not be performed until the application returns to its event loop.

Parameters
name If non-null, this is the name of a previous back state to look for; if found, all states up to that state will be popped. The POP_BACK_STACK_INCLUSIVE flag can be used to control whether the named state itself is popped. If null, only the top state is popped.
flags Either 0 or POP_BACK_STACK_INCLUSIVE.

public abstract void popBackStack (int id, int flags)

Since: API Level 11

Pop all back stack states up to the one with the given identifier. This function is asynchronous -- it enqueues the request to pop, but the action will not be performed until the application returns to its event loop.

Parameters
id Identifier of the stated to be popped. If no identifier exists, false is returned. The identifier is the number returned by FragmentTransaction.commit(). The POP_BACK_STACK_INCLUSIVE flag can be used to control whether the named state itself is popped.
flags Either 0 or POP_BACK_STACK_INCLUSIVE.

public abstract boolean popBackStackImmediate (int id, int flags)

Since: API Level 11

Like popBackStack(int, int), but performs the operation immediately inside of the call. This is like calling executePendingTransactions() afterwards.

Returns
  • Returns true if there was something popped, else false.

public abstract boolean popBackStackImmediate (String name, int flags)

Since: API Level 11

Like popBackStack(String, int), but performs the operation immediately inside of the call. This is like calling executePendingTransactions() afterwards.

Returns
  • Returns true if there was something popped, else false.

public abstract boolean popBackStackImmediate ()

Since: API Level 11

Like popBackStack(), but performs the operation immediately inside of the call. This is like calling executePendingTransactions() afterwards.

Returns
  • Returns true if there was something popped, else false.

public abstract void putFragment (Bundle bundle, String key, Fragment fragment)

Since: API Level 11

Put a reference to a fragment in a Bundle. This Bundle can be persisted as saved state, and when later restoring getFragment(Bundle, String) will return the current instance of the same fragment.

Parameters
bundle The bundle in which to put the fragment reference.
key The name of the entry in the bundle.
fragment The Fragment whose reference is to be stored.

public abstract void removeOnBackStackChangedListener (FragmentManager.OnBackStackChangedListener listener)

Since: API Level 11

Remove a listener that was previously added with addOnBackStackChangedListener(OnBackStackChangedListener).