DialogFrament遇到的兩個坑
阿新 • • 發佈:2018-12-31
DialogFragment碰到的兩個坑:
坑1:
如果要複用,在show的時候,要判斷是否add:
if(loadingDialog.isAdded()) {
((Activity)context).getFragmentManager().beginTransaction().remove(loadingDialog).commit();
}
先把 stack裡舊DialogFragment移掉,不然會導致重複新增
在dismiss、show的時候,需要判斷所依付的activity是否finish,一般是在網路請求過程中,cancel掉,跳到其它介面,但仍執行callBack導致的,為了避免stack資訊不一致,使用allow state loss
!= getDialog() && getDialog().isShowing()) {
super.dismissAllowingStateLoss();
}
if(null != activity && !activity.isFinishing() && (null和 if (null != activity && !activity.isFinishing() && null== getDialog() || !getDialog().isShowing())) { FragmentManager fragmentManager = activity.getFragmentManager(); FragmentTransaction transaction = fragmentManager.beginTransaction(); transaction.add(this, tag); transaction.commitAllowingStateLoss(); transaction.show(this); }
super.dismissAllowingStateLoss();
}