Java – use add () in the fragmenttransaction addToBackStack(),add(). Detach() and replace() What’s the difference between addtobackstack()?

In the FragmentTransaction project on Android docs, the method replace () is described, which is the same as calling the remove () method for all the fragments added to the current view, and then the add () method is called. In this case, in order to restore the previous fragment, we can use addbacktostack (), which means that the transaction state is still managed by the fragment manager, and it will reverse its operation when we pop the stack

On the other hand, when we use add () to implement transactions, in addition to using add () In addition to addbacktostack (), we can use the detach () method and attach () to recover the fragment. These behaviors are the same as addbacktostack ()

So what is the difference behind these scenes?

Solution

I've studied more. Obviously, the difference between detach () and addtobackstack () lies in the fragment life cycle When we add a fragment to the stack later, we call the methods after onpause(), onstop() and ondestroyview() In this state, the fragment cleans up the resources associated with its view and waits there to be called again Returning to the layout from the back stack is called oncreateview () and just draws its user interface for the fragment In fact, this clip has not been destroyed

On the other hand, when we use detach() to delete or replace a fragment, we first call all the same methods (onpause(), onstop(), ondestroyview()) to clean up the state of the fragment and ondetach() to separate the fragment from its activity

Basically, behind the scenes, they don't have the same behavior: use the addtobackstack () fragment to keep instantiation and detach (), don't

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>