Fragment生命周期

Fragment的生命周期

轉載http://www.bkjia.com/Androidjc/995847.html

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出來,為了可以畫控件,<u>要返回一個 布局的view,也可以返回null</u>;當系統用到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.
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容