Fragment生命周期详解

相关图片请点击:详情点击

生命周期分五种常见的情况:

1. fragment第一次加载的时候即,FragmentTransaction.show();

**注:当hide()和show()进行切换的时候,该fragment的生命周期并没有什么变化**
09-15 22:48:14.298  17888-17888/com.lyp.idcard I/TAG﹕ IDCardFragment****onAttach******
09-15 22:48:14.298  17888-17888/com.lyp.idcard I/TAG﹕ IDCardFragment****onCreate
09-15 22:48:14.298  17888-17888/com.lyp.idcard I/TAG﹕ IDCardFragment****onCreateView
09-15 22:48:14.301  17888-17888/com.lyp.idcard I/TAG﹕ IDCardFragment****onActivityCreated
09-15 22:48:14.303  17888-17888/com.lyp.idcard I/TAG﹕ IDCardFragment****onStart
09-15 22:48:14.308  17888-17888/com.lyp.idcard I/TAG﹕ IDCardFragment****onResume

2.锁屏和解锁过程

  • 锁屏状态
09-15 22:48:45.803  17888-17888/com.lyp.idcard I/TAG﹕ IDCardFragment****onPause
09-15 22:48:45.817  17888-17888/com.lyp.idcard I/TAG﹕ IDCardFragment****onStop
  • 解锁状态
09-15 22:50:59.456  17888-17888/com.lyp.idcard I/TAG﹕ IDCardFragment****onStart
09-15 22:50:59.468  17888-17888/com.lyp.idcard I/TAG﹕ IDCardFragment****onResume

3.跳转到另一个Activity,重新回到该Activity

  • 跳转到另一个Activity
09-15 22:54:04.092  17888-17888/com.lyp.idcard I/TAG﹕ IDCardFragment****onPause
09-15 22:54:04.682  17888-17888/com.lyp.idcard I/TAG﹕ IDCardFragment****onStop
  • 从另一个Activity回来
09-15 22:54:19.711  17888-17888/com.lyp.idcard I/TAG﹕ IDCardFragment****onStart
09-15 22:54:19.711  17888-17888/com.lyp.idcard I/TAG﹕ IDCardFragment****onResume

4.回到主屏幕(按下HOME)和返回到该界面

  • 回到主屏幕
09-15 22:56:49.570  17888-17888/com.lyp.idcard I/TAG﹕ IDCardFragment****onPause
09-15 22:56:49.971  17888-17888/com.lyp.idcard I/TAG﹕ IDCardFragment****onStop
  • 回到该界面
09-15 22:57:01.098  17888-17888/com.lyp.idcard I/TAG﹕ IDCardFragment****onStart
09-15 22:57:01.099  17888-17888/com.lyp.idcard I/TAG﹕ IDCardFragment****onResume

5.退出应用

09-15 23:00:08.713  25008-25008/com.lyp.idcard I/TAG﹕ IDCardFragment****onPause
09-15 23:00:09.015  25008-25008/com.lyp.idcard I/TAG﹕ IDCardFragment****onStop
09-15 23:00:09.015  25008-25008/com.lyp.idcard I/TAG﹕ IDCardFragment****onDestroyView
09-15 23:00:09.016  25008-25008/com.lyp.idcard I/TAG﹕ IDCardFragment****onDestroy
09-15 23:00:09.016  25008-25008/com.lyp.idcard I/TAG﹕ IDCardFragment****onDetach

示例代码:

public class IDCardFragment extends Fragment {

    public static IDCardFragment newInstance(){
        IDCardFragment fragment = new IDCardFragment();
        return fragment;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        Log.i("TAG","IDCardFragment****onAttach******");

    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i("TAG", "IDCardFragment****onCreate");

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Log.i("TAG","IDCardFragment****onCreateView");
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_idcard_layout, container, false);
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Log.i("TAG", "IDCardFragment****onActivityCreated");
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.i("TAG", "IDCardFragment****onStart");
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.i("TAG", "IDCardFragment****onResume");
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.i("TAG", "IDCardFragment****onPause");
    }

    @Override
    public void onStop() {
        super.onStop();
        Log.i("TAG", "IDCardFragment****onStop");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.i("TAG", "IDCardFragment****onDestroyView");
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i("TAG", "IDCardFragment****onDestroy");

    }

    @Override
    public void onDetach() {
        super.onDetach();
        Log.i("TAG", "IDCardFragment****onDetach");

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值