引入
每個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 會報錯班缎。
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:
可以看到生命周期從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);
}
}
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纱扭。