当使用FragmentTransaction的连接和分离方法方法、FragmentTransaction

2023-09-06 00:10:26 作者:安稳意中人

我刚刚经历了连接()分离()方法 FragmentTransaction :

  

连接():重新连接一个片段后,它已previously已经脱离了UI与分离(片段)。这将导致要重新创建其视图层次,附着到UI,并显示出来。

好了,这是什么意思?

更​​具体地讲,我看到了一个例子:

  mMapFragment =新MapFragment();
ft.beginTransaction(mMapFragment)
  。连接()
  。新增(R.id.container,mMapFragment)
  。承诺();
 

我删除了连接(),再次尝试:我没有看到任何区别。什么是连接做这个例子吗?有什么区别相比,这样的:

  ft.beginTransaction()
  。新增(R.id.container,mMapFragment)
  。承诺();
 

fragment重叠问题,使用savedInstanceState获取保存数据的方法来解决,结果出现空指针

在情况下,上面的例子是不够好以示区别。我只是想知道我们什么时候需要调用连接()分离()明确?这将是更好,如果你能解释相对于添加/删除/替换的差异。

解决方案

我觉得这是更好地看看分离的定义,并删除的 FragmentTransaction文档明白是怎么回事。

分离

  

分离从UI给定的片段。这是相同的状态时   它被放置在背面堆栈:片段被去除从UI,   然而,它的状态仍在积极的片段管理   经理。当进入这个状态视图层次结构被破坏。

删除

  

删除现有片段。如果它被添加到容器中,其图   也从容器中取出。

这意味着:

到拆卸的你只会破坏该片段的看法,但保持其状态在片段经理。然而,通过的删除的你将删除该片段经理的碎片,它的状态;除了它会删除,如果它加入到一个UI容器的片段视图。 所以,他们都消灭片段观点,但分离保持在片段管理器中的片段的状态。

现在是时候看看附加和补充。

添加

  

添加一个片段的活动状态。这个片段可以任选   也有它的观点(如果Fragment.onCreateView返回非null)成   活动的容器视图。

连接

  

再附加一个片段后,它已previously被deatched从UI   与分离(片段)。这使得它的视图层次是   重新创建的,贴在UI中,并显示出来。

这意味着:

将片段将被添加到活动状态,其视图将被添加到定义容器视图后。 但是,通过附加什么,如果片段是不是已经加入到用户界面会显示出来。它只是重视片段经理。但是,如果有人已经加入到在用户界面的容器,之后分离的,通过将它会再次在其容器被显示。最后,你可以使用连接和分离,如果你想暂时摧毁片段查看并希望显示而不失内活动的状态,建立其对未来看法。

I just went through the documentation of the attach() and detach() methods of FragmentTransaction:

attach(): Re-attach a fragment after it had previously been detached from the UI with detach(Fragment). This causes its view hierarchy to be re-created, attached to the UI, and displayed.

Well, what does that mean?

More specifically, I saw an example:

mMapFragment = new MapFragment();
ft.beginTransaction(mMapFragment)
  .attach()
  .add(R.id.container, mMapFragment)
  .commit();

I deleted the attach() and tried again: I did not notice any difference. What does the attach do in this example? What is the difference compared to this:

ft.beginTransaction()
  .add(R.id.container, mMapFragment)
  .commit();

In case the example above is not good enough to show the difference... I just want to know when do we need to call the attach() and detach() explicitly? It would be better if you can explain the difference with respect to add/remove/replace.

解决方案

I think it is better to have a look at definition of Detach and Remove in FragmentTransaction Documentation to understand what is going on.

Detach

Detach the given fragment from the UI. This is the same state as when it is put on the back stack: the fragment is removed from the UI, however its state is still being actively managed by the fragment manager. When going into this state its view hierarchy is destroyed.

Remove

Remove an existing fragment. If it was added to a container, its view is also removed from that container.

It means:

By detaching you only destroy the fragment View but keep its state in the fragment manager. However, by removing you will remove the fragment and its state from the fragment manager; in addition it will remove the fragment view if it was added to a UI container. So both of them destroy the fragment view, but detach keeps the fragment state in the fragment manager.

Now its time to have a look at attach and add.

Add

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.

Attach

Re-attach a fragment after it had previously been deatched from the UI with detach(Fragment). This causes its view hierarchy to be re-created, attached to the UI, and displayed.

It means:

After adding Fragment it will be added to activity state and its view will be added to defined Container view. But by attaching nothing will be displayed if fragment was not already added to UI. It just attaches to fragment manager. However if view was already added to a container in UI and detached after that, by attaching it will be displayed again in its container. Finally you can use attach and detach if you want to destroy fragment View temporarily and want to display and build its view on future without losing its state inside activity.