Android fragmentTransaction.add 和 fragmentTransaction.replace 之间的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20682248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:17:02  来源:igfitidea点击:

difference between fragmentTransaction.add and fragmentTransaction.replace

androidandroid-fragmentsfragment

提问by Wood

What I already known is:

我已经知道的是:

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

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

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

调用后fragMgr.popBackStack();,无论使用fragmentTransaction.replace还是fragmentTransaction.add()使用,都会返回上一个片段

So what does fragmentTransaction.replacedo?

那么有什么作用fragmentTransaction.replace呢?

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

我可以理解我们可以在前一个片段上“添加”一个片段,然后返回到前一个片段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?

如果前一个片段被当前片段“替换”,我猜前一个片段被删除并添加了当前片段,它如何在popBackStack()调用时返回到前一个片段?

回答by Kuffs

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.

您可以将多个片段添加到容器中,它们将一个叠放在另一个上面。如果您的片段具有透明背景,您将看到这种效果并且能够同时与多个片段进行交互。

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.add 会发生这种情况。您添加的片段将放置在现有片段的顶部。

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.

如果您使用FragmentTransaction.replace(R.id.container,fragment)它,它将删除容器中已有的所有片段,并将新的片段添加到同一个容器中。

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.

您也可以使用不带容器 id 的 add 方法,您的片段将被简单地添加到 FragmentManager 中的片段列表中,您可以随时通过它们的 Tag 值调用这些片段。

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.

如果您将事务添加到后台堆栈,您仍然可以返回到以前的配置。即使之前的操作删除了一个片段,您也可以这样做。删除的片段在事务中被记住,弹出返回堆栈将其带回来。

回答by Rohit Singh

Two choices

两种选择

Let's say you have a fragment container.
And, your task is to add a fragment into the container.

假设您有一个片段容器。
而且,您的任务是将片段添加到容器中。

You can do this by calling any of the following methods

您可以通过调用以下任何方法来做到这一点

1) add(containerId,fragment)
2) replace(containerId,fragment)

1) add(containerId,fragment)
2)replace(containerId,fragment)

But both methods differ in behavior !!!

但是这两种方法的行为不同!!!

enter image description hereAlthough both methods will add your fragment into the fragment container, their innards(internal working) differ based on the two possible statesof the fragment container.
When fragment container
1) does not have any fragment in it.
2) already have one or multiple fragments attached to it.

在此处输入图片说明尽管这两种方法都会将您的片段添加到片段容器中,但它们的内部结构(内部工作)因片段容器的两种可能状态而异。
当片段容器
1) 中没有任何片段时。
2) 已经附有一个或多个片段。

Let's see what happens when we call add()and replace()method.

让我们看看当我们调用add()replace()方法时会发生什么。

Case 1: When there is no fragment attached in a container

案例1:当容器中没有附加片段时

In this case, both methods will add the fragment to the container. So they will produce same effect.

在这种情况下,两种方法都会将片段添加到容器中。所以它们会产生同样的效果

Case 2: When the fragmentContainer already has fragment/fragments

情况 2:当 fragmentContainer 已经有 fragment/fragments

add(): adds the new fragment on the top another fragment
replace(): removes everything then adds the new fragment

add():将新片段添加到另一个片段的顶部
replace():删除所有内容然后添加新片段

Example
So suppose the Fragment container has fragments[A->B->C].
Now you want to add a new fragment D.
add()method result will be [A->B->C->D]
replace()method result will be [D]

示例
所以假设 Fragment 容器有fragments[A->B->C].
现在您想添加一个新的fragment D.
add()方法结果将是[A->B->C->D]
replace()方法结果将是[D]

Relevant Link:

相关链接:

Check this Demo projectfor better understanding.

检查此演示项目以获得更好的理解。