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
7
votes
1 answer

AppCompat Action Bar library not displaying added fragments

EDIT: If I extend FragmentActivity instead of ActionBarActivity my layout shows up again (without an ActionBar of course). The ActionBar works as intended on 4.x devices, but on my 2.3 device all I get is the ActionBar and a blank screen below it.…
7
votes
1 answer

Strange FragmentTransaction in the FragmentLayout class from the ApiDemos sample

I am sorry, this question is for those of you working on Eclipse with access to the ApiDemo sample codes. Specifically, I am trying to base a fragment activity on the sample called FragmentLayout The following code is problematic for me (you can…
ilomambo
  • 8,290
  • 12
  • 57
  • 106
6
votes
2 answers

How to show enter fragment above exit fragment while animating.

Effect what I want to achieve is to overlay enter (new) fragment above the exit(old) fragment, but when I am replacing old fragment by new fragment,the old one just vanishes and new fragment slides up the container, which is visible (container). I…
Dory
  • 7,462
  • 7
  • 33
  • 55
6
votes
1 answer

Fragment onCreateView and onActivityCreated not being called on rotation

I am working with fragments and pushing new fragments on the backstack but when I rotate the device twice the fragment's onCreateView, onActivityCreated, and so on in the fragment life cycle methods are never called leaving a blank screen. This…
Ge3ng
  • 1,875
  • 1
  • 24
  • 38
6
votes
3 answers

Android: Getting white screen with fragment transaction

I am making a news app where I have a fragment that holds all the news in a list and another fragment that have videos list. When you touch a new it will call a method from the activity named openNewsCont that will replace the current fragment with…
6
votes
5 answers

How to add a fragment to a layout of a DialogFragment?

I am having a custom DialogFragment which contains a layout, some UI elements, and a Fragment holder layout. What I need to do is inflate a Content fragment into the holder layout and provide a navigation inside that. On clicking a button inside the…
6
votes
1 answer

How can I implement FragmentManager and FragmentTransaction to replace just a single fragment?

I have an activity with 3 fragments; a header; a body; a footer (same point as in HTML). The bodyfragment contains three buttons which each should replace the middle fragment (body; itself) with another one, but I can't figure out how to work…
Carl
  • 249
  • 2
  • 5
  • 13
6
votes
1 answer

FragmentTransacation with slide in/out animation on >4 performance

I'm trying to implement fragment transaction with slide in/out animation. I'm developing on minimum 14 sdk so ObjectAnimator is the only option for me (is there any other way? translate animation is not available as I understand). The Code is very…
5
votes
3 answers

Fragment Transaction .add vs .replace optimum perspective?

When using a fragment transaction we can do them using add and replace methods. If we use add, the previous fragment is not destroyed and kept in memory. And if we use replace the previous fragment is destroyed and recreated again when we go back.…
Damia Fuentes
  • 5,308
  • 6
  • 33
  • 65
5
votes
1 answer

Deep link and animation using Android Navigation component

I'm trying to implement a forgot password flow. What I'd like to do is to handle the received email, which contains a link to a forgot password web page, inside the app. Using the Navigation component I created a deep link, which opens the right…
5
votes
0 answers

How to fix "FragmentManager is already executing transactions" error?

Ever since I updated my app implementations, I find myself unable to lunch the application. I am getting a "java.lang.IllegalStateException: FragmentManager is already executing transactions" error, and can't seem to find a solution to this…
5
votes
3 answers

When is it safe to commit a FragmentTransaction?

According to the docs 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…
user3591494
  • 159
  • 3
  • 8
5
votes
1 answer

Android TextInputLayouts losing text/content when coming back in fragment transaction

I've been searching for a while now but I think most of the reported bugs (and there are quite a few) in android.support.design.widget.TextInputLayout differ a little from this one. At least, I have solved most of the other bugs, but struggle with…
5
votes
1 answer

Fragment's Transaction replace on API-21 is staying behind

I am developing an app that uses fragments, last week my test device took lolipop update. When I test my app on a lolipop device, I saw that Fragment Transaction's replace method didn't work properly. It work with confusingly in Lolipop version…
salih
  • 727
  • 13
  • 36
5
votes
1 answer

how to setCustomAnimations for FragmentTransaction on this code

i have 5 fragments and I use the following code to setCustomAnimations for FragmentTransaction: FragmentTransaction trans = getFragmentManager().beginTransaction(); trans.setCustomAnimations(android.R.anim.slide_in_left,…
ali-star
  • 706
  • 1
  • 10
  • 19