给领导演示应用的时候,某个fragment突然空白了,这就尴尬了
把fragment从v4包一个一个换成了sdk包下的,也不行,又重新过了一遍fragment的hide和show逻辑,没毛病。在生命周期方法里打了log,也正确调用了,但还是偶尔一片空白,是不是inflate layout文件有问题嘞?
以前的代码:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (null != rootView) {
ViewGroup parent = (ViewGroup) rootView.getParent();
if (parent != null) {
parent.removeView(rootView);
}
}else{
rootView = loadView(inflater);
}
bindView(rootView);
return rootView;
}
改了之后的代码:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (null != rootView) {
ViewGroup parent = (ViewGroup) rootView.getParent();
if (parent != null) {
parent.removeView(rootView);
}
}
rootView = loadView(inflater);
bindView(rootView);
return rootView;
}
以前判断非空和加载布局文件是互斥的,非空就不加载了,所以偶尔会出问题。说明了一个问题,非空的时候可能没有加载完成。