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

问题描述

我已经知道的是:

fragmentTransaction.replace(),当前片段的的onStop()函数将被调用而 fragmentTransaction.add()不会。

after fragmentTransaction.replace(), current fragment's onStop() function will be calledwhile fragmentTransaction.add() won't.

和调用后 fragMgr.popBackStack(); ,我们将回到previous片段无论 fragmentTransaction.replace fragmentTransaction.add()用于

and after calling fragMgr.popBackStack();, we will return to previous fragment no matter fragmentTransaction.replace or fragmentTransaction.add() is used

那么,是什么 fragmentTransaction.replace 吗?

So what does fragmentTransaction.replace do?

我能理解我们的添加片段OPON一个previous片段后来被返回previous片段popBackStack(),而

I can understand we can "add" a fragment opon a previous fragment and later return to previous fragment by popBackStack(), BUT:

如果previous片段是改为按当前片段,我想previous片段被删除,当前片段被加了进来,怎么能回到previous片段当 popBackStack()叫什么名字?

if previous fragment is "replaced" by current fragment, I guess previous fragment is removed and current fragment is added in, how can it return to previous fragment when popBackStack() called?

推荐答案

您可以添加多个片段到一个容器中,他们将在另一个的上面分层之一。如果你的片段具有透明背景,你会看到这样的效果,并能够同时与多个片段进行互动。

You can add multiple fragments to a container and they will be layered one on top of the other. If your fragments have transparent backgrounds you will see this effect and will be able to interact with the multiple fragments at the same time.

这是如果你使用FragmentTransaction.add容器上会发生什么。让您的片段将被放置在现有片段之上。

This is what will happen if you use FragmentTransaction.add on a container. Your added fragment will be placed on top of your existing fragment.

如果您使用 FragmentTransaction.replace(R.id.container,片段)这将删除已在该容器中的任何片段和你的新添加到同一个容器。

If you use FragmentTransaction.replace(R.id.container,fragment) it will remove any fragments that are already in the container and add your new one to the same container.

您也可以使用add方法,无需容器的id和你的片段将被添加到在FragmentManager片段列表,你可以在任何时候通过自己的标记值回想起这些。

You can also use the add method without a container id and your fragment will simply be added to the list of fragments in the FragmentManager and you can recall these at any time by their Tag value.

您仍然可以返回到previous配置如果您添加的交易来支持堆叠。你可以这样做即使previous操作删除片段。移除的片段记忆中的交易和弹出后退堆栈把它带回。

You can still return to a previous configuration IF you added the transaction to back stack. You can do this even if a previous operation removed a fragment. The removed fragment is remembered in the transaction and popping the back stack brings it back.

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

09-04 22:34