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

Freezing UI when fragment transaction

I'm facing problem with replacing fragment using FragmentManager. My problem is freezing the UI. I'm trying to find some good practices and/or library to handle my problems. Part of my code: Main activity…
5
votes
1 answer

Hide / Show fragment with fragmentTransaction not working

I want my activity to contain 2 fragments, and to switch between them with a button at the action bar. So here is my activity's layout:
Lucas Jota
  • 1,863
  • 4
  • 24
  • 43
5
votes
1 answer
5
votes
2 answers

Android: When is it appropriate to use FragmentTransaction.remove?

I thought I had understood that you're supposed to call FragmentTransaction.add() in onCreate() and FragmentTransaction.remove() in onDestroy(). My app crashes in onDestroy() with this error: 06-26 15:25:50.213: E/AndroidRuntime(579):…
KairisCharm
  • 1,363
  • 1
  • 13
  • 32
5
votes
1 answer

ViewPager inside Fragment disappear after transaction.remove() (Maintain instance?)

I'm using Google API 8(Android 2.2) with v4 packages. Hi, here is my problem: I have a FragmentActivity that has a menu who always stays on screen and a little container(a FrameLayout) where a I put many fragments. My application works fine when I'm…
adheus
  • 3,985
  • 2
  • 20
  • 33
4
votes
1 answer

Skip fragments onBackPressed (add/hide)

I am using fragmentTransaction to proceed through fragments, it looks like this: FragmentManager fm = oldFragment.getFragmentManager(); FragmentTransaction fragmentTransaction = fm.beginTransaction(); …
4
votes
1 answer

No view found for id when popping backstack without adding to backstack

I am working on an Android app and have encountered an error involving Fragments and FragmentTransactions. I have created an example app to demonstrate the problem. Here is what I am doing: add Fragment1 to fragmentSpace, without adding to…
4
votes
2 answers

Android Fragment casting error with second FragmentTransaction.replace() call

I have 3 types of fragments all being held in my fragment_containerin my classic_menu.xmlfor my MainActivity.java. I start with fragment A and by pressing a button go to fragment B via a method I have that uses…
4
votes
1 answer

How to clear backStack of support FragmentManager?

Is there any way, how to clear backStack of support FragmentManager without calling onCreateView() in stored fragments? I understand fragment lyfe cycle and calling onDestroyView() and onCreateView() when it is…
4
votes
2 answers

How to avoid multiple instances of fragments in Activity after app is killed and resumed?

I have an app with a Home screen that has 2 fragments (for now) and a navigation drawer. Currently I load the fragment A (Explore) on startup and load fragment B when clicked. From then on, I show and hide fragments. It's faster than recreating…
Rahul Sainani
  • 3,437
  • 1
  • 34
  • 48
4
votes
1 answer

Disable fragment transaction animation after it's been commited

I have a little uncommon fragment navigation, because I want to have app responsive. Due to that I use almost only show/hide methods with fragments. Whenever I want to navigate to another fragment and go back with the back key, I add this…
Lucas
  • 3,521
  • 3
  • 27
  • 39
4
votes
2 answers

No view found for id - DialogFragment + Fragment

I have a DialogFragment with a simple layout. I want to add a fragment (and in the future replace this fragment with another) to a FrameLayout that is inside the DialogFragment's layout. However, the method of adding the new fragment fails with the…
4
votes
1 answer

FragmentTransaction.replace replaces only first Fragment in a container

I have two fragments in a single container (say A and B) added in a single transaction. I try to replace them with C. According to the documentation, replace should remove all fragments from a specified container and then add new one. Instead, it…
Eugene
  • 656
  • 8
  • 20
4
votes
1 answer

How can I know the fragment transaction is finished?

I want to make two fragment transactions in a row. Fragment1 fragment1 = new Fragment1(); FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.fragment_container,…
Dragon warrior
  • 1,644
  • 2
  • 24
  • 37
4
votes
3 answers

how to wait until a fragment is removed

I have an activity with dynamic fragments in it. I need to run some code after a fragment is removed but remove(myFragment).commit() is executed asynchronously and i cant know when exactly the fragment is removed.Here is my code: final…
Mehdi Fanai
  • 4,021
  • 13
  • 50
  • 75