Skip to content Skip to sidebar Skip to footer

How To Fix Illegalstateexception: Activity Has Been Destroyed When Showing Dialogfragment After Rotation

I have a DialogFragment that nothing but make a Volley request and populate the data to a RecyclerView. I am launching (via a click on TextView link) this dialog from a fragment.

Solution 1:

Try modifying your DialogFragment to this

//allows the DialogFragment to automatically recreate itself upon rotation@OverridepublicvoidonSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
 }

 // reinitialize the listener if need be still in DialogFragment @OverridepublicvoidonAttach(Activity activity) {
    super.onAttach(activity);
    Log.v(TAG, "onAttach");
    try {
        if (activity != null)
            mTagCatPostClickedListener = (OnTagCatPostClickedListener) activity;
    } catch (ClassCastException e) {
        thrownewClassCastException(activity.toString()
                + " must implement OnTagCatPostClickedListener");
    }
}

and minimize the number of DialogFragment instances created in the DetailsFragment

protectedstaticTagCatFragment tagCatFragment;

privatevoidparseJson(JSONObject jsonObject) {
    //your json stuff hereif (tagCatFragment == null)
        tagCatFragment = TagCatFragment.newInstance("xxx", "yyyy",
                "Author");
    button.setOnClickListener(
            newOnClickListener() {

                @OverridepublicvoidonClick(View arg0) {
                    try {
                        tagCatFragment.show(getChildFragmentManager(),
                                "TagCatFragment");
                    } catch (IllegalStateException e) {
                        e.printStackTrace();
                    }
                }
            });
}

Not sure where your saving the mFragment instance. However you might want to persist your data in the DialogFragment's on onDestroy method. Let me know how it goes.

Post a Comment for "How To Fix Illegalstateexception: Activity Has Been Destroyed When Showing Dialogfragment After Rotation"