Android复习-fragment声明周期与Activity生命周期(个人笔记)

Fragment生命周期(笔记):

onAttach 

onCreate

onCreateView:

onViewCreated

onStart

onReume:

onPause:

onStop

onDestoryView

onDestory

onDetch


fragment的生命周期与Activity绑定的,但是又不能统一而说,比如用fragmentManager动态添加到Activity的fragment,声明周期是这种规律:关于创建的生命周期,是activity先执行,然后fragment后执行,关于销毁和暂停的生命周期,是fragment先执行,activity后执行。 但是如果是activity在xml文件中声明的fragment标签,那么就是关于创建的fragment先执行,activity后执行,关于显示(onResume)的,activity先执行,fragment后执行,关于暂停销毁的,fragment 先执行,activity的后执行。


fragment有一个很重要的方法作为懒加载时的必备 :

setUserVisiableHint(boolean isVisiableToUser) : 这个方法是通过fragmentManager添加fragment时,fragment要显示给用户时必须回调的方法,这个方法被回调并且

isVisiableToUser为true,如果不显示前 先回调到置为false。


tablayout+viewpager + fragment实现懒加载的时候 : 通过setUserVisiableHint + onViewCreated + onDestoryView + isInitialData变量来通过决定loadData。

public class TestFragment extends Fragment {

    public static final String PAGE_INDEX = "page_index";
    private int page = -1;
    private TextView textView;

    private boolean isVisibleToUser;
    private boolean isViewCreated;
    private boolean isInitialData;

    private int params = -2;
    public static TestFragment newInstance(int position) {
        
        Bundle args = new Bundle();
        args.putInt(PAGE_INDEX,position);
        TestFragment fragment = new TestFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        this.isVisibleToUser = isVisibleToUser;
        loadData();
        Log.i("mydata",page + "----setUserVisibleHint :" + isVisibleToUser);
    }

    private void loadData() {
//        Log.i("mydata",page + " : loadadata-->" + "--isVisibleToUser--" + isVisibleToUser + "--isViewCreated--" + isViewCreated + "--isInitialData--" + isInitialData);
        if(isVisibleToUser && isViewCreated && !isInitialData){
            Log.i("mydata",page + "在加载数据");
            isInitialData = true;
            params = new Random().nextInt(10);
            textView.setText(textView.getText() + " : " + params);
        }
    }

    @Override
    public void onAttach(Context context) {
        page = getArguments().getInt(PAGE_INDEX);
        Log.i("mydata",page + "----onAttach");
        super.onAttach(context);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i("mydata",page + "----onCreate");
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Log.i("mydata",page + "----onCreateView");
        return inflater.inflate(R.layout.fragment_layout,null);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Log.i("mydata",page + "----onViewCreated");
        this.isViewCreated = true;
        textView = (TextView) view.findViewById(R.id.tv_fragment);
        textView.setText("index :" + page);
        loadData();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Log.i("mydata",page + "----onActivityCreated");
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.i("mydata",page + "----onStart");
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.i("mydata",page + "----onResume");
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.i("mydata",page + "----onPause");
    }

    @Override
    public void onStop() {
        super.onStop();
        Log.i("mydata",page + "----onStop");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        //不用写的其实,因为若果onDestroyView被执行了,那么下一次要显示fragment的时候必然会先执行onViewCreated方法,那么isViewCreated = true必须会执行。。
        isViewCreated = false;
        Log.i("mydata",page + "----onDestroyView");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //如果fragment是list存储的,那么即使执行了onDestroy ,fragment的实例也不会被销毁,所以isInitialData依然等于true,所以下次的显示的时候无法加载数据
        isInitialData = false;
        Log.i("mydata",page + "----onDestroy");
    }

    @Override
    public void onDetach() {
        super.onDetach();
        Log.i("mydata",page + "----onDetach");
    }
}




你可能感兴趣的:(android)