Google is committed to advancing racial equity for Black communities. See how.

FragmentTransaction

abstract class FragmentTransaction
kotlin.Any
   ↳ androidx.fragment.app.FragmentTransaction

Static library support version of the framework's android.app.FragmentTransaction. Used to write apps that run on platforms prior to Android 3.0. When running on Android 3.0 or above, this implementation is still used; it does not try to switch to the framework's implementation. See the framework SDK documentation for a class overview.

Summary

Constants
static Int

Bit mask that is set for all enter transitions.

static Int

Bit mask that is set for all exit transitions.

static Int

Fragment is being removed from the stack

static Int

Fragment should simply fade in or out; that is, no strong navigation associated with it except that it is appearing or disappearing for some reason.

static Int

Fragment is being added onto the stack

static Int

No animation for transition.

static Int

Not set up for a transition.

Public constructors

Public methods
FragmentTransaction
add(@NonNull fragmentClass: Class<out Fragment!>, @Nullable args: Bundle?, @Nullable tag: String?)

Calls add(int, Class, Bundle, String) with a 0 containerViewId.

open FragmentTransaction
add(@NonNull fragment: Fragment, @Nullable tag: String?)

Calls add(int, Fragment, String) with a 0 containerViewId.

FragmentTransaction
add(@IdRes containerViewId: Int, @NonNull fragmentClass: Class<out Fragment!>, @Nullable args: Bundle?)

Calls add(int, Class, Bundle, String) with a null tag.

open FragmentTransaction
add(@IdRes containerViewId: Int, @NonNull fragment: Fragment)

Calls add(int, Fragment, String) with a null tag.

FragmentTransaction
add(@IdRes containerViewId: Int, @NonNull fragmentClass: Class<out Fragment!>, @Nullable args: Bundle?, @Nullable tag: String?)

Add a fragment to the activity state.

open FragmentTransaction
add(@IdRes containerViewId: Int, @NonNull fragment: Fragment, @Nullable tag: String?)

Add a fragment to the activity state.

open FragmentTransaction
addSharedElement(@NonNull sharedElement: View, @NonNull name: String)

Used with custom Transitions to map a View from a removed or hidden Fragment to a View from a shown or added Fragment.

open FragmentTransaction
addToBackStack(@Nullable name: String?)

Add this transaction to the back stack.

open FragmentTransaction
attach(@NonNull fragment: Fragment)

Re-attach a fragment after it had previously been detached from the UI with detach(Fragment).

abstract Int

Schedules a commit of this transaction.

abstract Int

Like commit but allows the commit to be executed after an activity's state is saved.

abstract Unit

Commits this transaction synchronously.

abstract Unit

Like commitNow but allows the commit to be executed after an activity's state is saved.

open FragmentTransaction
detach(@NonNull fragment: Fragment)

Detach the given fragment from the UI.

open FragmentTransaction

Disallow calls to addToBackStack(String).

open FragmentTransaction
hide(@NonNull fragment: Fragment)

Hides an existing fragment.

open Boolean

Returns true if this FragmentTransaction is allowed to be added to the back stack.

open Boolean

open FragmentTransaction
remove(@NonNull fragment: Fragment)

Remove an existing fragment.

FragmentTransaction
replace(@IdRes containerViewId: Int, @NonNull fragmentClass: Class<out Fragment!>, @Nullable args: Bundle?)

Calls replace(int, Class, Bundle, String) with a null tag.

open FragmentTransaction
replace(@IdRes containerViewId: Int, @NonNull fragment: Fragment)

Calls replace(int, Fragment, String) with a null tag.

FragmentTransaction
replace(@IdRes containerViewId: Int, @NonNull fragmentClass: Class<out Fragment!>, @Nullable args: Bundle?, @Nullable tag: String?)

Replace an existing fragment that was added to a container.

open FragmentTransaction
replace(@IdRes containerViewId: Int, @NonNull fragment: Fragment, @Nullable tag: String?)

Replace an existing fragment that was added to a container.

open FragmentTransaction
runOnCommit(@NonNull runnable: Runnable)

Add a Runnable to this transaction that will be run after this transaction has been committed.

open FragmentTransaction
setAllowOptimization(allowOptimization: Boolean)

open FragmentTransaction
setBreadCrumbShortTitle(@StringRes res: Int)

Set the short title to show as a bread crumb when this transaction is on the back stack.

open FragmentTransaction

Like setBreadCrumbShortTitle(int) but taking a raw string; this method is not recommended, as the string can not be changed later if the locale changes.

open FragmentTransaction
setBreadCrumbTitle(@StringRes res: Int)

Set the full title to show as a bread crumb when this transaction is on the back stack.

open FragmentTransaction
setBreadCrumbTitle(@Nullable text: CharSequence?)

Like setBreadCrumbTitle(int) but taking a raw string; this method is not recommended, as the string can not be changed later if the locale changes.

open FragmentTransaction
setCustomAnimations(@AnimatorRes @AnimRes enter: Int, @AnimatorRes @AnimRes exit: Int)

Set specific animation resources to run for the fragments that are entering and exiting in this transaction.

open FragmentTransaction
setCustomAnimations(@AnimatorRes @AnimRes enter: Int, @AnimatorRes @AnimRes exit: Int, @AnimatorRes @AnimRes popEnter: Int, @AnimatorRes @AnimRes popExit: Int)

Set specific animation resources to run for the fragments that are entering and exiting in this transaction.

open FragmentTransaction
setMaxLifecycle(@NonNull fragment: Fragment, @NonNull state: Lifecycle.State)

Set a ceiling for the state of an active fragment in this FragmentManager.

open FragmentTransaction
setPrimaryNavigationFragment(@Nullable fragment: Fragment?)

Set a currently active fragment in this FragmentManager as the primary navigation fragment.

open FragmentTransaction
setReorderingAllowed(reorderingAllowed: Boolean)

Sets whether or not to allow optimizing operations within and across transactions.

open FragmentTransaction
setTransition(transition: Int)

Select a standard transition animation for this transaction.

open FragmentTransaction
setTransitionStyle(@StyleRes styleRes: Int)

Set a custom style resource that will be used for resolving transit animations.

open FragmentTransaction
show(@NonNull fragment: Fragment)

Shows a previously hidden fragment.

Extension functions
From androidx.fragment.app
FragmentTransaction
FragmentTransaction.add(@IdRes containerViewId: Int, tag: String? = null, args: Bundle? = null)

Add a fragment to the associated FragmentManager, inflating the Fragment's view into the container view specified by containerViewId, to later retrieve via FragmentManager.findFragmentById.

FragmentTransaction
FragmentTransaction.add(tag: String, args: Bundle? = null)

Add a fragment to the associated FragmentManager without adding the Fragment to any container view.

FragmentTransaction
FragmentTransaction.replace(@IdRes containerViewId: Int, tag: String? = null, args: Bundle? = null)

Replace an existing fragment that was added to a container.

Constants

TRANSIT_ENTER_MASK

static val TRANSIT_ENTER_MASK: Int

Bit mask that is set for all enter transitions.

Value: 0x1000

TRANSIT_EXIT_MASK

static val TRANSIT_EXIT_MASK: Int

Bit mask that is set for all exit transitions.

Value: 0x2000

TRANSIT_FRAGMENT_CLOSE

static val TRANSIT_FRAGMENT_CLOSE: Int

Fragment is being removed from the stack

Value: 2 | TRANSIT_EXIT_MASK

TRANSIT_FRAGMENT_FADE

static val TRANSIT_FRAGMENT_FADE: Int

Fragment should simply fade in or out; that is, no strong navigation associated with it except that it is appearing or disappearing for some reason.

Value: 3 | TRANSIT_ENTER_MASK

TRANSIT_FRAGMENT_OPEN

static val TRANSIT_FRAGMENT_OPEN: Int

Fragment is being added onto the stack

Value: 1 | TRANSIT_ENTER_MASK

TRANSIT_NONE

static val TRANSIT_NONE: Int

No animation for transition.

Value: 0

TRANSIT_UNSET

static val TRANSIT_UNSET: Int

Not set up for a transition.

Value: -1

Public constructors

<init>

FragmentTransaction()

Deprecated: You should not instantiate a FragmentTransaction except via FragmentManager#beginTransaction().

Public methods

add

@NonNull fun add(
    @NonNull fragmentClass: Class<out Fragment!>,
    @Nullable args: Bundle?,
    @Nullable tag: String?
): FragmentTransaction

Calls add(int, Class, Bundle, String) with a 0 containerViewId.

add

@NonNull open fun add(
    @NonNull fragment: Fragment,
    @Nullable tag: String?
): FragmentTransaction

Calls add(int, Fragment, String) with a 0 containerViewId.

add

@NonNull fun add(
    @IdRes containerViewId: Int,
    @NonNull fragmentClass: Class<out Fragment!>,
    @Nullable args: Bundle?
): FragmentTransaction

Calls add(int, Class, Bundle, String) with a null tag.

add

@NonNull open fun add(
    @IdRes containerViewId: Int,
    @NonNull fragment: Fragment
): FragmentTransaction

Calls add(int, Fragment, String) with a null tag.

add

@NonNull fun add(
    @IdRes containerViewId: Int,
    @NonNull fragmentClass: Class<out Fragment!>,
    @Nullable args: Bundle?,
    @Nullable tag: String?
): FragmentTransaction

Add a fragment to the activity state. This fragment may optionally also have its view (if Fragment.onCreateView returns non-null) into a container view of the activity.

Parameters
containerViewId Int: Optional identifier of the container this fragment is to be placed in. If 0, it will not be placed in a container.
fragmentClass Class<out Fragment!>: The fragment to be added, created via the FragmentManager's FragmentFactory.
args Bundle?: Optional arguments to be set on the fragment.
tag String?: Optional tag name for the fragment, to later retrieve the fragment with FragmentManager.findFragmentByTag(String).
Return
FragmentTransaction Returns the same FragmentTransaction instance.

add

@NonNull open fun add(
    @IdRes containerViewId: Int,
    @NonNull fragment: Fragment,
    @Nullable tag: String?
): FragmentTransaction

Add a fragment to the activity state. This fragment may optionally also have its view (if Fragment.onCreateView returns non-null) into a container view of the activity.

Parameters
containerViewId Int: Optional identifier of the container this fragment is to be placed in. If 0, it will not be placed in a container.
fragment Fragment: The fragment to be added. This fragment must not already be added to the activity.
tag String?: Optional tag name for the fragment, to later retrieve the fragment with FragmentManager.findFragmentByTag(String).
Return
FragmentTransaction Returns the s