欧美三级国产三级日韩三级_亚洲熟妇丰满大屁股熟妇_欧美亚洲成人一区二区三区_国产精品久久久久久模特

Android百日程序:Fragment動態(tài)管理和生命期 - 新聞資訊 - 云南小程序開發(fā)|云南軟件開發(fā)|云南網(wǎng)站建設(shè)-昆明葵宇信息科技有限公司

159-8711-8523

云南網(wǎng)建設(shè)/小程序開發(fā)/軟件開發(fā)

知識

不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價值,我們在追求其視覺表現(xiàn)的同時,更側(cè)重于功能的便捷,營銷的便利,運營的高效,讓網(wǎng)站成為營銷工具,讓軟件能切實提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序為后期升級提供便捷的支持!

您當前位置>首頁 » 新聞資訊 » 技術(shù)分享 >

Android百日程序:Fragment動態(tài)管理和生命期

發(fā)表時間:2020-10-19

發(fā)布人:葵宇科技

瀏覽次數(shù):51


之前寫過Fragment應(yīng)用的法度榜樣,F(xiàn)ragment可以靜態(tài),也可以動態(tài)載入內(nèi)存中的,這一章進一步看看若何動態(tài)地改換Fragment和看看Fragment生命期都有什么函數(shù)。
本章應(yīng)用響應(yīng)菜單點擊事宜,輪流載入不合的Fragment,顯示不合的界面,效不雅如下:
開端的是沒有載入Fragmen為空白:
[img]http://img.blog.csdn.net/20150102205328964?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQva2VuZGVuMjM=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
點擊菜單的NEXT FRAGMENT VIEW,就進入下一界面,載入兩個:
[img]http://img.blog.csdn.net/20150102205544294?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQva2VuZGVuMjM=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
持續(xù)點擊顯示Fragment 1:
[img]http://img.blog.csdn.net/20150103082318046?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQva2VuZGVuMjM=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
持續(xù)點擊,顯示Fragment2:
[img]http://img.blog.csdn.net/20150103082350890?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQva2VuZGVuMjM=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
然后就是輪回了:
[img]http://img.blog.csdn.net/20150103082433353?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQva2VuZGVuMjM=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
如斯輪回顯示不合畫面。
一 起首實現(xiàn)這一效不雅的關(guān)鍵代碼,就是在菜單響應(yīng)函數(shù)中輸入如下代碼:
	@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 是治理所有Fragment的類,故此查找已有的Fragment應(yīng)用函數(shù): Fragment frag2 = fragManager.findFragmentByI(R.id.framelayout2);留意要斷定是否取回Fragment,如不雅沒有那么frag2 == null。
2 FragmentTransaction治理Fragment變革事務(wù),所有動作,如例子中的remove, WordStr,都必須是在beginTransaction()和commit()之間才會生效,反復(fù)調(diào)用commit而沒有調(diào)用beginTransaction會法度榜樣崩潰的。
3 addToBackStack(null),就是手動把Fragment放在棧中,如許可以應(yīng)用back按鍵退回上一層的Fragment安排。Activity是主動放到棧中的。
4 簡單邏輯:turnNum是全局變量,根據(jù)這個turnNum的值的不合應(yīng)用不合的Fragment。
二 主界面的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>

兩個FrameLayout用來裝Fragment
三 菜單res/menu/main.xml的代碼:
<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生命期的全部代碼:
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");
	}
}

重要和Activity不合的是:
onAttached() : Fragment和Activity接起來的是調(diào)用
onCreateView(): Fragment的View創(chuàng)建
onActivityCreated():當Activity的onCreate()調(diào)用的時刻調(diào)用
onDestroyView(): 當Fragment的view刪除的時刻調(diào)用
onDetach(): Fragment大年夜Activity中去掉落的是調(diào)用

相關(guān)案例查看更多