What Is the Meaning of Addtobackstack with Null Parameter

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)

What is the purpose of null in addToBackStack(null)?

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 and 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);

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

Android - addToBackStack() doesn't work?

I am not sure about the actual solution but I can guide you a bit and perhaps you can figure out what the problem is.

Are you actually replacing two fragments? If you do not then there is no transaction to revert. Also is your first Fragment added from XML? The manager will not know about this fragment and you might need to add the first Fragment using a transaction too.

Be careful to check for if (savedInstanceState == null) performFirstTransaction() otherwise you will end up adding your first Fragment twice.

One good idea is to use enableDebugLogging in the FragmentManager. This will tell you about which fragments the manager knows about.

See this:
http://developer.android.com/reference/android/app/FragmentManager.html#enableDebugLogging(boolean)

As a side note, it is NOT recommended to use a custom constructor for your Fragment. That is because if your app gets killed and re-instantiated by the OS it will call the empty constructor.

You should use a static method such as ContentFragment.getInstance(<params>) to create your Fragment.

See more info at: http://developer.android.com/reference/android/app/Fragment.html at the "Class Overview" section.

I hope my answer helps you a little bit to find the problem.

popBackStack() after addToBackStack does not work

You use the getSupportedFragmentManager() to replace FragmentA by FragmentB. But you call popBackStack() on the getFragmentManager().

If you are adding the Fragments to the android.support.v4.app.FragmentManager you also have to call popBackStack() on the same FragmentManager.

This code should solve the problem:

if (getSupportFragmentManager().getBackStackEntryCount() > 0){
boolean done = getSupportFragmentManager().popBackStackImmediate();
}

Inflate fragment just once

If you use addToBackStack, it'll always save the fragments to the backstack. Remove that line to not add the fragment to backstack. addToBackStack is used when there are multiple changes to the transaction, then all the changes are added in stack, and pressing back button will then restore those transactions one by one.

helpFragment = HelpFragment.newInstance()
supportFragmentManager
.beginTransaction() // Começar a transição
.replace(R.id.container, helpFragment)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.commit() // Aplicar as alterações
}


Related Topics



Leave a reply



Submit