causing a java.IllegalStateException error, No Activity, only when navigating to Fragment for the SECOND time

前端 未结 8 1317
感动是毒
感动是毒 2020-12-08 06:39

I am getting a very puzzling bug that I have no idea how to even begin working through.

I have a simple app with one activity, the views are implemented with Fragme

8条回答
  •  感动是毒
    2020-12-08 07:34

    I followed the link in jeremyvillalobos answer (which was very helpful) that led me to this workaround.

    public class CustomFragment extends Fragment {
        private static final Field sChildFragmentManagerField;
    
        static {
            Field f = null;
            try {
                f = Fragment.class.getDeclaredField("mChildFragmentManager");
                f.setAccessible(true);
            } catch (NoSuchFieldException e) {
                Log.e(LOGTAG, "Error getting mChildFragmentManager field", e);
            }
            sChildFragmentManagerField = f;
        }
    
        @Override
        public void onDetach() {
            super.onDetach();
    
            if (sChildFragmentManagerField != null) {
                try {
                    sChildFragmentManagerField.set(this, null);
                } catch (Exception e) {
                    Log.e(LOGTAG, "Error setting mChildFragmentManager field", e);
                }
            }
        }
    
        ...
    }
    

    It works for me well, without the need to reinstantiate the fragment.

提交回复
热议问题