Android Fragment源碼分析Add方法
前言
本篇我們就來(lái)講講Fragment管理中的 Add() 方法
Add()
在我們動(dòng)態(tài)的添加、管理Fragment中,Add屬于最基礎(chǔ)的方法了; 用法也很簡(jiǎn)單,如下就是向Activity添加一個(gè)Fragment:
getSupportFragmentManager().beginTransaction().add(R.id.fragmenta,new FragmentA()).commit();
一般時(shí)候我們使用到Fragment的時(shí)候,都是不止一個(gè),比如微信界面,底部導(dǎo)航有四個(gè)按鈕,分別對(duì)應(yīng)不同的四個(gè)Fragment,像這種的每點(diǎn)擊一次底部按鈕就切換一下界面的話,我們就可以使用Add()外加hide和show進(jìn)行組合
下面我們簡(jiǎn)單實(shí)現(xiàn)一下,這里我們就弄兩個(gè)Fragment,
這里我們的MainActivity的布局如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.shaoen.lenovo.myapplication.MainActivity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/fragmenta"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/fragmenta_button"
android:text="FragmentA"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<Button
android:id="@+id/fragmentb_button"
android:text="FragmentB"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
下面看MainActivity的內(nèi)容:
package com.shaoen.lenovo.myapplication;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.shaoen.lenovo.myapplication.fragment.FragmentA;
import com.shaoen.lenovo.myapplication.fragment.FragmentB;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private String TAG=MainActivity.class.getSimpleName();
private Button fragmentA_Button;
private Button fragmentB_Button;
private FragmentTransaction transaction;
private FragmentManager fragmentManager;
private Fragment fragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG,"onCreate--執(zhí)行了");
setContentView(R.layout.activity_main);
fragmentManager=getSupportFragmentManager();
transaction= fragmentManager.beginTransaction();
fragment=new FragmentA();
transaction.add(R.id.fragmenta,fragment,"FragmentA").commit();
fragmentA_Button=(Button) findViewById(R.id.fragmenta_button);
fragmentB_Button=(Button) findViewById(R.id.fragmentb_button);
fragmentA_Button.setOnClickListener(this);
fragmentB_Button.setOnClickListener(this);
}
@Override
protected void onStart() {
super.onStart();
Log.i(TAG,"onStart--執(zhí)行了");
}
@Override
protected void onResume() {
super.onResume();
Log.i(TAG,"onResume--執(zhí)行了");
}
@Override
protected void onPause() {
super.onPause();
Log.i(TAG,"onPause--執(zhí)行了");
}
@Override
protected void onStop() {
super.onStop();
Log.i(TAG,"onStop--執(zhí)行了");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i(TAG,"onDestroy--執(zhí)行了");
}
@Override
public void onClick(View v) {
transaction= fragmentManager.beginTransaction();
switch (v.getId()){
case R.id.fragmenta_button:
if (fragment!=null)
transaction.hide(fragment);
fragment= fragmentManager.findFragmentByTag("FragmentA");
if (fragment!=null){
transaction.show(fragment);
}
else {
fragment=new FragmentA();
transaction.add(R.id.fragmenta,fragment,"FragmentA").commit();
}
break;
case R.id.fragmentb_button:
if (fragment!=null)
transaction.hide(fragment);
fragment= fragmentManager.findFragmentByTag("FragmentB");
if (fragment!=null){
transaction.show(fragment);
}
else {
fragment=new FragmentB();
transaction.add(R.id.fragmenta,fragment,"FragmentB").commit();
}
break;
}
}
}這里我們寫(xiě)的比較簡(jiǎn)單,主要是為了看一下他們的執(zhí)行生命周期,在這里我把所以log都打印出來(lái)了
剛開(kāi)始運(yùn)行時(shí)的log如下:
I/MainActivity: onCreate--執(zhí)行了
I/FragmentA: onAttach--執(zhí)行了
I/FragmentA: onCreate--執(zhí)行了
I/FragmentA: onCreateView--執(zhí)行了
I/FragmentA: onActivityCreated--執(zhí)行了
I/FragmentA: onStart--執(zhí)行了
I/MainActivity: onStart--執(zhí)行了
I/MainActivity: onResume--執(zhí)行了
I/FragmentA: onResume--執(zhí)行了
此時(shí)我們點(diǎn)擊FragmentB按鈕;
I/FragmentB: onAttach--執(zhí)行了
I/FragmentB: onCreate--執(zhí)行了
I/FragmentB: onCreateView--執(zhí)行了
I/FragmentB: onActivityCreated--執(zhí)行了
I/FragmentB: onStart--執(zhí)行了
I/FragmentB: onResume--執(zhí)行了
然后我們?cè)诜磸?fù)點(diǎn)擊FragmentA和FragmentB按鈕,發(fā)現(xiàn)沒(méi)有任何log打印,此時(shí)證明FragmentA和FragmentB通過(guò)hide和show方法進(jìn)行切換時(shí),都只會(huì)初始化一次,
下面我們看向replace這個(gè)方法
replace:
首先replace方法,其實(shí)是remove和add方法的組合; remove就是將一個(gè)Fragment從FragmentManager中刪除,如果我們切換下一個(gè)Fragment時(shí),上一個(gè)Fragment不需要了,可以直接使用replace,如果我們還需要的話,API中也提供了相應(yīng)的方法,那就是加入回退棧addToBackStack()
下面我們把MainActivity中的代碼改一下:
@Override
public void onClick(View v) {
transaction= fragmentManager.beginTransaction();
switch (v.getId()){
case R.id.fragmenta_button:
if (fragment!=null)
transaction.hide(fragment);
fragment= fragmentManager.findFragmentByTag("FragmentA");
if (fragment!=null){ Log.i(TAG,"fragment不為空");
transaction.show(fragment);
}
else {
Log.i(TAG,"fragment為空");
fragment=new FragmentA();
transaction.replace(R.id.fragmenta,fragment,"FragmentA").addToBackStack("FragmentA").commit();
}
break;
case R.id.fragmentb_button:
if (fragment!=null)
transaction.hide(fragment);
fragment= fragmentManager.findFragmentByTag("FragmentB");
if (fragment!=null){
Log.i(TAG,"fragment不為空");
transaction.show(fragment);
}
else {
Log.i(TAG,"fragment為空");
fragment=new FragmentB();
transaction.replace(R.id.fragmenta,fragment,"FragmentB").addToBackStack("FragmentB").commit();
}
break;
}
}這里我們就改了一下OnClick中的代碼,這時(shí)我們?cè)俅蛴∫幌耹og看看:
首先初始化時(shí)是一致的:
這里寫(xiě)代碼片
此時(shí)我們點(diǎn)擊FragmentB:
12-18 21:48:14.227 21081-21081/com.shaoen.lenovo.myapplication I/MainActivity: fragment為空
12-18 21:48:14.228 21081-21081/com.shaoen.lenovo.myapplication I/FragmentA: onPause--執(zhí)行了
12-18 21:48:14.228 21081-21081/com.shaoen.lenovo.myapplication I/FragmentA: onStop--執(zhí)行了
12-18 21:48:14.228 21081-21081/com.shaoen.lenovo.myapplication I/FragmentA: onDestroyView--執(zhí)行了
12-18 21:48:14.229 21081-21081/com.shaoen.lenovo.myapplication I/FragmentB: onAttach--執(zhí)行了
12-18 21:48:14.229 21081-21081/com.shaoen.lenovo.myapplication I/FragmentB: onCreate--執(zhí)行了
12-18 21:48:14.250 21081-21081/com.shaoen.lenovo.myapplication I/FragmentB: onCreateView--執(zhí)行了
12-18 21:48:14.250 21081-21081/com.shaoen.lenovo.myapplication I/FragmentB: onActivityCreated--執(zhí)行了
12-18 21:48:14.250 21081-21081/com.shaoen.lenovo.myapplication I/FragmentB: onStart--執(zhí)行了
12-18 21:48:14.250 21081-21081/com.shaoen.lenovo.myapplication I/FragmentB: onResume--執(zhí)行了
我們發(fā)現(xiàn)Fragment調(diào)用了destroy方法,此時(shí)我們?cè)冱c(diǎn)擊FragmentA:
I/MainActivity: fragment不為空
此時(shí)發(fā)現(xiàn)FragmentA沒(méi)有切換過(guò)來(lái),這是因?yàn)?,我們?cè)贔ragmentManager中找到了FragmentA的實(shí)例,但是此時(shí),F(xiàn)ragmentA的界面已經(jīng)被銷(xiāo)毀了,所以我們看見(jiàn)的還是FragmentB,此時(shí)我們的OnClick改成如下:
@Override
public void onClick(View v) {
transaction= fragmentManager.beginTransaction();
switch (v.getId()){
case R.id.fragmenta_button:
fragment=new FragmentA();
transaction.replace(R.id.fragmenta,fragment,"FragmentA").addToBackStack("FragmentA").commit();
break;
case R.id.fragmentb_button:
fragment=new FragmentB();
transaction.replace(R.id.fragmenta,fragment,"FragmentB").addToBackStack("FragmentB").commit();
break;
}
}這時(shí)再打印一下log,
12-18 21:48:14.228 21081-21081/com.shaoen.lenovo.myapplication I/FragmentA: onPause--執(zhí)行了
12-18 21:48:14.228 21081-21081/com.shaoen.lenovo.myapplication I/FragmentA: onStop--執(zhí)行了
12-18 21:48:14.228 21081-21081/com.shaoen.lenovo.myapplication I/FragmentA: onDestroyView--執(zhí)行了
12-18 21:48:14.229 21081-21081/com.shaoen.lenovo.myapplication I/FragmentB: onAttach--執(zhí)行了
12-18 21:48:14.229 21081-21081/com.shaoen.lenovo.myapplication I/FragmentB: onCreate--執(zhí)行了
12-18 21:48:14.250 21081-21081/com.shaoen.lenovo.myapplication I/FragmentB: onCreateView--執(zhí)行了
12-18 21:48:14.250 21081-21081/com.shaoen.lenovo.myapplication I/FragmentB: onActivityCreated--執(zhí)行了
12-18 21:48:14.250 21081-21081/com.shaoen.lenovo.myapplication I/FragmentB: onStart--執(zhí)行了
12-18 21:48:14.250 21081-21081/com.shaoen.lenovo.myapplication I/FragmentB: onResume--執(zhí)行了
12-18 21:48:14.228 21081-21081/com.shaoen.lenovo.myapplication I/FragmentB: onPause--執(zhí)行了
12-18 21:48:14.228 21081-21081/com.shaoen.lenovo.myapplication I/FragmentB: onStop--執(zhí)行了
12-18 21:48:14.228 21081-21081/com.shaoen.lenovo.myapplication I/FragmentB: onDestroyView--執(zhí)行了
12-18 21:48:14.229 21081-21081/com.shaoen.lenovo.myapplication I/FragmentA: onAttach--執(zhí)行了
12-18 21:48:14.229 21081-21081/com.shaoen.lenovo.myapplication I/FragmentA: onCreate--執(zhí)行了
12-18 21:48:14.250 21081-21081/com.shaoen.lenovo.myapplication I/FragmentA: onCreateView--執(zhí)行了
12-18 21:48:14.250 21081-21081/com.shaoen.lenovo.myapplication I/FragmentA: onActivityCreated--執(zhí)行了
12-18 21:48:14.250 21081-21081/com.shaoen.lenovo.myapplication I/FragmentA: onStart--執(zhí)行了
12-18 21:48:14.250 21081-21081/com.shaoen.lenovo.myapplication I/FragmentA: onResume--執(zhí)行了
此時(shí)發(fā)現(xiàn)每次切換時(shí),都會(huì)調(diào)用Fragment都會(huì)重新調(diào)用onCreateView()到onDestroyView()的所有方法,其實(shí)就是Fragment的布局層整個(gè)銷(xiāo)毀到重建的過(guò)程
注: 當(dāng)我們進(jìn)行Fragment嵌套時(shí),如果我們點(diǎn)擊返回鍵,不想回到上一個(gè)Fragment,而想直接回到更往前一個(gè),或者更往前的Fragment,我們可以使用FragmentManager.popBackStackImmediate (String tag, int flags)方法,彈出TAG為tag的Fragment,同時(shí)把此Fragment以上的Fragment全都彈出(彈出回退棧,即徹底銷(xiāo)毀,detach)
到此這篇關(guān)于Android Fragment源碼分析Add方法的文章就介紹到這了,更多相關(guān)Android Fragment內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android使用Fragment打造萬(wàn)能頁(yè)面切換框架
這篇文章主要介紹了Android使用Fragment打造萬(wàn)能頁(yè)面切換框架的相關(guān)資料,需要的朋友可以參考下2016-01-01
Android string-array數(shù)據(jù)源簡(jiǎn)單使用
這篇文章主要介紹了Android string-array數(shù)據(jù)源簡(jiǎn)單使用的相關(guān)資料,需要的朋友可以參考下2016-09-09
Android ListView優(yōu)化之提高android應(yīng)用效率
android listview優(yōu)化做的好是提高androoid應(yīng)用效率的前提條件,本文給大家介紹Android ListView優(yōu)化之提高android應(yīng)用效率,對(duì)android listview優(yōu)化相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2015-12-12
條件數(shù)據(jù)庫(kù)Android:sqllite的簡(jiǎn)單使用
條件數(shù)據(jù)庫(kù)Android:sqllite的簡(jiǎn)單使用,需要的朋友可以參考一下2013-05-05
Android Popupwindow彈出窗口的簡(jiǎn)單使用方法
這篇文章主要為大家詳細(xì)介紹了Android Popupwindow彈出窗口的簡(jiǎn)單使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
Android實(shí)現(xiàn)檢測(cè)手機(jī)搖晃的監(jiān)聽(tīng)器
本文給大家分享一段代碼實(shí)現(xiàn)檢測(cè)手機(jī)搖晃的監(jiān)聽(tīng)器,代碼簡(jiǎn)單易懂,非常不錯(cuò),感興趣的朋友參考下吧2016-12-12
Android Studio中引入Lambda表達(dá)式的方法
這篇文章主要給大家介紹了在Android Studio中引入Lambda表達(dá)式的方法,文中通過(guò)圖文介紹的非常詳細(xì),對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-03-03
Android使用Intent傳大數(shù)據(jù)簡(jiǎn)單實(shí)現(xiàn)詳解
這篇文章主要為大家介紹了Android使用Intent傳大數(shù)據(jù)簡(jiǎn)單實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03

