Fragment生命周期 setMaxLifecycle

引入

每個fragment都有他自己的生命周期,當用戶操作add们拙,remove,replece時阁吝,fragment的生命周期都會發(fā)生變化砚婆。我在想到底是什么場景需要這個呢?比如:如果用tab切換fragment的話突勇,你們是習慣使用replace還是show射沟,hide呢?一般我們?yōu)榱梭w驗上能快一些都會采用show与境、hide验夯,因為replace相當于remove,add摔刁,show挥转,每次都會重新創(chuàng)建一個新的實例,創(chuàng)建實例就涉及view的創(chuàng)建繪制等共屈,性能肯定比不上show绑谣,hide來得好。但是我這邊的舊項目就是用的replace拗引,為了提高性能借宵,我想把他改成show,hide的方式來矾削,這時候Lifecycle就派上了用場壤玫。

使用

我創(chuàng)建了一個界面只有按鈕豁护,每click一次。將會切換一次fragment欲间,以此來檢驗fragment生命周期的變化

public class FragmentDemoActivity extends AppCompatActivity {
    private FragmentManager mFragmentManager;
    public static final String TAG = "FragmentDemoActivity";
    private Button mBtn;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mFragmentManager = getSupportFragmentManager();
        mBtn = findViewById(R.id.btn);
        mBtn.setText("chang fragment");
        Fragment f1 = new Fragment1();
        Fragment f2 = new Fragment2();
        mBtn.setAllCaps(false);
        mBtn.setOnClickListener(v -> {
            FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
            if (!f1.isAdded()){
                fragmentTransaction.add(R.id.container, f1, "fragment1");
            }else if (!f2.isAdded()){
                fragmentTransaction.add(R.id.container, f2, "fragment2");
            }
            if (!f1.isVisible()){
                fragmentTransaction.show(f1).setMaxLifecycle(f1, Lifecycle.State.RESUMED);
                if (f2.isVisible()){
                    fragmentTransaction.hide(f2).setMaxLifecycle(f2, Lifecycle.State.CREATED);
                }
            }else if (!f2.isVisible()){
                fragmentTransaction.show(f2).setMaxLifecycle(f2, Lifecycle.State.RESUMED);
                if (f1.isVisible()){
                    fragmentTransaction.hide(f1).setMaxLifecycle(f1, Lifecycle.State.CREATED);
                }
            }
            fragmentTransaction.commitNowAllowingStateLoss();
            Log.i(TAG, "onCreate: " + mFragmentManager.getFragments().size());
        });
    }
}

關鍵代碼在于:

if (!f1.isVisible()){
    fragmentTransaction.show(f1).setMaxLifecycle(f1, Lifecycle.State.RESUMED);
    if (f2.isVisible()){
        fragmentTransaction.hide(f2).setMaxLifecycle(f2, Lifecycle.State.CREATED);
    }
}else if (!f2.isVisible()){
    fragmentTransaction.show(f2).setMaxLifecycle(f2, Lifecycle.State.RESUMED);
    if (f1.isVisible()){
        fragmentTransaction.hide(f1).setMaxLifecycle(f1, Lifecycle.State.CREATED);
    }
}

這邊狀態(tài)可以傳入:Lifecycle.State.CREATED楚里、Lifecycle.State.STARTED、Lifecycle.State.RESUME

傳入Lifecycle.State.DESTORYED猎贴、Lifecycle.State.INITIALIZED 會報錯班缎。

image.png

fragmentTransaction.hide(f2).setMaxLifecycle(f2, Lifecycle.State.CREATED);對應的log

2022-06-29 10:31:17.632 10472-10472/com.example.demo D/FragmentManager: movefrom RESUMED: Fragment2{956426b} (fcf2cfed-3d42-44be-9466-1d6170956877 id=0x7f080082 tag=fragment2)
2022-06-29 10:31:17.633 10472-10472/com.example.demo I/Fragment2: onPause: 
2022-06-29 10:31:17.634 10472-10472/com.example.demo D/FragmentManager: movefrom STARTED: Fragment2{956426b} (fcf2cfed-3d42-44be-9466-1d6170956877 id=0x7f080082 tag=fragment2)
2022-06-29 10:31:17.636 10472-10472/com.example.demo I/Fragment2: onStop: 
2022-06-29 10:31:17.637 10472-10472/com.example.demo D/FragmentManager: movefrom ACTIVITY_CREATED: Fragment2{956426b} (fcf2cfed-3d42-44be-9466-1d6170956877 id=0x7f080082 tag=fragment2)
2022-06-29 10:31:17.649 10472-10472/com.example.demo D/FragmentManager: movefrom CREATE_VIEW: Fragment2{956426b} (fcf2cfed-3d42-44be-9466-1d6170956877 id=0x7f080082 tag=fragment2)

fragmentTransaction.show(f2).setMaxLifecycle(f2, Lifecycle.State.RESUMED);對應的log

2022-06-29 10:31:26.256 10472-10472/com.example.demo D/FragmentManager: moveto CREATE_VIEW: Fragment2{956426b} (fcf2cfed-3d42-44be-9466-1d6170956877 id=0x7f080082 tag=fragment2)
2022-06-29 10:31:26.258 10472-10472/com.example.demo I/Fragment2: onCreateView: 
2022-06-29 10:31:26.280 10472-10472/com.example.demo I/Fragment2: onViewCreated: 
2022-06-29 10:31:26.282 10472-10472/com.example.demo D/FragmentManager: moveto ACTIVITY_CREATED: Fragment2{956426b} (fcf2cfed-3d42-44be-9466-1d6170956877 id=0x7f080082 tag=fragment2)
2022-06-29 10:31:26.283 10472-10472/com.example.demo D/FragmentManager: moveto RESTORE_VIEW_STATE: Fragment2{956426b} (fcf2cfed-3d42-44be-9466-1d6170956877 id=0x7f080082 tag=fragment2)
2022-06-29 10:31:26.286 10472-10472/com.example.demo D/FragmentManager: moveto STARTED: Fragment2{956426b} (fcf2cfed-3d42-44be-9466-1d6170956877 id=0x7f080082 tag=fragment2)
2022-06-29 10:31:26.286 10472-10472/com.example.demo I/Fragment2: onStart: 
2022-06-29 10:31:26.299 10472-10472/com.example.demo D/FragmentManager: moveto RESUMED: Fragment2{956426b} (fcf2cfed-3d42-44be-9466-1d6170956877 id=0x7f080082 tag=fragment2)
2022-06-29 10:31:26.300 10472-10472/com.example.demo I/Fragment2: onResume: 
image.png

可以看到生命周期從onCreatView開始走,因為view的加載也會重新走她渴,所以效果不大达址。但是如果是將生命周期改成這樣,可以看到fragment只走了onPause趁耗,再一次show時走了onResume沉唠,少做了很多事情。

if (!f1.isVisible()){
    fragmentTransaction.show(f1).setMaxLifecycle(f1, Lifecycle.State.RESUMED);
    if (f2.isVisible()){
        fragmentTransaction.hide(f2).setMaxLifecycle(f2, Lifecycle.State.STARTED);
    }
}else if (!f2.isVisible()){
    fragmentTransaction.show(f2).setMaxLifecycle(f2, Lifecycle.State.RESUMED);
    if (f1.isVisible()){
        fragmentTransaction.hide(f1).setMaxLifecycle(f1, Lifecycle.State.STARTED);
    }
}
image.png
2022-06-29 10:45:27.769 11190-11190/com.example.demo D/FragmentManager: movefrom RESUMED: Fragment2{956426b} (c694ebf8-2a32-4a8a-9748-09520ca42a21 id=0x7f080082 tag=fragment2)
2022-06-29 10:45:27.770 11190-11190/com.example.demo I/Fragment2: onPause: 

2022-06-29 10:45:45.381 11190-11190/com.example.demo D/FragmentManager: moveto RESUMED: Fragment2{956426b} (c694ebf8-2a32-4a8a-9748-09520ca42a21 id=0x7f080082 tag=fragment2)
2022-06-29 10:45:45.382 11190-11190/com.example.demo I/Fragment2: onResume: 

那如果不設置生命周期呢对粪,操作show右冻,hide就沒有任何生命周期上的變化了,僅僅是將fragment所在的view設置為gone著拭,或者visible纱扭。

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市儡遮,隨后出現(xiàn)的幾起案子乳蛾,更是在濱河造成了極大的恐慌,老刑警劉巖鄙币,帶你破解...
    沈念sama閱讀 218,941評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件肃叶,死亡現(xiàn)場離奇詭異,居然都是意外死亡十嘿,警方通過查閱死者的電腦和手機因惭,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,397評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來绩衷,“玉大人蹦魔,你說我怎么就攤上這事】妊啵” “怎么了勿决?”我有些...
    開封第一講書人閱讀 165,345評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長招盲。 經(jīng)常有香客問我低缩,道長,這世上最難降的妖魔是什么曹货? 我笑而不...
    開封第一講書人閱讀 58,851評論 1 295
  • 正文 為了忘掉前任咆繁,我火速辦了婚禮讳推,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘么介。我一直安慰自己娜遵,他們只是感情好蜕衡,可當我...
    茶點故事閱讀 67,868評論 6 392
  • 文/花漫 我一把揭開白布壤短。 她就那樣靜靜地躺著,像睡著了一般慨仿。 火紅的嫁衣襯著肌膚如雪久脯。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,688評論 1 305
  • 那天镰吆,我揣著相機與錄音帘撰,去河邊找鬼。 笑死万皿,一個胖子當著我的面吹牛摧找,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播牢硅,決...
    沈念sama閱讀 40,414評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼蹬耘,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了减余?” 一聲冷哼從身側(cè)響起综苔,我...
    開封第一講書人閱讀 39,319評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎位岔,沒想到半個月后如筛,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,775評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡抒抬,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年杨刨,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片擦剑。...
    茶點故事閱讀 40,096評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡妖胀,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出抓于,到底是詐尸還是另有隱情做粤,我是刑警寧澤,帶...
    沈念sama閱讀 35,789評論 5 346
  • 正文 年R本政府宣布捉撮,位于F島的核電站怕品,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏巾遭。R本人自食惡果不足惜肉康,卻給世界環(huán)境...
    茶點故事閱讀 41,437評論 3 331
  • 文/蒙蒙 一闯估、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧吼和,春花似錦诊沪、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,993評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至末捣,卻和暖如春侠姑,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背箩做。 一陣腳步聲響...
    開封第一講書人閱讀 33,107評論 1 271
  • 我被黑心中介騙來泰國打工莽红, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人邦邦。 一個月前我還...
    沈念sama閱讀 48,308評論 3 372
  • 正文 我出身青樓安吁,卻偏偏與公主長得像,于是被迫代替她去往敵國和親燃辖。 傳聞我的和親對象是個殘疾皇子鬼店,可洞房花燭夜當晚...
    茶點故事閱讀 45,037評論 2 355

推薦閱讀更多精彩內(nèi)容