刚上手一个项目 懒的用viewpager+fragment模式,尽管在后面的项目中还是用到viewpager+fragment。先说说问题,多次切换fragment的时候页面出现空白,刚开始以为传递的参数有问题,但是也确实出现这个问题,手贱导致。
比如根据传入的tag实例化的时候,错把第一个数据和第二个传入相同的数据。copy的错,死活切换不了,后来debug才搞定:
/**
* 初始化 newInstance 管理fragment页面 同时设置当期fragment页面
*/
public static BaseFragment newInstance(Context context,String tag){
BaseFragment baseFragment = null;
if(TextUtils.equals(tag, context.getString(R.string.m))){
if(baseFragment==null)
baseFragment = new FragmentA();
}else if(TextUtils.equals(tag, context.getString(R.string.f))){
if(baseFragment==null)
baseFragment = new FragmentB();
}else if(TextUtils.equals(tag, context.getString(R.string.g))){
if(baseFragment==null)
baseFragment = new FragmentC();
}
return baseFragment;
}
=================================
后来问题表现在,第一个fragment加载数据缓慢 在未加载完全的时候切换其他页面的时候出现空白,在网上找了各种方法 刚开始以为缓存方面没做好(确实还没来得及做缓存那块的优化),后来看各种帖子,设置在清单文件设至signTask, ...终于意识到fragment状态没保存。。。
只要在onCreateView中加一个判断就搞定了:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (mView != null) {
ViewGroup parent = (ViewGroup) mView.getParent();
if (parent != null) {
parent.removeView(mView);
}
return mView;
}
View rootView = null;
rootView = inflater.inflate(R.layout.question, container, false);
mView = rootView;
return rootView;
}
================ 2017/12/18 分割线 ===================
无意中看到自己的笔记 现在进行修正 下面的util工具类
public class FragmentUtils {
private final HashMap<String, Fragment> fragments = new HashMap<>();
private final FragmentActivity activity;
private final FragmentManager manager;
private final int container;
private String current;
public FragmentUtils(FragmentActivity activity, @IdRes int container) {
this.activity = activity;
this.container = container;
this.manager = activity.getSupportFragmentManager();
}
/**
* one add show
* @param fragment
*/
public void switchTo(Class<? extends Fragment> fragment) {
String name = fragment.getName();
if (null != current) {
manager.beginTransaction().hide(fragments.get(current)).commit();
}
if (null == manager.findFragmentByTag(name)) {
Fragment instance = Fragment.instantiate(activity, name);
fragments.put(name, instance);
manager.beginTransaction().add(container, instance, name).addToBackStack(name).commit();
} else {
manager.beginTransaction().show(fragments.get(name)).commit();
}
current = name;
}
@Nullable
public Fragment getFragment(Class<? extends Fragment> fragment) {
return fragments.get(fragment.getName());
}
/**
* two replace
*/
public void openFragment(Fragment fragment){
FragmentTransaction fragmentTransaction = manager
.beginTransaction();
fragmentTransaction.replace(container, fragment);
fragmentTransaction.commit();
}
/**
*
* 单纯添加 用的比较少
* The {@code fragment} is added to the container view with id {@code frameId}. The operation is
* performed by the {@code fragmentManager}.
*/
public void addFragmentToActivity (@NonNull Fragment f) {
Fragment fragment = checkNotNull(f);
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(container, fragment);
transaction.commit();
}
}