Fragment - should I reuse view in onCreateView and how should I do that?

前端 未结 3 1160
生来不讨喜
生来不讨喜 2021-02-04 09:31

Actually, I always reused my view in my fragments like the following:

private View mView = null;

@Override
public View onCreateView(LayoutInflater inflater, Vie         


        
3条回答
  •  深忆病人
    2021-02-04 10:03

    Maybe this can help to understand the behavior. If you check out FragmentManagerImpl.java you will find the following:

    First we create a view by calling onCreateView() (line 845) and then we wrap created view with another view, which becomes a parent of our view (lines 848-849). This means our view does not become a child of real container, but it's a child of a wrapper view now. The problem with reuse happens, when view gets removed from the container (line 998). FragmentManager removes wrapper view from the container, but our real view stays added to the parent wrapper view. This is what causes the issue you experience.

    Thus, if you remove the view from its parent, it can work. Even knowing this, I would not recommend reusing views in the fragment also because views can live a bit longer than fragments, because they can be used in "disappearing" animations even after the fragment is been destroyed. If you try to remove such a view from its parent at that time, then the animation might be broken.

    Another argument to not cache the view is that Android doesn't support view recycling in fragments by design. Remember ListAdapter allowing to reuse the views? Android takes care for caching and proper reusing those views. This is not the case with fragment though.

提交回复
热议问题