Q: Getting the error “Java.lang.IllegalStateException Activity has been destroyed” when using tabs with ViewPager

D: I have an application that consists of using ActionBarSherlock in tab mode.I have 5 tabs and the content of each tab is handled using fragments. For tab2 though, I have a fragment the xml file of which holds a ViewPager element which in turn has some fragment pages. When I initially start the application the application, I am able to switch between tabs no problem but when I press on tab2 for the second time I get the error mentioned above.
More details: http://stackoverflow.com/questions/15207305

Test Case #1


File ID: #15656428-4-cc


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);
    }
}

  1. So after 1 year, this bug is still not fixed? I got the same error even the latest API and SDK updates...
  2. 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.
  3. This bug is being tracked in the Android Open Source issue tracker: https://code.google.com/p/android/issues/detail?id=42601

Comments Quality
Accurate?:
Precise?:
Concise?:
Useful?: