Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
115 views
in Technique[技术] by (71.8m points)

android - What is the meaning of addToBackStack with null parameter?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...