<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>
一個Activity中放入2個Fragment,然后測試Activity和2個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后才開始依次初始化每個Fragment,每個Fragment先onAttach,然后onCreate,onCreateView,onViewCreated,這里值得注意的是,與Activity不同,F(xiàn)ragment的onCreate中,并沒有創(chuàng)建、呈現(xiàn)子組件,所以在onCreate中,是無法訪問子組件的。在onCreateView運行完成后,F(xiàn)ragment中才創(chuàng)建添加了子組件。所以至少要再onViewCreated中,才能用 fragment.getView().findViewById(R.id.xxx)獲取子組件的引用。
2、主Activity中所有Fragment依次執(zhí)行完onViewCreated之后,Activity才onStart開始顯示界面。這時每個Fragment會調(diào)用onActivityCreated,僅僅在Activity初次onStart時,每個Fragment才會調(diào)用onActivityCreated,Activity以后再次調(diào)用onStart,F(xiàn)ragment不會再調(diào)用onActivityCreated(根據(jù)onActivityCreated的名稱,這是可想而知的)。如果某個Fragment中的組件想訪問其他Fragment中組件的數(shù)據(jù),那么必須等到該Fragment調(diào)用onActivityCreated時,才能保證其他所有Fragment都已創(chuàng)建了子組件,在onActivityCreated之前,很可能會因為別的Fragment還沒有初始化創(chuàng)建子組件而導(dǎo)致findViewById返回null。每個Fragment執(zhí)行onActivityCreated后,會執(zhí)行onStart。
3、主Activity分別執(zhí)行onResume,onPause,onStop之后,每個Fragment也會跟著Activity之后依次執(zhí)行同名的方法,即Activity.onResume——>Fragment1.onResume——>Fragment2.onResume……。但Fragment沒有onRestart方法,主Activity重新回到棧頂顯示界面,執(zhí)行onRestart,onStart,每個Fragment依次執(zhí)行onStart(沒有onRestart)
4、主Activity退出時,Activity和每個Fragment依次執(zhí)行onPause,onStop。最后主Activity調(diào)用onDestroy。每個Fragment依次調(diào)用onDestroyView,onDestroy,onDetach,注意Fragment最后的回調(diào)方法是onDetach而不是onDestroy
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
本文題目:Activity與Fragment的生命周期測試-創(chuàng)新互聯(lián)
分享地址:http://sd-ha.com/article44/shiee.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營銷、網(wǎng)站導(dǎo)航、商城網(wǎng)站、ChatGPT、定制網(wǎng)站、營銷型網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容