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
440 questions
524
votes
33 answers

IllegalStateException: Can not perform this action after onSaveInstanceState with ViewPager

I'm getting user reports from my app in the market, delivering the following exception: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState at…
106
votes
11 answers

Fragment lifecycle - which method is called upon show / hide?

I am using the following method to switch between Fragments (in my NavigationDrawer) by showing / hiding them. protected void showFragment(int container, Fragment fragment, String tag, String lastTag, boolean addToBackStack ) { …
Philipp Jahoda
  • 47,594
  • 21
  • 164
  • 175
70
votes
2 answers

How is the new FragmentTransaction commitNow() working internally?

The new commitNow() method added in Android N and support library version 24 has a limited and a bit confusing documentation. Commits this transaction synchronously. Any added fragments will be initialized and brought completely to the lifecycle…
Kaizie
  • 4,308
  • 3
  • 18
  • 18
63
votes
2 answers

What does FragmentManager and FragmentTransaction exactly do?

I have simple code below FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.replace(R.id.fragment_container,…
46
votes
7 answers

DrawerLayout's item click - When is the right time to replace fragment?

I'm developing an application which uses the navigation drawer pattern (With DrawerLayout). Each click on a drawer's item, replaces the fragment in the main container. However, I'm not sure when is the right time to do the fragment transaction? When…
45
votes
7 answers

Android FragmentTransaction commit already called

My ERROR : java.lang.IllegalStateException: commit already called My CODE: final FragmentTransaction fragmentTransaction =getFragmentManager().beginTransaction(); f1_fragment = new F1_Fragments(); f2_fragment = new…
JPs
  • 461
  • 1
  • 4
  • 6
36
votes
8 answers

findFragmentByTag() returns null after perform a FragmentTransaction using replace() method

My Android app consists three fragments: A, B and C. They're loaded in the two containers defined in the MainActivity layout. When the app is started, it shows the fragmentA loaded in the left_container and the fragmentC in the right_container. If…
Guillermo Barreiro
  • 1,336
  • 1
  • 11
  • 13
36
votes
1 answer

When to use the attach and detach methods of FragmentTransaction

I just went through the documentation of the attach() and detach() methods of FragmentTransaction: attach(): Re-attach a fragment after it had previously been detached from the UI with detach(Fragment). This causes its view hierarchy to be…
GingerJim
  • 3,217
  • 5
  • 23
  • 36
36
votes
3 answers

How to replace fragment C with fragment A when back button is pressed?

My scenario : Activity 1 consists of Fragments A-> B-> C. All the fragments are added using this code : FragmentManager fm = getSupportFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); …
35
votes
16 answers

java.lang.IllegalArgumentException: No view found for id 0x1020002 (android:id/content) for fragment

I am trying to move from one fragment to another.. It shows following error during fragment transaction- java.lang.IllegalArgumentException: No view found for id 0x1020002 (android:id/content) for fragment PhotosFragment2{41a57218 #3…
Android Developer
  • 7,267
  • 16
  • 62
  • 113
32
votes
5 answers

An invisible layout behind the fragment is getting clicked:

I have created several fragments and I add the first fragment the following way: mainFragment = (MainFragment) MainFragment.create(); getSupportFragmentManager().beginTransaction() .setCustomAnimations(R.anim.slide_in_right,…
Emil Adz
  • 38,699
  • 35
  • 127
  • 177
25
votes
1 answer

What is the difference between enter/exit and popEnter/popExit animations?

In setCustomAnimations() it takes four resource id for the animation. Not really understand them. If someone having clearer picture of it it would be appreciated if you could explain. Let's say having fragment A add in the place holder and…
lannyf
  • 6,775
  • 4
  • 49
  • 92
24
votes
9 answers

Navigation Drawer lag on Android

I'm having a problem with Navigation Drawer , it is too slow, the solution I'm looking for is to close the drawer first and then show the activity, but It is not working, certainly I'm missing something. private class DrawerItemClickListener…
AND4011002849
  • 1,994
  • 6
  • 31
  • 75
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…
18
votes
2 answers

Fragment isAdded() returns false on an already added Fragment

I have this neat function: private void addMapFragment(){ if(!mapFragment.isAdded()){ FragmentTransaction ft = getFragmentManager().beginTransaction(); ft.add(R.id.mapContainer, mapFragment); ft.commit(); } } I'm…
Lumbi
  • 648
  • 2
  • 7
  • 16
1
2 3
29 30