- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我有一个客户代码。所有 fragment 只有一个 Activity ,即单个 Activity 正在管理所有 fragment 。
此 Activity 包含该 fragment 方法末尾的任何 fragment 的以下代码-
例如—— fragment MoreFragment:
MoreFragment firstFragment = new MoreFragment();
getSupportFragmentManager().beginTransaction()
.replace(R.id.article_fragment, firstFragment)
.addToBackStack(null).commit();
所以,
1) addToBackStack(null)
后跟 commit()
是什么意思?
2) 为什么需要向 addToBackStack
传递一个空参数?
3)这样添加后如何获取该 fragment ?
似乎这段代码没用,因为我在没有最后一行 .addToBackStack(null).commit()
的情况下运行了代码,并且运行没有任何问题。
最佳答案
What is the meaning of addToBackStack(null) followed by a commit()?
引用文档:
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.
向 FragmentTransaction 添加更改的顺序无关紧要,除了:
您必须最后调用 commit()
。如果您将多个 fragment 添加到同一个容器,那么您添加它们的顺序决定了它们在 View 层次结构中出现的顺序。
所以你必须在最后提交。
Why you need to pass a null parameter to addToBackStack?
它不需要为空,它可以是一个字符串。如果你不想要,只需传递 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
如果您想导航到上一个 fragment ,请将其添加到 backstack。所以这取决于您是否要将 fragment 添加到后台堆栈。
How to get that fragment after being added like this?
您已经拥有 fragment 实例 firstFragment
。所以我不知道你说的稍后获取 fragment 是什么意思。
更多信息@
http://developer.android.com/guide/components/fragments.html
关于android - 带 null 参数的 addToBackStack 是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22984950/
在 Android 上的 FragmentTransaction 项中 docs ,说明方法replace()与调用方法remove()是一样的,对当前view中添加的所有fragment调用方法添加
我正在尝试关闭 fragment 并希望通过在我的 fragment 实现中使用 addToBackStack 来恢复主要 Activity ,但它不起作用。后退按钮正在关闭应用程序。 我使用的 fr
在我的应用程序中,我正在处理一个显示不同 fragment 的 Activity 。该 Activity 还具有抽屉布局。 activity_main.xml: 所以现在我试图通过替换它们
我就是不明白。 我有一个 Activity A、一个 Fragment F1 和一个 Fragment F2。当 Activity 启动时,它会通过 F1 打开 getFragmentManager(
我的 MainActivity 管理两个一起工作的 fragment 。我的方法之一是我的 ListFragment 上的监听器接口(interface),MainActivity 负责切换 frag
这是负责将 fragment 添加到后台堆栈的函数: public void populateContent(File f) { ContentFragment cf = new Conten
有什么办法可以让这个更干净一点吗?我知道当 query = ""时我位于主菜单,因此返回应该关闭应用程序。 if (query == "") { fragmen
当我们在 fragment 中使用 addToBackStack() 时如何设置动画? 我的旧片段需要保留,新片段应该将旧片段与动画重叠并显示给用户。 我尝试了 setCustomAnimation(
按后退按钮后,单击按钮时显示的新 fragment 不会关闭,而是关闭整个应用程序。该按钮位于三个选项卡之一内。 mainActivity.java、HeroesFragment(具有三个选项卡的 f
我被它弄糊涂了。 我有一个 Activity A,Fragment F1 和 F2,当单击一个按钮时,我想隐藏 F1 并将 F2 添加到同一位置,然后可以通过后退按钮返回到 F1。所以我做了如下。
这是我的主要 Activity 。我关注了this guide关于 fragment 正确。当我单击“后退”按钮时,我的应用程序关闭而不是返回到 MainScreenFragment。为什么会发生这种
我有一个 FragmentActivity,它在启动时加载一个自定义 ListFragment,并在单击某项后用新 fragment 替换它。 在 fragment Activity 子类中: pub
每当我按下我的应用程序的后退按钮时,就会发生以下情况。 原创手机展示:FragA 点击 FragA 上的 View 后手机展示:FragB 点击返回按钮后手机显示: fragment A fragme
我正在研究 Fragments,在区分 FragmentTransaction.replace(id, fragment, tag) 和 FragmentTransaction.addToBackSt
当我按下“后退”按钮(在模拟器和 Galaxy S2 上测试)时,无论提交了多少 fragment 事务,应用程序都会关闭:( 好吧,以防万一,我会发布我的 fragment 事务的实现,尽管我怀疑它
我的应用程序有选项卡,在一个选项卡中,我有一个带有 ListView 的 fragment 。单击列表中的项目时,我尝试使用以下内容在该选项卡下初始化一个新 fragment : private cl
我在使用 FragmentTransaction addToBackStack 时发现了一个奇怪的行为。每当手机旋转时,它都会重新创建堆栈中的所有 fragment 。为什么要重新创建这些 fragm
我在抽屉导航 Activity 中创建了一个选项卡式 Activity 。我已将抽屉导航内的列表与各个 fragment 链接起来。我还在 ft.commit() 之前使用了命令 ft.addToBa
我没有找到解决问题的方法。 我有几个 fragment (动态创建),但我的后退按钮根本不起作用,按下它将关闭应用程序,无论 fragment 是“Activity 的”。 @Override pub
具有添加 fragment 和稍后的 replace() fragment 的流程。所有 fragment 都是动态添加的,但不是所有 fragment 都调用addToBackStack()。 ge
我是一名优秀的程序员,十分优秀!