且构网

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

参数为空的 addToBackStack 是什么意思?

更新时间:2021-07-09 22:11:38

addToBackStack(null) 后跟一个 commit() 是什么意思?

What is the meaning of addToBackStack(null) followed by a commit()?

引用文档:

通过调用addToBackStack(),替换事务被保存到返回堆栈,以便用户可以撤销交易并带回按下后退按钮,上一个片段.

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.

如果对事务添加多个更改(例如另一个 add()或 remove()) 并调用 addToBackStack(),然后应用所有更改在您调用 commit() 之前作为单个添加到后台堆栈交易和后退按钮会将它们全部撤消.

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.

向 FragmentTransaction 添加更改的顺序无关紧要,除了:

The order in which you add changes to a FragmentTransaction doesn't matter, except:

您必须最后调用 commit().如果您将多个片段添加到同一个容器,则添加它们的顺序决定了它们在视图层次结构中的显示顺序.

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.

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

Why you need to pass a null parameter to addToBackStack?

它不需要为空,它可以是一个字符串.如果你不想,直接传null.

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)

public abstract FragmentTransaction addToBackStack (String name)

在 API 级别 11 中添加将此事务添加到后台堆栈.这意味着事务在提交后会被记住,并在稍后弹出堆栈时反转其操作.

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.

参数name 此返回堆栈状态的可选名称,或为 null.

Parameters name An optional name for this back stack state, or null.

关于:

似乎这段代码没用,因为我在没有最后一个的情况下运行了代码线 .addToBackStack(null).commit() 它运行没有任何问题

Seems like this code is useless as I ran the code without the last line .addToBackStack(null).commit() and it ran without any problems

如果您想导航到上一个片段,请将其添加到 backstack.所以这取决于您是否要将片段添加到后台堆栈中.

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?

您已经拥有片段实例 firstFragment.所以我不知道你说的稍后获取片段是什么意思.

You already have the fragment instance firstFragment. So I don't know what you mean by get the fragment later.

更多信息@

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

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