Click here to Skip to main content
15,883,841 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am new to Andoid development and I have created a snippet for replacing fragments programetically.

I followed the guide on android developers.

I have create a method named `selectFrag` and fired it on button click:

public void selectFrag(View view)

    {

 Fragment fr;

         if(view == findViewById(R.id.showsecond)) {
             fr = new secondfragment();

         }else {
             fr = new firstfragment();
         }

        FragmentManager fm = getFragmentManager();

        FragmentTransaction ft = fm.beginTransaction();

        ft.replace(R.id.fragment_place,fr);

        ft.addToBackStack(null);

        ft.commit();
    }


Code works perfectly and I understand everything except `addToBackStack(null)`.

I experimented and understood that this method is for adding the fragment to the back button's stack, so that if we click back button, it do not leave the screen and show the previous work.

But, I do not understand what `null` shows here.

So, my question is very simple :What is meant by null here? or What do null does?
Posted

The 'tag' used in add/replace(id, fragment, tag) is used to retrieve the fragment by calling fragmentManager.findFragmentByTag(tag).

However, the 'name' used in addToBackStatck(name) is used to control to which fragment you want to pop the fragment back stack by calling popBackStatck/Immediate(name, flags). So if I have a fragment stack with named fragments: A, B, C and D with A at the bottom. When you call popBackStack(B, XXX_EXCLUSIVE), then your fragment back stack will be like: A and B after the call. Without the name, you can't do that.
 
Share this answer
 
I found answer on stackoverflow:

From documentation, it is pretty clear:

public abstract FragmentTransaction addToBackStack (String name)


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.


So your parameter is optional a represents the name of the fragment.

If you just want to add this transaction to the back stack and don't need to access it later then you can put `null` as the name.

In this context, `null` in plain English means "I don't need a name for this fragment". That is why it says the name is optional. If you do put a name you can use that name later. If you put a null that just means add this fragment to the back stack and I don't need it anymore.

The use of the name is to identify that specific fragment. This can be useful for example if you want to obtain that fragment from the `FragmentManager`:

addToBackStack (FRAGMENT_NAME);
getFragmentMangager().findFragmentByTag(FRAGMENT_NAME);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900