Android 片段方法:attach()、detach()、remove()、replace()、popBackStack()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12154157/
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 09:54:27  来源:igfitidea点击:

Fragment methods: attach(), detach(), remove(), replace(), popBackStack()

androidandroid-fragmentsfragmentback-stack

提问by Vikram Gupta

I am very confused between these functions and their purposes. What I have observed that using replace()replaces the existing fragment with a new one. We can use addToBackStack(null)to put that fragment in back stack so we can go back to the previously shown fragment. Now when a fragment is added (or replaced) - onAttach()-> onCreate()etc.... methods of the fragment are called in order.

我对这些功能及其目的感到非常困惑。我观察到 usingreplace()用一个新的片段替换了现有的片段。我们可以使用addToBackStack(null)将该片段放在返回堆栈中,以便我们可以返回到先前显示的片段。现在,当添加(或替换)片段时 - onAttach()->onCreate()等等.... 按顺序调用片段的方法。

Now when we call remove()on the fragment from our activity, which functions of the fragment are called and in which order?

现在,当我们remove()从活动中调用片段时,会调用片段的哪些函数以及调用顺序?

What does attach()and detach()do? Does detach()remove the fragment? And when these two attach()and detach()are used, which functions of the fragment are called and in which order??

做什么attach()detach()做什么?是否detach()删除片段?而当使用这两个attach()和时detach(),片段的哪些函数被调用,以什么顺序调用??

Also, what happens on popBackStack()?? I mean which functions are called when we use popBackStack()on the fragment from our activity??

另外,会发生什么popBackStack()??我的意思是当我们popBackStack()在我们的活动中使用片段时会调用哪些函数?

And when does onDestroy() called??

onDestroy() 什么时候调用??

Thank you.

谢谢你。

采纳答案by Adam L. Mónos

Now when we call remove() on the fragment from our activity, which functions of the fragment are called and in which order?

现在,当我们从活动中对片段调用 remove() 时,会调用片段的哪些函数以及调用顺序?

Look at http://developer.android.com/reference/android/app/Fragment.html.

查看http://developer.android.com/reference/android/app/Fragment.html

The order is: onPause(), onStop(), onDestroyView(), onDestroy(), onDetach()

顺序是:onPause(), onStop(), onDestroyView(), onDestroy(),onDetach()

What does attach() and detach() do? Does detach() remove the fragment? And when these two attach() and detach() are used, which functions of the fragment are called and in which order??

attach() 和 detach() 有什么作用?detach() 会删除片段吗?而这两个 attach() 和 detach() 使用的时候,fragment 的哪些函数被调用,顺序是什么??

attach()and detach()is respectively associates or detaches the Fragmentwith/from the Activity. When attaching the Fragment, the onAttach()lifecycle method is called, when detaching, the onDetach()lifecycle method is called in the Fragment. For more information look at the link above.

attach()detach()分别将Fragment与关联或分离Activity。当安装Fragment时,onAttach()生命周期方法被调用,拆卸时,所述onDetach()生命周期方法被称为在Fragment。有关更多信息,请查看上面的链接。

Also, what happens on popBackStack()?? I mean which functions are called when we use popBackStack()on the fragment from our activity??

另外,popBackStack() 会发生什么??我的意思是当我们在我们的活动中的片段上使用 popBackStack() 时会调用哪些函数?

If the Fragmenthasn't been destroyed, then on popBackStack()the onStart()and onResume()methods are called. If the Fragmenthas been destroyed previously, then the lifecycle methods will be called starting from onAttach(). It's the same as, when you press the back button on Activities.

如果Fragment没有被破坏,然后popBackStack()onStart()onResume()方法调用。如果Fragment之前已被销毁,则生命周期方法将从 开始调用onAttach()。这与当您按下 上的后退按钮时相同Activities

回答by emote_control

Just a note on popBackStack(). It doesn't pop a fragment, it pops a fragment transaction. So whatever the last fragment transaction was is reversed. If you were displaying a FragmentAand your transaction was:

只是一个注释popBackStack()。它不会弹出片段,而是弹出片段事务。所以无论最后一个片段交易是什么,都被逆转了。如果您显示的是 aFragmentA并且您的交易是:

fragmentTransaction.replace(R.id.your_layout, fragmentB);
fragmentTransaction.addToBackStack(null);

It would replace FragmentAwith FragmentB, and add that transaction (not the fragment) to the back stack. If you then hit the back button, it pops the back stack and gets the transaction, which was "replace this FragmentAwith a FragmentB". It then reverses that transaction. Backwards, the instruction is to replace whatever the current fragment is with FragmentA. If the original FragmentAstill exists, it uses that one. If it's been destroyed, it makes a new one.

它将替换FragmentAFragmentB,并将该事务(不是片段)添加到返回堆栈中。如果您然后按下后退按钮,它会弹出后退堆栈并获取事务,即“FragmentA用 a替换它FragmentB”。然后撤销该交易。向后,指令是用 替换当前片段的任何内容FragmentA。如果原件FragmentA仍然存在,则使用该原件。如果它被破坏了,它会产生一个新的。

回答by Richa

Suppose fragment A and fragment B was added to a container with the following steps:

假设片段 A 和片段 B 通过以下步骤添加到容器中:

1. Added fragment A =>  .replace(R.id.container, fragmentA) => addToBackStack(null)
2. Added fragment B =>  .replace(R.id.container, fragmentB) => addToBackStack(null)
3. Removed fragment B => fragmentManager.popBackStack();

Callbacks when fm.popBackStack() is called:

调用 fm.popBackStack() 时的回调:

FragmentB: onPause()
FragmentB: onStop()
FragmentB: onDestroy()
FragmentB: onDetach()
FragmentA: onCreateView()
FragmentA: onViewCreated()
FragmentA: onActivityCreated()
FragmentA: onStart()
FragmentA: onResume()

Explanation: Why while removing and destroying fragment B using popBackStack(), fragment A view was created again and resumed?

说明:为什么在使用 popBackStack() 删除和销毁片段 B 时,片段 A 视图又被创建并恢复了?

Ans: When you added fragment B, you used replace() and addToBackStack(), so all the fragments were removed from the container, and fragment B was added to the container. And, also this transaction was recorded in the Back stack. So, when fm.popBackStack() is called, first the transaction is popped out from the back stack and so the operations reverted itself, therefore fragment b is removed from the container and destroyed. And all other fragments get added, for our case fragment A's view is added to the container. Also noted that fragment A's onAttach & onCreate() is not called because it has already been created & attached to the MainActivity earlier.

Ans:添加片段B时,使用了replace()和addToBackStack(),所以所有的片段都从容器中移除,片段B被添加到容器中。而且,这个事务也被记录在 Back 堆栈中。因此,当 fm.popBackStack() 被调用时,首先事务从返回堆栈中弹出,因此操作恢复了自身,因此片段 b 从容器中移除并销毁。并且所有其他片段都被添加,对于我们的案例片段 A 的视图被添加到容器中。还注意到片段 A 的 onAttach & onCreate() 没有被调用,因为它之前已经被创建并附加到 MainActivity。

Back press on fragment B does the same thing:

回按片段 B 做同样的事情:

If you press the back button, it calls fm.popBackStack() and pops the transaction.