65

I have simple code below

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, mFeedFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();

What do these lines of code do?

Alireza.Heidari
  • 737
  • 1
  • 8
  • 11
  • 1
    I'm surprised you awarded an answer. As far as I can tell, the explanations below are far short of really explaining this two-step process. – SMBiggs May 13 '16 at 05:47

3 Answers3

73
getFragmentManager()

Return the FragmentManager for interacting with fragments associated with this activity.

FragmentManager which is used to create transactions for adding, removing or replacing fragments.

fragmentManager.beginTransaction();

Start a series of edit operations on the Fragments associated with this FragmentManager.

The FragmentTransaction object which will be used.

fragmentTransaction.replace(R.id.fragment_container, mFeedFragment);

Replaces the current fragment with the mFeedFragment on the layout with the id: R.id.fragment_container

fragmentTransaction.addToBackStack(null);

Add this transaction to the back stack. This means that the transaction will be remembered after it is committed, and will reverse its operation when later popped off the stack.

Useful for the return button usage so the transaction can be rolled back. The parameter name:

Is an optional name for this back stack state, or null.

See for information the other question What is the meaning of addToBackStack with null parameter?

The Last statement commits the transaction and executes all commands.

See the google documentation for more help:

http://developer.android.com/reference/android/support/v4/app/FragmentActivity.html http://developer.android.com/reference/android/app/FragmentManager.html http://developer.android.com/reference/android/app/FragmentTransaction.html

Community
  • 1
  • 1
Zelldon
  • 5,396
  • 3
  • 34
  • 46
  • Can you tell me some important app that use fragment? is that really usefull? – Alireza.Heidari Apr 21 '15 at 22:09
  • 1
    Yes it use because every app which uses the navigation drawer use fragments. https://developer.android.com/design/patterns/navigation-drawer.html https://developer.android.com/training/implementing-navigation/nav-drawer.html – Zelldon Apr 21 '15 at 22:10
26

Android FragmentManager

A FragmentManager manages Fragments in Android, specifically it handles transactions between fragments. A transaction is a way to add, replace, or remove fragments.

Android FragmentTransaction

As said before a FragmentTransaction gives us methods to add, replace, or remove fragments in Android. It gives us an interface for interacting with fragments.


fragmentTransaction.replace(R.id.fragment_container, mFeedFragment);

The method replace(int containerViewId, Fragment fragment) replaces an existing Fragment object from the container containerViewId and adds the the Fragment fragment

fragmentTransaction.addToBackStack(null);

This method, addToBackOfStack(String name), adds this transaction to the back stack, this can be used so that Fragments are remembered and can be used again by the Activity

fragmentTransaction.commit();

The method commit() schedules this transaction, this is not instantaneous; It is scheduled on the main thread to be done when the thread is ready.

Reference

Gready
  • 1,134
  • 8
  • 15
  • 7
    Yes, we all know WHAT these classes do, but WHY? Why the separate classes? Why couldn't the FragmentManager do the work of the FragmentTransaction? – SMBiggs May 13 '16 at 05:46
  • 2
    @ScottBiggs Thanks for the question there. I had a closer look into these classes,and I believe it is a question of separating similar functionality; FragmentTransacion deal with specific things to do with a 'transaction', such as animations and breadcrumbs. Another thought I had is that this will mainly help with future development as functionally grows within Android. I noticed you were quite qualified, I wonder what your opinion is here? – Gready May 13 '16 at 11:18
  • 2
    You're probably right, but we're trying to read the minds of the Google UI programmers, which is pretty difficult. Android programming started out as very difficult and increased in complexity with every new version. At this point I just want simplicity, even if it means no more backwards compatibility. – SMBiggs May 16 '16 at 04:32
  • 1
    @ScottBiggs, another reason the FragmentTransaction and FragmentManager logic might be implemented in separate classes: in https://developer.android.com/reference/android/app/FragmentTransaction.html#setBreadCrumbShortTitle, the documentation talks about the breadcrumb title being modified for "when the transaction is on the back stack". I could be in left field here, but it would make sense to me to separate the transaction logic, because of the back stack. ? – Charles Thomas May 21 '16 at 21:36
  • @CharlesThomas Very interesting theory. But since FragmentBreadCrumbs has been deprecated, I kind of doubt that is the full reason. There may indeed be something about how the back stack is handled. Please comment again if you find out more. – SMBiggs May 23 '16 at 07:48
  • https://developer.android.com/guide/components/fragments.html talks about modular nature of fragments. I *THINK* the reason for FragmentTransactions is... Without a FragmentTransaction, it would take a monolithic FragmentManager to handle all the possible permutations of a fragment's configuration. – Charles Thomas Apr 06 '18 at 17:07
0

For more readability and simplified transaction You can define simple function or use simple static functions in class FragmentTransaction :

https://github.com/mahditavakoli1312/FragmentTransaction---mahdi-tavakoli