Fragment生命周期

Fragment的生命周期

转载http://www.bkjia.com/Androidjc/995847.html

1.看图:

Fragment生命周期_第1张图片
fragment.jpg

流程:

onAttach()

作用:fragment已经关联到activity

// 这个是 回调函数
    @Override
    public void onAttach(Activity activity) {
            super.onAttach(activity);
            Log.i("onAttach_Fragment");
            
    }

这个时候 activity已经传进来了,获得activity的传递的值,就可以进行 与activity的通信里当然也可以使用getActivity(),前提是这个fragment已经和宿主的activity关联,并且没有脱离他只调用一次。

onCreate()

系统创建fragment的时候回调他,在他里面实例化一些变量 ,这些个变量主要是:当你 暂停、停止的时候 你想保持的数据 ,如果我们要为fragment启动一个后台线程,可以考虑将代码放于此处。 参数是:Bundle savedInstance, 用于保存 Fragment 参数, Fragement 也可以 重写 onSaveInstanceState(BundleoutState) 方法, 保存Fragement状态;可以用于文件保护 ,他只调用一次。

onCreateView()

第一次使用的时候 fragment会在这上面画一个layout出来,为了可以画控件,要返回一个 布局的view,也可以返回null;当系统用到fragment的时候 fragment就要返回他的view,越快越好,所以尽量在这里不要做耗时操作,比如从数据库加载大量数据显示listview,当然线程还是可以的。 给当前的fragment绘制ui布局,可以使用线程更新UI说白了就是加载fragment的布局的。这里一般都先判断是否为null

if(text==null){
      Bundle args=getArguments();
      text=args.getString("text");
}
if (view == null) {
      view = inflater.inflate(R.layout.hello, null);
}

这样进行各判断省得每次都要加载,减少资源消耗

onActivityCreated()

当Activity中的onCreate方法执行完后调用

注意了:从这句官方的话可以看出:当执行onActivityCreated()的时候 activity的onCreate才刚完成。所以在onActivityCreated()调用之前 activity的onCreate可能还没有完成,所以不能在onCreateView()中进行 与activity有交互的UI操作,UI交互操作可以在onActivityCreated()里面进行

所以呢:这个方法主要是初始化那些你需要你的父Activity或者Fragment的UI已经被完整初始化才能初始化的元素。如果在onCreateView里面初始化空间 会慢很多,比如listview等

onStart()

和activity一致 启动, Fragement 启动时回调, 此时Fragement可见;

onResume()

和activity一致  在activity中运行是可见的激活, Fragement 进入前台, 可获取焦点时激活;

onPause()

和activity一致  其他的activity获得焦点,这个仍然可见第一次调用的时候,指的是 用户 离开这个fragment(并不是被销毁),通常用于 用户的提交(可能用户离开后不会回来了)

onStop()

和activity一致  fragment不可见的, 可能情况:activity被stopped了OR fragment被移除但被加入到回退栈中,一个stopped的fragment仍然是活着的如果长时间不用也会被移除

onDestroyView()

Fragment中的布局被移除时调用。
表示fragemnt销毁相关联的UI布局
清除所有跟视图相关的资源
以前以为这里没什么用处其实 大有文章可做,
相信大家都用过ViewPager+Fragment,由于ViewPager的缓存机制,每次都会加载3
页。
例如:有四个 fragment 当滑动到第四页的时候 第一页执行onDestroyView(),但没有
执行onDestroy。他依然和activity关联。当在滑动到第一页的时候又执行了 
onCreateView()。 生命周期可以自己试一下。
那么问题来了。会出现重复加载view的局面,所以这么做(下面是先人的代码)
  @Override
    public void onDestroyView() {
        Log.i("onDestroyView_Fragment");
        if(view!=null){
                        ((ViewGroup)view.getParent()).removeView(view);
        }
        super.onDestroyView();
    }

onDestroy()

销毁fragment对象
跟activity类似了。

onDetach()

Fragment和Activity解除关联的时候调用。
脱离activity

可见fragment的销毁还是很优雅地,一个一个的来。

//开始启动:
05-07 05:55:08.553: I/Log(1990): oncreate
05-07 05:55:08.553: I/Log(1990): onAttach_Fragment
05-07 05:55:08.553: I/Log(1990): onCreate_Fragment
05-07 05:55:08.553: I/Log(1990): onCreateView_Fragment
05-07 05:55:08.553: I/Log(1990): onActivityCreated_Fragment
05-07 05:55:08.553: I/Log(1990): onStart
05-07 05:55:08.553: I/Log(1990): onStart_Fragment
05-07 05:55:08.553: I/Log(1990): onResume
05-07 05:55:08.553: I/Log(1990): onResume_Fragment

//按下home按键
05-07 05:55:28.725: I/Log(1990): onPause_Fragment
05-07 05:55:28.725: I/Log(1990): onPause
05-07 05:55:29.221: I/Log(1990): onStop_Fragment
05-07 05:55:29.221: I/Log(1990): onStop
//再回到界面
05-07 05:55:49.441: I/Log(1990): onRestart
05-07 05:55:49.441: I/Log(1990): onStart
05-07 05:55:49.441: I/Log(1990): onStart_Fragment
05-07 05:55:49.441: I/Log(1990): onResume
05-07 05:55:49.441: I/Log(1990): onResume_Fragment
//销毁activity
05-07 05:59:02.293: I/Log(1990): onPause_Fragment
05-07 05:59:02.293: I/Log(1990): onPause
05-07 05:59:02.757: I/Log(1990): onStop_Fragment
05-07 05:59:02.757: I/Log(1990): onStop
05-07 05:59:02.757: I/Log(1990): onDestroyView_Fragment
05-07 05:59:02.757: I/Log(1990): onDestroy_Fragment
05-07 05:59:02.757: I/Log(1990): onDetach_Fragment
05-07 05:59:02.757: I/Log(1990): onDestroy

原文中:

Lifecycle

Though a Fragment's lifecycle is tied to its owning activity, it has its own wrinkle on the standard activity lifecycle. It includes basic activity lifecycle methods such as onResume(), but also important are methods related to interactions with the activity and UI generation.

The core series of lifecycle methods that are called to bring a fragment up to resumed state (interacting with the user) are:

  1. onAttach(Activity) called once the fragment is associated with its activity.
  2. onCreate(Bundle) called to do initial creation of the fragment.
  3. onCreateView(LayoutInflater, ViewGroup, Bundle) creates and returns the view hierarchy associated with the fragment.
  4. onActivityCreated(Bundle) tells the fragment that its activity has completed its own Activity.onCreate().
  5. onViewStateRestored(Bundle) tells the fragment that all of the saved state of its view hierarchy has been restored.
  6. onStart() makes the fragment visible to the user (based on its containing activity being started).
  7. onResume() makes the fragment begin interacting with the user (based on its containing activity being resumed).

As a fragment is no longer being used, it goes through a reverse series of callbacks:

  1. onPause() fragment is no longer interacting with the user either because its activity is being paused or a fragment operation is modifying it in the activity.
  2. onStop() fragment is no longer visible to the user either because its activity is being stopped or a fragment operation is modifying it in the activity.
  3. onDestroyView() allows the fragment to clean up resources associated with its View.
  4. onDestroy() called to do final cleanup of the fragment's state.
  5. onDetach() called immediately prior to the fragment no longer being associated with its activity.

你可能感兴趣的:(Fragment生命周期)