add()、replace() 和 addToBackStack() 之间的区别 [英] Difference between add(), replace(), and addToBackStack()

查看:45
本文介绍了add()、replace() 和 addToBackStack() 之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

调用这些方法的主要区别是什么:

What is the main difference between calling these methods:

fragmentTransaction.addToBackStack(name);
fragmentTransaction.replace(containerViewId, fragment, tag);
fragmentTransaction.add(containerViewId, fragment, tag);

替换一个已经存在的片段,并在活动状态中添加一个片段,并将一个活动添加到返回堆栈中是什么意思?

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?

其次,使用 findFragmentByTag(),是否搜索由 add()/replace() 方法或 add()/replace() 添加的标签代码>addToBackStack() 方法?

Secondly, with findFragmentByTag(), does this search for tag added by the add()/replace() method or the addToBackStack() method?

推荐答案

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)

描述 - 替换添加到容器中的现有片段.这本质上与对使用相同 containerViewId 添加的所有当前添加的片段调用 remove(Fragment) 然后使用此处给出的相同参数调用 add(int, Fragment, String) 相同.

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)

描述 - 将片段添加到活动状态.该片段还可以选择将其视图(如果 Fragment.onCreateView 返回非 null)放入 Activity 的容器视图中.

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.

您可以在单个 Activity 中组合多个 Fragment 以构建多窗格 UI 并在多个 Activity 中重用 Fragment.当您在不同的布局中定义片段容器时,这非常有用.您只需要替换为任何布局中的任何其他片段.

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.

当您导航到当前布局时,您拥有该容器的 id 以将其替换为您想要的片段.

When you navigate to the current layout, you have the id of that container to replace it with the fragment you want.

您还可以使用 popBackStack() 方法返回 backStack 中的前一个片段.为此,您需要使用 addToBackStack()commit() 将该片段添加到堆栈中以进行反映.这是与电流相反的顺序.

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 会搜索由 add/replace 添加的标签方法还是 addToBackStack 方法?

findFragmentByTag does this search for tag added by the add/replace method or the addToBackStack method ?

如果取决于您添加标签的方式.然后它会根据您之前定义的标签找到一个片段,无论是从 XML 膨胀时还是在添加到事务中时提供.

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.

参考:FragmentTransaction

这篇关于add()、replace() 和 addToBackStack() 之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆