Questions tagged [fragmenttransaction]

FragmentTransaction is an API for performing a set of Fragment operations.

A great feature about using fragments in your activity is the ability to add, remove, replace, and perform other actions with them, in response to user interaction. Each set of changes that you commit to the activity is called a transaction and you can perform one using APIs in FragmentTransaction.

Each transaction is a set of changes that you want to perform at the same time. You can set up all the changes you want to perform for a given transaction using methods such as add(), remove(), and replace(). Then, to apply the transaction to the activity, you must call commit(). For doing that you have to call *FragmentTransaction.

General Use

/ Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);

// Commit the transaction
transaction.commit();

In this example, newFragment replaces whatever fragment (if any) is currently in the layout container identified by the R.id.fragment_container ID. By calling addToBackStack(), the replace transaction is saved to the back stack so the user can reverse the transaction and bring back the previous fragment by pressing the Back button.

If you add multiple changes to the transaction (such as another add() or remove()) and call addToBackStack(), then all changes applied before you call commit() are added to the back stack as a single transaction and the Back button will reverse them all together.

The order in which you add changes to a FragmentTransaction doesn't matter, except:

  1. You must call commit() last
  2. If you're adding multiple fragments to the same container, then the order in which you add them determines the order they appear in the view hierarchy

Note:

If you do not call addToBackStack() when you perform a transaction that removes a fragment, then that fragment is destroyed when the transaction is committed and the user cannot navigate back to it. Whereas, if you do call addToBackStack() when removing a fragment, then the fragment is stopped and will be resumed if the user navigates back.

Reference

  1. http://developer.android.com/reference/android/app/FragmentTransaction.html
  2. http://developer.android.com/guide/components/fragments.html
  3. http://www.vogella.com/articles/AndroidFragments/article.html
  4. http://www.javacodegeeks.com/2013/06/android-fragment-transaction-fragmentmanager-and-backstack.html
451 questions
3
votes
0 answers

How to switch between fragments forwards and backwards correctly?

I'm currently creating an activity containing a few stages. It is supposed to start at 'stage1' and when clicking next proceed to the next stage and when clicking back go to the previous stage. I've tried using FragmentTransaction and replace an…
3
votes
4 answers

How to reuse a fragment

When I replace a fragment, it gets destroyed. I tried to create the fragment in activity's onCreate and hold its reference in the activity and use it when I "re show" or "reopen" it by calling replace with the reference, but that did not help and…
3
votes
0 answers

Show a fragment with shared elements animation

In my app I have code like this: final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.fragment_container, fragment, "tag"); transaction.addSharedElement(view,…
3
votes
2 answers

Where exactly implement Fragment transactions?

In the Android Fragments Tutorial it clearly states that "Once the activity reaches the resumed state, you can freely add and remove fragments to the activity. So technically it means that we should add and remove (or replace, I guess the tutorial's…
Mr T
  • 506
  • 2
  • 9
  • 26
3
votes
2 answers

Android fragment transaction listener

I need to listen when a fragment transaction is performed, for example, when I replace a fragment with another, without call to addToBackStack(). FragmentManager class provides a addOnBackStackChangedListener callback, but when I perform a fragment…
3
votes
1 answer

Problems with fragment transitions

I have two fragments in an activity and I want the first one to gracefully slide out of view through the left of the screen while the second enters through the right of the screen. I am using the Transitions API and this is what happens: Initial…
3
votes
0 answers

fragment exit animation not working

I want slide up animation when opening Fragment B. And when going Back from Fragment B to A, I need the animation in reverse order. I have an Activity which contains a Fragment A, I open Fragment B from A using the given code with animation. …
Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300
3
votes
5 answers

How to pass from one fragment to another

In my app I have navigation drawer with some fragments. When I choose in the drawer I do this code: FragmentTransaction ft = fm.beginTransaction(); ft.replace(R.id.container, f, tag); ft.commit(); in the MainActivity. Now in one fragment I want to…
ste9206
  • 1,822
  • 4
  • 31
  • 47
3
votes
0 answers

RecyclerView in CoordinatorLayout doesn't scroll to the end after fragment transaction

I've encountered an issue where a fragment with a CoordinatorLayout and RecyclerView doesn't allow scrolling to reveal all the content (only part of it) after a fragment transaction. More specifically I have the following components: An activity…
3
votes
0 answers

Best Practice/usual way to switch between 2 fragments in FrameLayout

I working on a app whitch has 1 activity with 2 buttons (Map and Listview). By clicking on the buttons the fragment into the FrameLayout needs to change from a listview to a mapview (or from map to listview). I got this working and the fragments are…
3
votes
0 answers

remove fragment not work after addToBackStack

When I start a fragment and add to BackStack , then it can not be remove by FragmentTransaction.remove(),add like this: mManager.beginTransaction() .add(R.id.fragment_container, new FB(), "B") .addToBackStack("addB") …
Fantasy_RQG
  • 143
  • 1
  • 13
3
votes
2 answers

Do Samsung devices treat FragmentTransactions differently than other devices?

I have been trying to solve a problem that is driving me nuts for ages. I have an app with one Activity that shows different screens by dynamic fragment exchange, ie the usual getFragmentManager().beginTransaction().replace(R.id.fragementContainer,…
3
votes
5 answers

addToBackStack() method is not working without overriding the onBackPressed() method in android

Here is the code of MainActivity which I have written. I am loading the list of fragment in the first screen. When the user taps on any of the list items, the planet name will be shown to the user in the detail fragment which I have defined in a…
3
votes
2 answers

add progress dialog while fragment transition

I try to show A ProgressDialog while switching one Fragment to another Fragment such as message "please wait..." or "loading page..." etc I need to add this message because loading time of Fragment even I have added…
Menma
  • 799
  • 1
  • 9
  • 35
3
votes
5 answers

What id to put in fragmentTransaction.replace() method argument?

I am trying to call fragment from fragment. I am using following code: Fragment fragment = new TeamDetails3(); FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction(); …
user3904345