错误提示:
java.lang.IllegalStateException: No activity
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1075)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1070)
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1861)
at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1474)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:931)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
.......
这个问题是在解决上一篇文章(http://blog.csdn.net/leewenjin/article/details/19409863)中指出的问题后出现的。问题解决方法是参考文章:
http://stackoverflow.com/questions/15207305/getting-the-error-java-lang-illegalstateexception-activity-has-been-destroyed
bug出现的原理问题及解决方法是
This seems to be a bug in the newly added support for nested fragments. Basically, the child FragmentManager ends up with a broken internal state when it is detached from the activity. A short-term workaround that fixed it for me is to add the following to onDetach() of every Fragment which you call getChildFragmentManager() on:
解决方法重写onDetach()
- @Override
- public void onDetach() {
- super.onDetach();
- try {
- Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
- childFragmentManager.setAccessible(true);
- childFragmentManager.set(this, null);
- } catch (NoSuchFieldException e) {
- throw new RuntimeException(e);
- } catch (IllegalAccessException e) {
- throw new RuntimeException(e);
- }
- }
其中的 Field 是
引起bug的原因
If you look at the implementation of Fragment, you'll see that when moving to the detached state, it'll reset its internal state. However, it doesn't reset mChildFragmentManager (this is a bug in the current version of the support library). This causes it to not reattach the child fragment manager when the Fragment is reattached, causing the exception you saw.