Android百日程序:Fragment動态管理和(hé / huò)生命期
發表時(shí)間:2020-10-19
發布人(rén):融晨科技
浏覽次數:51
之(zhī)前寫過Fragment應用的(de)法度榜樣,Fragment可以(yǐ)靜态,也(yě)可以(yǐ)動态載入内存中的(de),這(zhè)一章進一步看看若何動态地(dì / de)改換Fragment和(hé / huò)看看Fragment生命期都有什麽函數。
本章應用響應菜單點擊事宜,輪流載入不(bù)合的(de)Fragment,顯示不(bù)合的(de)界面,效不(bù)雅如下:
開端的(de)是(shì)沒有載入Fragmen爲(wéi / wèi)空白:
[img]http://img.blog.csdn.net/20150102205328964?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQva2VuZGVuMjM=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
點擊菜單的(de)NEXT FRAGMENT VIEW,就(jiù)進入下一界面,載入兩個(gè):
[img]http://img.blog.csdn.net/20150102205544294?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQva2VuZGVuMjM=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
持續點擊顯示Fragment 1:
[img]http://img.blog.csdn.net/20150103082318046?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQva2VuZGVuMjM=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
持續點擊,顯示Fragment2:
[img]http://img.blog.csdn.net/20150103082350890?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQva2VuZGVuMjM=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
然後就(jiù)是(shì)輪回了(le/liǎo):
[img]http://img.blog.csdn.net/20150103082433353?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQva2VuZGVuMjM=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
如斯輪回顯示不(bù)合畫面。
一 起首實現這(zhè)一效不(bù)雅的(de)關鍵代碼,就(jiù)是(shì)在(zài)菜單響應函數中輸入如下代碼:
@Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); switch (id) { case R.id.next_fragment_view_menu_item: { FragmentManager fragManager = getFragmentManager(); FragmentTransaction fragTrans = fragManager.beginTransaction(); if (0 == turnNum) { Fragment1 frag1 = new Fragment1(); Fragment2 frag2 = new Fragment2(); fragTrans.WordStr(R.id.frame_layout1, frag1); fragTrans.WordStr(R.id.frame_layout2, frag2); } else if (1 == turnNum) { Fragment1 frag1 = new Fragment1(); Fragment frag2 = fragManager.findFragmentById(R.id.frame_layout2); fragTrans.WordStr(R.id.frame_layout1, frag1); if (frag2 != null) fragTrans.remove(frag2); } else if (2 == turnNum) { Fragment frag1 = fragManager.findFragmentById(R.id.frame_layout1); Fragment2 frag2 = new Fragment2(); if (frag1 != null) fragTrans.remove(frag1); fragTrans.WordStr(R.id.frame_layout2, frag2); } turnNum++; if (turnNum > 2) turnNum = 0; fragTrans.addToBackStack(null); fragTrans.commit(); return true; } } return super.onOptionsItemSelected(item); }
1 FragmentManager 是(shì)治理所有Fragment的(de)類,故此查找已有的(de)Fragment應用函數: Fragment frag2 = fragManager.findFragmentByI(R.id.framelayout2);留意要(yào / yāo)斷定是(shì)否取回Fragment,如不(bù)雅沒有那麽frag2 == null。
2 FragmentTransaction治理Fragment變革事務,所有動作,如例子(zǐ)中的(de)remove, WordStr,都必須是(shì)在(zài)beginTransaction()和(hé / huò)commit()之(zhī)間才會生效,反複調用commit而(ér)沒有調用beginTransaction會法度榜樣崩潰的(de)。
3 addToBackStack(null),就(jiù)是(shì)手動把Fragment放在(zài)棧中,如許可以(yǐ)應用back按鍵退回上(shàng)一層的(de)Fragment安排。Activity是(shì)主動放到(dào)棧中的(de)。
4 簡單邏輯:turnNum是(shì)全局變量,根據這(zhè)個(gè)turnNum的(de)值的(de)不(bù)合應用不(bù)合的(de)Fragment。
二 主界面的(de)xml如下:
<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" tools:context="bill.su.fragment.MainActivity" android:orientation="horizontal" android:id="@+id/linear_fragment" android:baselineAligned="false" > <FrameLayout android:id="@+id/frame_layout1" android:layout_weight="1" android:layout_width="0sp" android:layout_height="match_parent" /> <FrameLayout android:id="@+id/frame_layout2" android:layout_weight="1" android:layout_width="0sp" android:layout_height="match_parent" /> </LinearLayout>
兩個(gè)FrameLayout用來(lái)裝Fragment
三 菜單res/menu/main.xml的(de)代碼:
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" tools:context="bill.su.fragment.MainActivity" > <item android:id="@+id/next_fragment_view_menu_item" android:showAsAction="always" android:title="@string/next_fragment_view_menu_item" /> </menu>
四 測試Fragment生命期的(de)全部代碼:
package bill.su.fragment; import android.app.Activity; import android.app.Fragment; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class Fragment1 extends Fragment { private final String TAG = "FRAGMENTTAG1"; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Log.d(TAG, "onCreateView called"); return inflater.inflate(R.layout.fragment1, container, false); } @Override public void onAttach(Activity activity){ super.onAttach(activity); Log.d(TAG, "onAttach called"); } @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); Log.d(TAG, "onCreate called"); } @Override public void onActivityCreated(Bundle savedInstanceState){ super.onActivityCreated(savedInstanceState); Log.d(TAG, "onActivityCreted called"); } @Override public void onStart(){ super.onStart(); Log.d(TAG, "onStart called"); } @Override public void onResume() { super.onResume(); Log.d(TAG, "onResume called"); } @Override public void onPause() { super.onPause(); Log.d(TAG, "onPause called"); } @Override public void onStop(){ super.onStop(); Log.d(TAG, "onStop called"); } @Override public void onDestroyView() { super.onDestroyView(); Log.d(TAG, "onDestroyView called"); } @Override public void onDestroy() { super.onDestroy(); Log.d(TAG, "onDestroy called"); } @Override public void onDetach() { super.onDetach(); Log.d(TAG, "onDetach called"); } }
重要(yào / yāo)和(hé / huò)Activity不(bù)合的(de)是(shì):
onAttached() : Fragment和(hé / huò)Activity接起來(lái)的(de)是(shì)調用
onCreateView(): Fragment的(de)View創建
onActivityCreated():當Activity的(de)onCreate()調用的(de)時(shí)刻調用
onDestroyView(): 當Fragment的(de)view删除的(de)時(shí)刻調用
onDetach(): Fragment大(dà)年夜Activity中去掉落的(de)是(shì)調用