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

前端 未结 3 1211
生来不讨喜
生来不讨喜 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 09:42

    I'm currently reusing the view with something like this:

    if(view == null){
        view = (ViewGroup) inflater.inflate(R.layout.news_list, container, false);
    } else {
        ((ViewGroup) view.getParent()).removeView(view);
    }
    return view;
    

    I don't know if this way is correct, but it seems to work for me..

    NOTE: I'm using this aproach because I have a listview in a fragment, and when user tap on an item it loads a new fragment (fragment manager replace current list fragment using). Then, when user hit backbutton, as I'm reusing the same old view of the fragment (that is not destroyed when removed with FM) then the user continues viewing the list in the position it was before opening detail fragment view.

提交回复
热议问题