加(),更换(),和addToBackStack的区别()区别、addToBackStack

2023-09-12 01:02:03 作者:男霸ゞ

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

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(),这是否搜索标签的添加() / 替换()法或 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,片段的片段,字符串标签)

说明的 - 更换被添加到容器的现有片段。这在本质上是一样的调用remove(片段)的添加具有相同containerViewId所有当前已添加的片段,然后添加(INT,片段,字符串)这里给出相同的参数。

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.onCreateView返回非空)插入活动的容器图。

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.

您可以将多个片段在一个活动建立一个多窗格UI和重用在多个活动的片段。当你定义了你的片段容器中,不同的布局,这本质上是有用的。你只需要更换以任何布局的任何其他片段。

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.

您也可以回到previous片段在backStack与 popBackStack()方法。对于需要使用 addToBackStack(),然后补充说,片段在堆栈提交()来体现。这是在相反的顺序与在顶部的电流。

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做到这一点搜索由添加添加标签/替换   方法或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