What Does Fragmentmanager and Fragmenttransaction Exactly Do

What does FragmentManager and FragmentTransaction exactly do?

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

Understanding FragmentManager and FragmentTransaction lifecycle with regards to Activity

Static variables in Java are kept across Activity creation/destruction - they are associated with the class itself but not a particular instance of the class.

See the official documentation here:
http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

Your application doesn't end when the user returns to the home screen, it just gets put in a background state. If you force stopped the application and restarted it, then the static FragmentManager will be null.

With regards to CameraFragment, unless you've set setRetainInstance(true), it will get destroyed on an orientation change.

==== EDIT

Here's a more detailed flow of what's happening...

  1. You open the application up for the first time
  2. Activity, say instance A1, gets created and its corresponding FragmentManager instance, FM1, also gets created
  3. You store FM1 as a static variable
  4. You go back to home
  5. Activity A1 and FM1 gets destroyed because of the normal Activity lifecycle, although FM1's reference is still held onto by the static variable. At this point, FM1 loses all the fragments it contains and isDestroyed() will return true.
  6. Starting the app again
  7. New Activity instance A2 gets created along with its new FragmentManager instance FM2

What is the meaning of addToBackStack with null parameter?

What is the meaning of addToBackStack(null) followed by a commit()?

Quoting docs:

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:

You must call commit() last. 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.

So you have to commit at the last.

Why you need to pass a null parameter to addToBackStack?

It don't need to be null, it can be a string. If you don't want, just pass null.

public abstract FragmentTransaction addToBackStack (String name)

Added in API level 11
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.

Parameters
name An optional name for this back stack state, or null.

Concerning:

Seems like this code is useless as I ran the code without the last
line .addToBackStack(null).commit() and it ran without any problems

If you want to navigate to previous fragment add it to backstack. So it depends on whether you want to add the fragment to the backstack.

How to get that fragment after being added like this?

You already have the fragment instance firstFragment. So I don't know what you mean by get the fragment later.

More information @

http://developer.android.com/guide/components/fragments.html

http://developer.android.com/reference/android/app/FragmentTransaction.html#addToBackStack(java.lang.String)

Difference between add(), replace(), and addToBackStack()

1) fragmentTransaction.addToBackStack(str);

Description - 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.

2) fragmentTransaction.replace(int containerViewId, Fragment fragment, String tag)

Description - Replace an existing fragment that was added to a container. This is essentially the same as calling remove(Fragment) for all currently added fragments that were added with the same containerViewId and then add(int, Fragment, String) with the same arguments given here.

3) fragmentTransaction.add(int containerViewId, Fragment fragment, String tag)

Description - Add a fragment to the activity state. This fragment may optionally also have its view (if Fragment.onCreateView returns non-null) into a container view of the activity.

What does it mean to replace an already existing fragment, and adding
a fragment to the activity state and adding an activity to the back
stack ?

There is a stack in which all the activities in the running state are kept. Fragments belong to the activity. So you can add them to embed them in a activity.

You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities. This is essentially useful when you have defined your fragment container at different layouts. You just need to replace with any other fragment in any layout.

When you navigate to the current layout, you have the id of that container to replace it with the fragment you want.

You can also go back to the previous fragment in the backStack with the popBackStack() method. For that you need to add that fragment in the stack using addToBackStack() and then commit() to reflect. This is in reverse order with the current on top.

findFragmentByTag does this search for tag added by the add/replace
method or the addToBackStack method ?

If depends upon how you added the tag. It then just finds a fragment by its tag that you defined before either when inflated from XML or as supplied when added in a transaction.

References: FragmentTransaction

fragment, add fragment of fragmentTransaction,

Replace: This removes the existing fragment and adds a new fragment i.e it removes frag B and add frag C when you make a transaction.
Also when you press back button the frag B that got replaced will be created with its onCreateView being invoked.

Fragment's life cycle events onPause, onResume, onCreateView will be invoked here.

Add:This retains the existing fragments and adds a new fragment i.e it wont remove frag B and add frag C when you make a transaction.
Here the previous fragment i.e frag B will be active and they wont be in 'paused' state so when a back button is pressed onCreateView is not called for the fragB.

Fragment's life cycle events onPause, onResume, onCreateView wont be invoked here.

Android fragment manager fragment change listener

You can implement following listener in your activity class to watch for changes to the fragment back stack.

public static interface FragmentManager.OnBackStackChangedListener  

later you need to register this listener to the fragment manager with the help of

 addOnBackStackChangedListener (FragmentManager.OnBackStackChangedListener listener)

Links for your reference

https://developer.android.com/reference/android/support/v4/app/FragmentManager#addonbackstackchangedlistener

https://developer.android.com/reference/android/support/v4/app/FragmentManager.OnBackStackChangedListener

Fragment is not being replaced but put on top of the previous one

You are doing two things wrong here:

  1. You cannot replace a fragment that is statically placed in an xml layout file. You should create a container (e.g. a FrameLayout) in the layout and then add the fragment programatically using FragmentTransaction.

  2. FragmentTransaction.replace expects the id of the container that contains the fragment and not the id of the fragment as the first parameter. So you should pass the first argument as the id of the container that you added the first fragment to.

You can refer to this link for more details.

Can't show already added fragment

Instead of replace(), you should use add(). This way you are not replacing an existing fragment, but instead pushing a fragment B on top of fragment A.

For ex.

FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction()
.add("id of your fragment container", fragment, fragment.class.getSimpleName())
.addToBackStack(fragment.class.getSimpleName())
.commit();

This way if you want to load previous fragment you just have to do

fm.popBackStack();

And this will pop the fragment B from the BackStack, showing the previously created fragment A.



Related Topics



Leave a reply



Submit