开发时发现在使用FragmentTabHost切换Fragment时,前一个Fragment会被销毁,切换回来会重新实例化
为了避免这种情况可以使用如下办法:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (rootView == null) {
rootView = inflater.inflate(R.layout.fragment, null);
}
ViewGroup parent=(ViewGroup) rootView.getParent();
if(parent!=null){
parent.removeView(rootView);
}
return rootView;
}