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

Android: How to prevent lag or animation skip on Fragment transaction when inflating RecyclerView inside?

I am working with some fragments but the transition animations when switching from one to another is very laggy and sometimes it even skips the entire animation (which I've read that sometimes is an outcome of poor fragment performance). The very…
18
votes
2 answers

Nested fragments transitioning incorrectly

Hello good programmers of stack overflow! I've spent a good week with this problem and am now very desperate for a solution. The scenario I'm using android.app.Fragment's not to be confused with the support fragments. I have 6 child fragments…
17
votes
1 answer

Is it OK to addToBackStack and replace in a fragment transaction?

Any thoughts on the following code? In my testing I've found the replaced fragment isn't destroyed and the instance is still around when popping the back stack. Just looking to verify that this is a valid way to use fragment…
bgolson
  • 3,460
  • 5
  • 24
  • 41
16
votes
1 answer

Replacing fragments have wrong elevation value

Hello again stack overflowians. I have another fragment question. (I'm using android.app.Fragment not Support Fragments) I'm trying to replace a fragment. But this isn't as simple as using: fragmentTransaction .replace(containerId, newFragment) …
16
votes
1 answer

android.R.id.content as container for Fragment

My situation is Activity A which contains Fragment B. I always implement it like this. Layout for Activity A:
14
votes
5 answers

NullPointerException : FragmentManager.beginTransaction()

I'm getting this error while trying to launch a Fragment from a first Fragment : java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.FragmentTransaction android.app.FragmentManager.beginTransaction()' on a null object…
14
votes
5 answers

Exit animation not working; FragmentTransaction custom animation does not work for hide

I use show/hide to display a fragment that takes up part of the screen. For some reason when the fragment is shown the slide_in_left animation plays, but when the fragment is being hidden there is no animation, the fragment just disappears. I've…
13
votes
3 answers

change navigation drawer selected item on fragment backstack change

I have two fragments FragmentHome and FragmentAbout, I have added NavigationDrawer to app when I click Home it opens FragmentHome and About opens FragmentAbout, when I open FragmentAbout I am also adding it to backstack. This is working fine. Now…
13
votes
3 answers

FragmentTransaction not doing anything

I am learning fragments and below given is my first fragment program. A simple project where I have 2 screens. When I click the next button of first screen, second button needs to be shown. I am targeting Android 2.1 and above and using…
Krishnabhadra
  • 34,169
  • 30
  • 118
  • 167
12
votes
6 answers

Android Fragment - move from one View to another?

Can i first add a Fragment to a View, then "detach" it, and then "re-attach" it to another View? In code, i want to: fragOne one = new fragOne(); getSupportFragmentManager().beginTransaction() .add(R.id.left, one,…
11
votes
1 answer

How to achieve smooth fragment transaction animations on Android

I've mostly worked with iOS and have become accustomed to very smooth and fluent screen change animations. I am now working on an Android app and can't for the life of me get a fragment transaction to smoothly add/replace a fragment. My set up is…
kev
  • 2,306
  • 3
  • 26
  • 31
11
votes
5 answers

Fragment Transaction load empty view but fragment is shown after rotating device

I am building a navigation drawer as designed by the google documentation however I have an issue where the fragment is not being replaced. http://developer.android.com/training/implementing-navigation/nav-drawer.html When the app first loads, the…
androiduae
  • 153
  • 2
  • 9
11
votes
2 answers

Change z-order of Fragments during ongoing FragmentTransaction

Is there a way how to change the z-order in which are fragments displayed during an ongoing FragmentTransaction? I've an animation where both fragments overlaps each other and I would like to have the fragment which slides from the right (the second…
11
votes
1 answer

What's the difference between using add().addToBackStack(), add().detach() and replace().addToBackStack() in a FragmentTransaction?

In FragmentTransaction item on Android docs, it is described that the method replace() is the same as calling the method remove() for all fragments added in the currently view and then called the method add(). In this case, to recover the previous…
mari
  • 864
  • 3
  • 12
  • 32
10
votes
0 answers

FragmentManager.getFragmens().size() dont decrease after FragmentTransaction.remove(Fragment)

After a while searching the reason of my NPE, i noticed that despite FragmentTransaction.remove(Fragment) reallys removes the Fragment, the size of the List returned by FragmentManager.getFragments() still the same. This screenshot shows what is…
1
2
3
30 31