I have a customer code. There is only one activity for all of the fragments i.e. the single activity is managing all the fragments.
This activity contains the following code for any fragment at the method end of that fragment-
For example - fragment MoreFragment:
MoreFragment firstFragment = new MoreFragment();
getSupportFragmentManager().beginTransaction()
.replace(R.id.article_fragment, firstFragment)
.addToBackStack(null).commit();
So,
1) What is the meaning of addToBackStack(null)
followed by a commit()
?
2) Why you need to pass a null parameter to addToBackStack
?
3) How to get that fragment after being added like this ?
Seems like this code is useless as I ran the code without the last line .addToBackStack(null).commit()
and it ran without any problems.
You answers are deprecated. If you don't want to add fragments to back stack you should use below snippet of code:
Belowe you have cute example how use it:
Quoting docs
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 commit at the last.
It need not be null it can be a string. If you don't want just pass null
Concerning:
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.
You already have the fragment instance
firstFragment
. So i don't know what you mean by get the fragment laterMore information @
http://developer.android.com/guide/components/fragments.html
http://developer.android.com/reference/android/app/FragmentTransaction.html#addToBackStack(java.lang.String)