fragmentation框架pop方法引起Can not perform this action after onSaveInstanceState异常处理

项目踩坑记录

        项目使用fragmentation框架,有个业务场景是通过异步扫描(耗时操作)进行绑定,如果60秒超时则自动关闭该页面,该页面是一个fragment,在框架里调用pop()方法就可以关闭;在正常情况下是没有问题的,但是如果在60秒的过程中手机自动熄屏或者锁屏、home键等操作,当60秒时间到达,调用pop()时就会出现异常:

FATAL EXCEPTION: main

                                                              java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState

                                                                  at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1842)

                                                                  at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:775)

                                                                  at me.yokeyword.fragmentation.FragmentationDelegate.debouncePop(FragmentationDelegate.java:500)

                                                                  at me.yokeyword.fragmentation.FragmentationDelegate.back(FragmentationDelegate.java:487)

                                                                  at me.yokeyword.fragmentation.SupportFragment.pop(SupportFragment.java:652)



第一反应查看 Can not perform this action after onSaveInstanceState这个问题,通过查看pop()方法源码:

SupportFragment类中:

接着看back()方法,跳到FragmentationDelegate类中的方法:

注意到红框中的方法,点过去

在FragmentManager类中

这是一个抽象类,那在哪里有它的实现呢,往下翻


终于在这个实现类中找到了这个方法:



红框中那熟悉的味道

原来是这里抛出了异常,那为什么会有异常呢,从这句异常的提示中我们注意到了这个方法onSaveInstanceState();原来手机自动熄屏或者锁屏、home键等操作会触发这个回调,以防应用被杀死后能迅速恢复到之前的状态,这个方法具体的说明可以去阅读Activity中的源码。那如何避免这个异常呢?网上对于这个异常的处理有很多,可参考http://blog.csdn.net/EdisonChang/article/details/49873669

大多是在activity中对onBackPressed()方法的处理,或者直接重写onSaveInstanceState(),这些方法大多粗暴,要么不能存储状态,要么不能在farament中使用。

我们的项目使用的是单一Activity加多Fragment这种架构(现在发现有很多缺陷),一开始在onSaveInstanceState()方法中尝试用反射的方法去更改mStateSaved的状态为false来防止异常的抛出,但是发现在pop()之前会调用saveAllState()和dispatchStop(),这些方法中又将mStateSaved重置为true,换个思路来想,既然我们要解决的pop()时的问题,那么可以重写pop(),在调用父类方法前对mStateSaved进行赋值,从而避免异常的抛出,而不需要去考虑什么时候执行了onSaveInstanceState()方法;

@Override

public void pop() {

if(!isSupportVisible()){

invokeFragmentManagerNoteStateNotSaved();

  }

super.pop();

}

private MethodnoteStateNotSavedMethod;

private ObjectfragmentMgr;

private String[]activityClassName = {"Activity", "FragmentActivity"};

public void invokeFragmentManagerNoteStateNotSaved() {

//java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState

  if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {

return;

  }

try {

if (noteStateNotSavedMethod !=null &&fragmentMgr !=null) {

noteStateNotSavedMethod.invoke(fragmentMgr);

return;

      }

Class cls =_mActivity. getClass();

      do {

cls = cls.getSuperclass();

      }while (!(activityClassName[0].equals(cls.getSimpleName())

||activityClassName[1].equals(cls.getSimpleName())));

      Field fragmentMgrField = prepareField(cls, "mFragments");

      if (fragmentMgrField !=null) {

fragmentMgr = fragmentMgrField.get(getActivity());

        noteStateNotSavedMethod = getDeclaredMethod(fragmentMgr, "noteStateNotSaved");

        if (noteStateNotSavedMethod !=null) {

noteStateNotSavedMethod.invoke(fragmentMgr);

        }

}

}catch (Exception ex) {

}

}

private FieldprepareField(Class c, String fieldName)throws NoSuchFieldException {

while (c !=null) {

try {

Field f = c.getDeclaredField(fieldName);

        f.setAccessible(true);

        return f;

      }finally {

c = c.getSuperclass();

      }

}

throw new NoSuchFieldException();

}

private MethodgetDeclaredMethod(Object object, String methodName, Class... parameterTypes) {

Method method =null;

  for (Class clazz = object.getClass(); clazz != Object.class; clazz = clazz.getSuperclass()) {

try {

method = clazz.getDeclaredMethod(methodName, parameterTypes);

        return method;

      }catch (Exception e) {

}

}

return null;

}

通过fragmentation中isSupportVisible()方法可以判断出是否是不可见状态,如果是的话调用反射方法,当然如果简单粗暴一点可以连isSupportVisible()都不判断,毕竟在屏幕旋转时这个方法是不会触发的,而会调用onSaveInstanceState(),引起异常,我们项目中是禁止屏幕旋转的,所以不考虑这种情况。

对于反射大致解释一下,通过反射FragmentActivity类,拿到“mFragments”这个变量,这个变量为FragmentController类型,再反射FragmentController类中的noteStateNotSaved方法,


mHost.mFragmentManager就是FragmentManagerImpl,


从而将mStateSaved置为false

现在就可以从容的在熄屏,home状态下pop()啦。

你可能感兴趣的:(fragmentation框架pop方法引起Can not perform this action after onSaveInstanceState异常处理)