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

前端 未结 13 1893
再見小時候
再見小時候 2020-11-22 11:53

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 fr

相关标签:
13条回答
  • 2020-11-22 12:29

    The bug has been fixed in the latest androidx version. And the famous workaround will cause crash now. so we need not it now.

    0 讨论(0)
  • 2020-11-22 12:34

    This one drove me crazy for Xamarin.

    I ran into this with a ViewPager implementation for TabLayout WITHIN a Fragment, that is itself implemented in the DrawerLayout:

     - DrawerLayout
       - DrawerFragment
         - TabLayout
         - TabViewPager
           - TabViewPagerFragments
    

    So you have to implement the following code in your DrawerFragment. Be aware to choose the correct FragmentManager-Path. Because you might have two different FragmentManager References:

    1. Android.Support.V4.App.FragmentManager
    2. Android.App.FragmentManager

    --> Choose the one you use. If you want to make use of the ChildFragmentManager, you had to use the class declaration Android.App.FragmentManager for your ViewPager!

    Android.Support.V4.App.FragmentManager

    Implement the following Method in your "Main" Fragment - in this example: DrawerFragment

    public override void OnDetach() {
        base.OnDetach();
        try {
            Fragment x = this;
            var classRefProp = typeof(Fragment).GetProperty("class_ref", BindingFlags.NonPublic | BindingFlags.Static);
            IntPtr classRef = (IntPtr)classRefProp.GetValue(x);
            var field = JNIEnv.GetFieldID(classRef, "mChildFragmentManager", "Landroid/support/v4/app/FragmentManagerImpl;");
            JNIEnv.SetField(base.Handle, field, IntPtr.Zero);
        }
        catch (Exception e) {
            Log.Debug("Error", e+"");
        }
    }
    

    Android.App.FragmentManager

    public class TabViewPager: Android.Support.V13.App.FragmentPagerAdapter {}
    

    That means you had to init the ViewPager with Android.App.FragmentManager.

    Implement the following Method in your "Main" Fragment - in this example: DrawerFragment

    public override void OnDetach() {
        base.OnDetach();
        try {
            Fragment x = this;
            var classRefProp = typeof(Fragment).GetProperty("class_ref", BindingFlags.NonPublic | BindingFlags.Static);
            IntPtr classRef = (IntPtr)classRefProp.GetValue(x);
            var field = JNIEnv.GetFieldID(classRef, "mChildFragmentManager", "Landroid/app/FragmentManagerImpl;");
            JNIEnv.SetField(base.Handle, field, IntPtr.Zero);
        }
        catch (Exception e) {
            Log.Debug("Error", e+"");
        }
    }
    
    0 讨论(0)
  • 2020-11-22 12:38

    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:

    @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);
        }
    }
    
    0 讨论(0)
  • 2020-11-22 12:38

    Wanted to add that my problem was in an activity where I tried to make a FragmentTransaction in onCreate BEFORE I called super.onCreate(). I just moved super.onCreate() to top of function and was worked fine.

    0 讨论(0)
  • 2020-11-22 12:42

    I had this issue and realized it was because I was calling setContentView(int id) twice in my Activity's onCreate

    0 讨论(0)
  • 2020-11-22 12:45

    I'm having exactly the same problem. The only workaround I've found, is to replace the fragments by a new instance, each time the tabs are changed.

    ft.replace(R.id.fragment_container, Fragment.instantiate(PlayerMainActivity.this, fragment.getClass().getName()));
    

    Not a real solution, but i haven't found a way to reuse the previous fragment instance...

    0 讨论(0)
提交回复
热议问题