且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

什么是addToBackStack与空参数的含义?

更新时间:2022-10-14 18:44:57

  

什么是addToBackStack(空),其次是意提交()?

引用文档

  

通过调用addToBackStack(),替换事务被保存到   回到栈使用户可以反向交易,并带回   由$ P $ previous片段pssing后退按钮。

     

如果您添加多个变化到事务(如另一个附加()   或删除()),并调用addToBackStack(),然后应用所有更改   在打电话之前提交()加入到背面叠层作为一个单一的   交易和后退按钮将扭转它们放在一起。

在其中添加更改为FragmentTransaction的顺序并不只是无所谓,:

您必须调用提交()最后如果要添加多个片段在同一容器中,然后在其中添加它们的顺序决定了它们出现在的顺序视图层次结构。

所以,你必须提交在最后。

  

为什么你需要传递一个空参数addToBackStack?

这不需要是空它可以是一个字符串。如果你不希望只是通过空

 公共抽象FragmentTransaction addToBackStack(字符串名称)

新增的API级别11
本次交易添加到后面堆栈。这意味着,该交易将记住它承诺之后,并改变其运行时后弹出堆栈。

参数
名称此回堆栈状态,或空的可选名称。
 

  

看起来这code是没用的,因为我跑了code没有最后   行.addToBackStack(空).commit(),它运行没有任何问题。

如果你想浏览到previous片段添加到backstack

。因此,这取决于你是否要将该片段添加到backstack。

  

如何获得该片段添加类似这样的后?

您已经有段实例 firstFragment 。所以我不知道你的意思被后来得到片段

更多信息@

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

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

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.

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 commit at the last.

Why you need to pass a null parameter to addToBackStack ?

It need not 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.

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)