Stop fragment from being recreated after resume?

前端 未结 3 1785
无人及你
无人及你 2021-01-04 08:06

I am using several fragments to be dynamically added into activity. Everything works fine, when I press back-button, the fragments go

3条回答
  •  伪装坚强ぢ
    2021-01-04 08:22

    Iam using 5 fragments and working for me good as I was facing the same issue before..

    public class MyFragmentView1 extends Fragment {
    
        View v;
        @Override
        public View onCreateView(LayoutInflater inflater,
                                 @Nullable ViewGroup container, 
                    @Nullable Bundle savedInstanceState) {
            if (v == null) 
                v = inflater.inflate(R.layout.my_fragment_view_layout, 
                    container, false
                );
            return v;
        }
    }
    

    I put the view variable inside class and inflating it as new only if the view instance is null or otherwise use the one created before

提交回复
热议问题