Android how to stop refreshing Fragments on tab change

前端 未结 7 1366
小蘑菇
小蘑菇 2020-12-12 22:09

I have the following code :

MainActivity.java

package com.erc.library;

import java.io.BufferedInputStream;
import java.io.File;
imp         


        
7条回答
  •  长情又很酷
    2020-12-12 22:43

    you can handle view recreation by check if the view is null or not

    public class FragmentExample extends Fragment {
    
        private View rootView;
    
        public FragmentExample() {}
    
    @Override
    public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
    
        if (rootView == null) {
    
            rootView = inflater.inflate(R.layout.fragment_example_layout, container, false);
    
            // Initialise your layout here
    
        } else {
            ((ViewGroup) rootView.getParent()).removeView(rootView);
        }
    
        return rootView;
        }
    }
    

提交回复
热议问题