先贴一下bugly上抓到的bug信息
一般是使用了Fragment+ViewPager+FragmentStatePagerAdapter+Fragment这种结构才会出现上述的bug。
个人推测应该是Activity异常退出,然后重建Activity时,里面的Adapter中的Fragment会从FragmentManager中进行恢复,在恢复的过程中出错了。
现在网上的做法核心就是在异常退出的时候不保存状态,所以就不存在恢复,就走不到下面的代码,也就不存在报上面的异常了。
下面分析下异常。根据日志可以看出是在FragmentManagerImpl的getFragment()方法中报出的异常。
/**
* Container for fragments associated with an activity.
*/
final class FragmentManagerImpl extends FragmentManager implements LayoutInflater.Factory2 {
...
@Override
@Nullable
public Fragment getFragment(Bundle bundle, String key) {
String who = bundle.getString(key);
if (who == null) {
return null;
}
Fragment f = mActive.get(who);
if (f == null) {
throwException(new IllegalStateException("Fragment no longer exists for key "
+ key + ": unique id " + who));
}
return f;
}
...
}
这个FragmentManagerImpl其实就是我们常用的FragmentManager的具体实现类。在往上看哪里调用了这个getFragment()方法。FragmentStatePagerAdapter中restoreState()调用的
public abstract class FragmentStatePagerAdapter extends PagerAdapter {
...
private final FragmentManager mFragmentManager;
private ArrayList<Fragment.SavedState> mSavedState = new ArrayList<Fragment.SavedState>();
private ArrayList<Fragment> mFragments = new ArrayList<Fragment>();
...
@Override
public void restoreState(@Nullable Parcelable state, @Nullable ClassLoader loader) {
if (state != null) {
Bundle bundle = (Bundle)state;
bundle.setClassLoader(loader);
Parcelable[] fss = bundle.getParcelableArray("states");
mSavedState.clear();
mFragments.clear();
if (fss != null) {
for (int i=0; i<fss.length; i++) {
mSavedState.add((Fragment.SavedState)fss[i]);
}
}
Iterable<String> keys = bundle.keySet();
for (String key: keys) {
if (key.startsWith("f")) {
int index = Integer.parseInt(key.substring(1));
Fragment f = mFragmentManager.getFragment(bundle, key);
if (f != null) {
while (mFragments.size() <= index) {
mFragments.add(null);
}
f.setMenuVisibility(false);
mFragments.set(index, f);
} else {
Log.w(TAG, "Bad fragment at key " + key);
}
}
}
}
}
...
}
这一步看方法名就能理解,是恢复状态的作用。从bundle中获取存储的状态,再通过FragmentManager获取到对应的Fragment然后存储到mFragments中。其实就是对Adapter中的Fragment进行恢复。向上追溯可以看到该方法会被ViewPager调用。
@Override
public class ViewPager extends ViewGroup {
...
public void onRestoreInstanceState(Parcelable state) {
if (!(state instanceof SavedState)) {
super.onRestoreInstanceState(state);
return;
}
SavedState ss = (SavedState) state;
super.onRestoreInstanceState(ss.getSuperState());
if (mAdapter != null) {
mAdapter.restoreState(ss.adapterState, ss.loader);
setCurrentItemInternal(ss.position, false, true);
} else {
mRestoredCurItem = ss.position;
mRestoredAdapterState = ss.adapterState;
mRestoredClassLoader = ss.loader;
}
}
...
}
在ViewPager的onRestoreInstanceState()方法中,如果mAdapter 不为空的话,会走到上面我们看过的mAdapter.restoreState(ss.adapterState, ss.loader);方法。而如果我们不想让他报异常的话,不让他走这个方法就行。就是让下面代码恒成立即可:
if (!(state instanceof SavedState)) {
super.onRestoreInstanceState(state);
return;
}
onRestoreInstanceState对应的是onSaveInstanceState存储的state。
@Override
public Parcelable onSaveInstanceState() {
Parcelable superState = super.onSaveInstanceState();
SavedState ss = new SavedState(superState);
ss.position = mCurItem;
if (mAdapter != null) {
ss.adapterState = mAdapter.saveState();
}
return ss;
}
现在网上流行的方法,就是重写mAdapter方法让其返回null,这样onRestoreInstanceState中判断条件成立,然后就不会走getFragment()就不会报错。
解决方式有两个:
- 第一种 FragmentStatePagerAdapter换成FragmentPagerAdapter
- 第二种 实现FragmentStatePagerAdapter时重写saveState()方法返回 null。
其实FragmentPagerAdapter也是将saveState()方法返回 null
public abstract class FragmentPagerAdapter extends PagerAdapter {
...
@Override
@Nullable
public Parcelable saveState() {
return null;
}
...
}
而FragmentStatePagerAdapter的saveState()方法默认实现如下
public abstract class FragmentStatePagerAdapter extends PagerAdapter {
...
@Override
@Nullable
public Parcelable saveState() {
Bundle state = null;
if (mSavedState.size() > 0) {
state = new Bundle();
Fragment.SavedState[] fss = new Fragment.SavedState[mSavedState.size()];
mSavedState.toArray(fss);
state.putParcelableArray("states", fss);
}
for (int i=0; i<mFragments.size(); i++) {
Fragment f = mFragments.get(i);
if (f != null && f.isAdded()) {
if (state == null) {
state = new Bundle();
}
String key = "f" + i;
mFragmentManager.putFragment(state, key, f);
}
}
return state;
}
...
}
总结:个人感觉这种方式只是一种投机的解决方式,会影响一定的性能(Fragment没能复用,当然这点性能可以忽略不计)。看看能不能找到更好的解决方式。