溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Activity與Fragment的生命周期測(cè)試

發(fā)布時(shí)間:2020-06-05 10:19:19 來(lái)源:網(wǎng)絡(luò) 閱讀:2083 作者:qqskynet 欄目:移動(dòng)開(kāi)發(fā)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                tools:context=".MainActivity">

    <fragment
        android:name="com.cstar.androidstudy.FragPageOne"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        tools:layout="@layout/frag_page_1"/>

    <fragment
        android:name="com.cstar.androidstudy.FragPageTwo"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        tools:layout="@layout/frag_page_2" />
</LinearLayout>

一個(gè)Activity中放入2個(gè)Fragment,然后測(cè)試Activity和2個(gè)Fragment的生命周期

1169-1169/com.cstar.androidstudy D/fragment﹕ MainActivity.onCreate

1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentOne.onAttach!
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentOne.onCreate!
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentOne.onCreateView!
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentOne.onViewCreated!
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentTwo.onAttach
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentTwo.onCreate!
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentTwo.onCreateView!
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentTwo.onViewCreated!

1169-1169/com.cstar.androidstudy D/fragment﹕ MainActivity.onStart!
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentOne.onActivityCreated!
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentTwo.onActivityCreated!
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentOne.onStart!
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentTwo.onStart!

1169-1169/com.cstar.androidstudy D/fragment﹕ MainActivity.onResume!
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentOne.onResume!
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentTwo.onResume!

1169-1169/com.cstar.androidstudy D/fragment﹕ MainActivity.onPause!
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentOne.onPause!
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentTwo.onPause!

1169-1169/com.cstar.androidstudy D/fragment﹕ MainActivity.onStop!
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentOne.onStop!
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentTwo.onStop!

1169-1169/com.cstar.androidstudy D/fragment﹕ MainActivity.onRestart!
1169-1169/com.cstar.androidstudy D/fragment﹕ MainActivity.onStart!
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentOne.onStart!
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentTwo.onStart!

1169-1169/com.cstar.androidstudy D/fragment﹕ MainActivity.onResume!
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentOne.onResume!
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentTwo.onResume!

1169-1169/com.cstar.androidstudy D/fragment﹕ MainActivity.onPause!
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentOne.onPause!
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentTwo.onPause!

1169-1169/com.cstar.androidstudy D/fragment﹕ MainActivity.onStop!
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentOne.onStop!
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentTwo.onStop!

1169-1169/com.cstar.androidstudy D/fragment﹕ MainActivity.onDestroy!
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentOne.onDestroyView!
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentOne.onDestroy!
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentOne.onDetach!
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentTwo.onDestroyView!
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentTwo.onDestroy!
1169-1169/com.cstar.androidstudy D/fragment﹕ FragmentTwo.onDetach!

注解:

1、主Activity onCreate后才開(kāi)始依次初始化每個(gè)Fragment,每個(gè)Fragment先onAttach,然后onCreate,onCreateView,onViewCreated,這里值得注意的是,與Activity不同,F(xiàn)ragment的onCreate中,并沒(méi)有創(chuàng)建、呈現(xiàn)子組件,所以在onCreate中,是無(wú)法訪問(wèn)子組件的。在onCreateView運(yùn)行完成后,F(xiàn)ragment中才創(chuàng)建添加了子組件。所以至少要再onViewCreated中,才能用 fragment.getView().findViewById(R.id.xxx)獲取子組件的引用。


2、主Activity中所有Fragment依次執(zhí)行完onViewCreated之后,Activity才onStart開(kāi)始顯示界面。這時(shí)每個(gè)Fragment會(huì)調(diào)用onActivityCreated,僅僅在Activity初次onStart時(shí),每個(gè)Fragment才會(huì)調(diào)用onActivityCreated,Activity以后再次調(diào)用onStart,F(xiàn)ragment不會(huì)再調(diào)用onActivityCreated(根據(jù)onActivityCreated的名稱,這是可想而知的)。如果某個(gè)Fragment中的組件想訪問(wèn)其他Fragment中組件的數(shù)據(jù),那么必須等到該Fragment調(diào)用onActivityCreated時(shí),才能保證其他所有Fragment都已創(chuàng)建了子組件,在onActivityCreated之前,很可能會(huì)因?yàn)閯e的Fragment還沒(méi)有初始化創(chuàng)建子組件而導(dǎo)致findViewById返回null。每個(gè)Fragment執(zhí)行onActivityCreated后,會(huì)執(zhí)行onStart。


3、主Activity分別執(zhí)行onResume,onPause,onStop之后,每個(gè)Fragment也會(huì)跟著Activity之后依次執(zhí)行同名的方法,即Activity.onResume——>Fragment1.onResume——>Fragment2.onResume……。但Fragment沒(méi)有onRestart方法,主Activity重新回到棧頂顯示界面,執(zhí)行onRestart,onStart,每個(gè)Fragment依次執(zhí)行onStart(沒(méi)有onRestart)


4、主Activity退出時(shí),Activity和每個(gè)Fragment依次執(zhí)行onPause,onStop。最后主Activity調(diào)用onDestroy。每個(gè)Fragment依次調(diào)用onDestroyView,onDestroy,onDetach,注意Fragment最后的回調(diào)方法是onDetach而不是onDestroy

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI