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
10
votes
2 answers

FragmentTransaction : replace and addToBackStack not working together?

I'm fairly new to Android development and now running in to weird behaviour. I have an empty FrameLayout to be the container of a fragment. If user press a button for the first time, generate new Fragment and put inside container. If user press a…
Tar_Tw45
  • 3,122
  • 6
  • 35
  • 58
9
votes
1 answer

FragmentTransaction before and after setCustomAnimation callback

I'm using a custom animation to replace fragments, and I'd like to disable some buttons when the animation starts and enable then when the animation ends. How can I do this?
Christopher Francisco
  • 15,672
  • 28
  • 94
  • 206
9
votes
2 answers

Android - Making translations and objectAnimator on the same XML file

I've been trying to make a 3D Cube rotation effect while sliding from one fragment to another. First i was using a translate effect (on XML) calling with FragmentTransaction.setCustomAnimations(...) and then, when opening/closing the fragment, i was…
Diogo Pereira
  • 152
  • 1
  • 1
  • 9
9
votes
1 answer

Back key on programmatically added Fragment leads to empty container

I have a problem with a fragmented layout and I sincerely apologize if it has been answered before and I was too dumb to find it. I searched for hours and got nothing (well, I got lots but nothing solved my problem). So here's my setup: I have a two…
Bobby Tables
  • 111
  • 1
  • 4
8
votes
1 answer

How do you handle fragment transactions when the state of the parent activity is bound to be saved?

My app has several fragments and activities. Over the course of the main parent activity's lifecycle, the app presents information/options to the user in other activities. The documentation for Fragments has the following stipulation for…
8
votes
1 answer

When should setReorderingAllowed() be called on a FragmentTransaction?

At this part of a talk given at Google I/O 2017, the speaker introduces a new API for setReorderingAllowed() than can be called on a FragmentTransaction. The speaker explains: It allows all the execution to happen all at once without changing …
Dick Lucas
  • 12,289
  • 14
  • 49
  • 76
8
votes
1 answer

Android Fragment Transaction with animation causes white flash

I have two fragments. Fragment A is initially in view. When the user presses a button Fragment B is animated up into view using the method below. When I pop fragment B it animates back down out of view but right as it finishes the screen flashes…
Nath5
  • 1,665
  • 5
  • 28
  • 46
8
votes
4 answers

Replacing a fragment with another fragment of the same class

I have a fragment (let's call it MyFragment) that inflates different layouts according to a parameter passed in the arguments. All works well if MyFragment is started from a different fragment. But if MyFragment is active and I want to launch a new…
ilomambo
  • 8,290
  • 12
  • 57
  • 106
8
votes
2 answers

move Android fragment to a different container Can't change container ID of fragment

Here is what I would like my application to do on a tablet. Fragment (0) has some menu that would display fragments (1)...(n) like this: ----------------- | | | | | | | | | | |(0)| X | X | X | | | | | | | | | | …
Matthieu
  • 16,103
  • 10
  • 59
  • 86
7
votes
1 answer

volley display previous fragment response in new fragment when replacing previous fragment to new fragment via navigation drawer

i have a total 12 fragment in navigation drawar.. each fragment has volley method. and each fragment display its own volley response except position = 1 and position = 5 fragment. when my app start Scenario 1: i open position 1 fragment and after…
7
votes
2 answers

Android Fragment not added to Back Stack

I am doing a NavigationDrawer based application. I have an hierarchy like given below NavigationDrawer --> RootFragment(Not added to back Stack) --> Detail Fragment (Added to Back Stack) Now, I am trying to show a message to user when he tries…
Zach
  • 9,989
  • 19
  • 70
  • 107
7
votes
1 answer

Android Lint says I can replace a layout with a tag, but I need the id of the layout in a fragment transaction

I have a tabbed interface in Android and use a FrameLayout as my main layout in my app. I'm getting an Android Lint warning that says: This can be replaced with a tag The only place I use this FrameLayout (named…
7
votes
1 answer

How to add/remove Fragment on Button click?

At present I have got a "RELATIVE_LAYOUT" container which I am using for adding my fragment. I am using OnClickListener on a button to load the fragment XML layout into the RelativeLayout container. What I want to achieve is that when I press the…
7
votes
1 answer

Android: Dialog Fragment and Backstack problems

So lets say I have the following Fragments: Fragment1 Fragment2 DialogFragment Fragment3 Fragment4 Lets say I go to the following Fragments, each fragmetn is added to the back stack: Fragment1 -> Fragment2 -> DialogFragment -> Fragment3 ->…
Eric Bergman
  • 1,453
  • 11
  • 46
  • 84
7
votes
4 answers

Clicking back button after a Fragment transaction using addToBackStack does nothing

I want to be able to reverse a replace FragmentTransaction by using addToBackStack(): FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction(); Fragment scheduleFragment = new…
jul
  • 36,404
  • 64
  • 191
  • 318
1 2
3
30 31