絕大部分的app首頁架構均爲Tab + Fragment,當程序發生異常自動恢復,或者app長時間處於後臺恢復後,Fragment出現重影(重疊)等問題。固然部分不顧及頁面層級的小夥伴,每一個Fragment的view都設置了背景,可能就察覺不出來,可是並不表明沒有。而後不少Fragment裏面又還有Fragment的使用不當甚至會出現白屏的現象。bash
Activity在非正常退出(點返回等屬於正常退出)會調用 onSaveInstanceState 方法來保存數據,其中就包括視圖層(View Hierarchy),當該Activity在此被重建時,會調用onRestoreInstanceState方法,以前被實例化過的 Fragment 依然會出如今 Activity 中,而後按照正常生命流程走,在onCreate中FragmentTransaction至關於又再次 add 了 fragment 進去的,hide()和show()方法對以前保存的fragment已經失效了。綜上這些因素致使了多個Fragment重疊在一塊兒架構
若是savedInstanceState不爲null,說明該Activity有保存的實例,在add fragment 時添加標籤,具體看源碼 selectedFragment方法 其中XXX.getClass().getSimpleName()爲Tag 爲演示才這樣寫的app
private void selectedFragment(int position) {
mPosition = position;
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
hideFragment(transaction);
switch (position) {
case 0:
if (fragment1 == null) {
fragment1 = new Fragment1();
transaction.add(R.id.fl_content, fragment1,fragment1.getClass().getSimpleName());
} else {
transaction.show(fragment1);
}
break;
case 1:
if (fragment2 == null) {
fragment2 = new Fragment2();
transaction.add(R.id.fl_content, fragment2,fragment2.getClass().getSimpleName());
} else {
transaction.show(fragment2);
}
break;
case 2:
if (fragment3 == null) {
fragment3 = new Fragment3();
transaction.add(R.id.fl_content, fragment3,fragment3.getClass().getSimpleName());
} else {
transaction.show(fragment3);
}
break;
case 3:
if (fragment4 == null) {
fragment4 = new Fragment4();
transaction.add(R.id.fl_content, fragment4,fragment4.getClass().getSimpleName());
} else {
transaction.show(fragment4);
}
break;
default:
}
transaction.commitAllowingStateLoss();
}
複製代碼
onCreate方法代碼ide
@Override
protected void onCreate(Bundle savedInstanceState) {
super.initData(savedInstanceState);
//不爲null,說明是死而復活,移除已經存在的fragment
if (savedInstanceState != null) {
FragmentTransaction mTransaction = getSupportFragmentManager().beginTransaction();
mTransaction.remove(mManager.findFragmentByTag(Fragment4.class.getSimpleName()));
mTransaction.remove(mManager.findFragmentByTag(Fragment3.class.getSimpleName()));
mTransaction.remove(mManager.findFragmentByTag(Fragment2.class.getSimpleName()));
mTransaction.remove(mManager.findFragmentByTag(Fragment1.class.getSimpleName()));
mTransaction.commitAllowingStateLoss();
}
selectedFragment(mPosition);
......
}
複製代碼
無需爲Fragment 添加Tag 保持最開始的實現邏輯不動 源碼ui
**
* 原理 去除Super 切斷原有恢復邏輯 保存位置
* @param outState
*/
@SuppressLint("MissingSuperCall")
@Override
protected void onSaveInstanceState(Bundle outState) {
/* 記錄當前的position */
outState.putInt("position", mPosition);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
mPosition = savedInstanceState.getInt("position");
selectedFragment(mPosition);
}
複製代碼
當Fragment裏面嵌套Fragment時,沒有使用getChildFragmentManager(),在Activity恢復後沒法獲取FragmentManager內的Fragment,從而出現白屏。spa
Fragment嵌套Fragment時,使用getChildFragmentManager()獲取事務調試