How to fix IllegalStateException: Activity has bee

2019-07-23 15:29发布

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. When the dialog is launched for the first time, it launches and does what it's supposed to do, no hitch.

Then I rotate the screen, launch the dialog again, and whoosh! I am greeted with "IllegalStateException: Activity has been destroyed".

One weird thing about this error is that I am also launching this same DialogFragment in the onBindViewHolder of an adapter class inside DetailsFragment and even if I rotate the screen ten times, the same dialog opens glitchlessly.

This is the stacktrace,

07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: java.lang.IllegalStateException: Activity has been destroyed
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1515)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:638)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:617)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at android.support.v4.app.DialogFragment.show(DialogFragment.java:139)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at com.ozuf.poster.Fragment.DetailsFragment$9.onClick(DetailsFragment.java:474)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at android.text.method.LinkMovementMethod.onTouchEvent(LinkMovementMethod.java:217)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at android.widget.TextView.onTouchEvent(TextView.java:8464)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at android.view.View.dispatchTouchEvent(View.java:8808)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2581)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2581)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2581)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2581)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2581)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2581)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2581)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2581)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2581)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2581)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2581)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2607)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1779)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at android.app.Activity.dispatchTouchEvent(Activity.java:2846)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2568)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at android.view.View.dispatchPointerEvent(View.java:9003)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4209)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4072)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3627)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3680)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3646)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3763)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3654)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3820)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3627)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3680)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3646)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3654)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3627)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5910)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5884)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5855)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6000)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at android.os.MessageQueue.nativePollOnce(Native Method)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at android.os.MessageQueue.next(MessageQueue.java:143)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at android.os.Looper.loop(Looper.java:122)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5910)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)

This is the DialogFragment

public class TagCatFragment extends DialogFragment {

    private final String TAG = "TagCatFragment";
    private static final String SLUG = "slug";
    private static final String SOURCE = "source";
    private static final String NAME = "name";

    private  List<TagCatItem> mTagCatItems = new ArrayList<>();
    RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    LinearLayoutManager mLayoutManager;

    OnTagCatPostClickedListener mTagCatPostClickedListener = sTagCatCallbacks;

    private int pageNumber = 2;

    public int getPageNumber() {
        return pageNumber++;
    }

    public void setPageNumber(int pageNumber) {
        this.pageNumber = pageNumber;
    }


    private String mSlug;
    private String mSource;
    private String mName;
    Button cancelButton;
    ProgressBar mProgressBar, mProgressBarB;
    TextView header, errorText;
    Button errorButton;
    ImageView cloudOff;
    JsonObjectRequest mJsonObjectRequest, moreJsonObjectRequest;
    View view;

    public TagCatFragment() {
        // Required empty public constructor
    }
    public static TagCatFragment newInstance(String name, String slug, String source) {
        TagCatFragment fragment = new TagCatFragment();
        Bundle args = new Bundle();
        args.putString(SLUG, slug);
        args.putString(SOURCE, source);
        args.putString(NAME, name);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mSlug = getArguments().getString(SLUG);
            mSource = getArguments().getString(SOURCE);
            mName = getArguments().getString(NAME);

            Log.d(TAG, "Slug is " + mSlug + " and Source is " + mSource);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_tag_cat, container, false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceStare) {
        super.onViewCreated(view, savedInstanceStare);
        cancelButton = (Button) view.findViewById(R.id.tagcat_cancel_button);
        mProgressBar = (ProgressBar) view.findViewById(R.id.progress_circle);
        mProgressBarB = (ProgressBar) view.findViewById(R.id.progress_circle_b);
        header = (TextView) view.findViewById(R.id.tagcat_header);
        mRecyclerView = (RecyclerView) view.findViewById(R.id.tagcat_recycler);
        cloudOff = (ImageView) view.findViewById(R.id.cloud_off);
        errorButton = (Button) view.findViewById(R.id.tagcat_retry);
        errorText = (TextView) view.findViewById(R.id.tagcat_error_text);


        mLayoutManager = new LinearLayoutManager(getActivity());
        TagCatAdapter tagCatAdapter = new TagCatAdapter(mTagCatItems, getActivity());
        mRecyclerView.setLayoutManager(mLayoutManager);
        mAdapter = tagCatAdapter;
        mRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity()));
        mRecyclerView.setAdapter(mAdapter);



        tagCatAdapter.setTagCatClickedListener(new UrlTagCatClickedListener() {
            @Override
            public void onUrlTagCatClicked(String postURL) {
                Log.d(TAG, "I go the url and it is " + postURL);
                mTagCatPostClickedListener.OnTagCatPostClicked(postURL);
                dismiss();
            }
        });



        mProgressBar.getIndeterminateDrawable().setColorFilter(ContextCompat.getColor(getActivity(), R.color.colorPrimaryDark), PorterDuff.Mode.SRC_IN);
        mProgressBarB.getIndeterminateDrawable().setColorFilter(ContextCompat.getColor(getActivity(), R.color.colorPrimaryDark), PorterDuff.Mode.SRC_IN);


        cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        errorButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getTagCat();
            }
        });

        getTagCat();
    }


    private void getTagCat() {
        Log.d(TAG, "getTagCat called");
        mProgressBar.setVisibility(View.VISIBLE);
        cloudOff.setVisibility(View.GONE);
        errorText.setVisibility(View.GONE);
        errorButton.setVisibility(View.GONE);
        String url = http://blah-blah-blah;

        Log.d(TAG, "Url is " + url);

        mJsonObjectRequest = new JsonObjectRequest(url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        Log.d(TAG, "getTagCat onResponse called");
                        if (mProgressBar != null) {
                            mProgressBar.setVisibility(View.GONE);
                        }
                        parseResponse(response);

                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.d(TAG, "getTagCat onErrorResponse called");
                        if (mProgressBar != null) {
                            mProgressBar.setVisibility(View.GONE);
                        }
                        cloudOff.setVisibility(View.VISIBLE);
                        errorText.setVisibility(View.VISIBLE);
                        errorButton.setVisibility(View.VISIBLE);
                    }
                });

        int retrytimes = 2;
        RetryPolicy policy = new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS, retrytimes, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
        mJsonObjectRequest.setRetryPolicy(policy);

        //Creating request queue
        RequestQueue requestQueue = Volley.newRequestQueue(getActivity());

        //Adding request to the queue
        requestQueue.add(mJsonObjectRequest);
    }

    private void parseResponse(JSONObject object) {
        Log.d(TAG, "Parsing response");

        try {
            int post_found =  object.getInt("found");
            if (isAdded()) {
                header.setText(getResources().getQuantityString(R.plurals.author_posts, post_found, post_found, mName));

                header.setVisibility(View.VISIBLE);
                Animation headAnim = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_in_right);
                header.setAnimation(headAnim);
            }
            Log.d(TAG, "found posts is  " + post_found);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        try {
            JSONArray array = object.getJSONArray("posts");
            for (int i = 0; i<array.length(); i++) {
                TagCatItem tagCatItem = new TagCatItem();
                JSONObject jsonObject;
                jsonObject = array.getJSONObject(i);
                tagCatItem.setPostTitle(jsonObject.getString("title"));
                tagCatItem.setPostURL(jsonObject.getString("URL"));

                SimpleDateFormat formatDate = new SimpleDateFormat("dd MMM, yy", Locale.getDefault());
                SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
                String inputDateStr = jsonObject.getString("date");
                try {
                    Date inputDate = inputFormat.parse(inputDateStr);
                    String postDateStr = formatDate.format(inputDate);
                    tagCatItem.setPostDate(postDateStr);
                } catch (ParseException e) {
                    Log.d(TAG, "Error in Parsing date");
                }

                mTagCatItems.add(tagCatItem);
                mAdapter.notifyItemRangeChanged(0, mAdapter.getItemCount());

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }


    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Log.d(TAG, "onCreateDialog called");
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        return dialog;
    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        Log.d(TAG, "onAttach called");
        try {
            mTagCatPostClickedListener = (OnTagCatPostClickedListener) context;
        } catch (ClassCastException e) {
            throw  new ClassCastException(context.toString() + " must implement OnTagCatPostClickedListener");
        }
    }

    @Override
    public void onDetach() {
        Log.d(TAG, "onDetach");
        super.onDetach();

        if (mJsonObjectRequest != null) {
            if (!mJsonObjectRequest.isCanceled()) {
                mJsonObjectRequest.cancel();
            }
        }
        if (moreJsonObjectRequest != null) {
            if (!moreJsonObjectRequest.isCanceled()) {
                moreJsonObjectRequest.cancel();
            }
        }
    }

    public interface OnTagCatPostClickedListener {
        void OnTagCatPostClicked (String tagcatLink);
    }

    private static OnTagCatPostClickedListener sTagCatCallbacks = new OnTagCatPostClickedListener() {
        @Override
        public void OnTagCatPostClicked(String tagcatLink) {

        }
    };

    @Override
    public void onActivityCreated(Bundle arg0) {
        super.onActivityCreated(arg0);
        getDialog().getWindow()
                .getAttributes().windowAnimations = R.style.TagCatDialog;
    }
}

This is the Fragment that I am launching the DialogFragment

public class DetailsFragment extends Fragment{
    //Skipped some codes

        @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Log.d(TAG, "onCreateView called");
        url = getArguments().getString("post_link");
        Log.d(TAG, "Post url is " + url);
        // Inflate the layout for this fragment
        view =  inflater.inflate(R.layout.fragment_details, container, false);

               if (savedInstanceState != null) {
            mFragment = getChildFragmentManager().getFragment(savedInstanceState, "mFragment");
            gadData = savedInstanceState.getString("gadData");

           mainPostStr = savedInstanceState.getString("mainPost");

            if (mainPostStr != null) {
            try {
                mainPost = new JSONObject(mainPostStr);
                parseJson(mainPost);
            } catch (JSONException e) {
                e.printStackTrace();
            } else  {
                 getPost()
            }
        return view;
    }

    private void  getPost() {
        Log.d(TAG, "getPost called");
        if (idHasValue) {
            Log.d(TAG, "Post has a value");
            progressBar.setVisibility(View.VISIBLE);
        }
        String postJson = GET_URL + postID;
        JsonObjectRequest postDetails = new JsonObjectRequest(Method.GET, postJson, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        Log.d(TAG, "onResponse for getPOst called");
                        parseJson(response);
                        mainPost = response;
                        if (progressBar != null) {
                            progressBar.setVisibility(View.GONE);
                        }
                    }
                },
                new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d(TAG,  "onErrorResponse for getPost called");
                if (progressBar != null) {
                    progressBar.setVisibility(View.GONE);
                }
                if (sthWrongAlert != null) {
                    sthWrongAlert.show();
                }
            }
        });

        int retrytimes = 2;
        RetryPolicy policy = new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS, retrytimes, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
        postDetails.setRetryPolicy(policy);

        //Creating requestqueue
        RequestQueue requestQueue = Volley.newRequestQueue(getActivity());

        //Adding request queue
        requestQueue.add(postDetails);
    }

    private void parseJson(JSONObject object) {
        Log.d(TAG, "Parsing Json");
        try {
            JSONObject author = object.getJSONObject("author");

            SimpleDateFormat formatDate = new SimpleDateFormat("dd MMMM, yyyy", Locale.getDefault());
            SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
            String inputDateStr = object.getString("date");
            Log.d(TAG, "post date is " + inputDateStr);
            String postDateStr = null;
            try {
                Date inputDate = inputFormat.parse(inputDateStr);
                postDateStr = formatDate.format(inputDate);
            } catch (ParseException e) {
                Log.d(TAG, "Error in Parsing date");
            }
            if (isAdded()) {
                String rawAuthor = String.format(getResources().getString(R.string.post_by), name, postDateStr);
                int i1 = rawAuthor.indexOf(" by ");
                int i2 = rawAuthor.indexOf(" on ");
                postAuthorDate.setText(rawAuthor, TextView.BufferType.SPANNABLE);
                postAuthorDate.setMovementMethod(LinkMovementMethod.getInstance());
                postAuthorDate.setHighlightColor(ContextCompat.getColor(getActivity(),  R.color.nav_bg));
                Spannable spannable = (Spannable)postAuthorDate.getText();
                ClickableSpan clickableSpan = new ClickableSpan() {
                    @Override
                    public void onClick(View widget) {
                        Log.d(TAG, "I was clicked");
                        //mOnTagCatClickedListener.OnTatCatClicked(name, authorId, "Author");
                        TagCatFragment tagCatFragment = TagCatFragment.newInstance(name, authorId, "Author");
                        try {
                            tagCatFragment.show(getChildFragmentManager(), "TagCAatFragment"); // This causes IllegalStateException but the one in onBindViewHolder below doesn't.
                        } catch (IllegalStateException e) {
                            e.printStackTrace();
                        }
                    }
                };
                spannable.setSpan(clickableSpan, i1+4, i2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }

           // Skipped some codes
        } catch (JSONException w) {
            w.printStackTrace();

        }

    }public class TagAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

      // Skipped some codes

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            final TagItem tagItem = mTagItems.get(position);
            ((TextViewHolder) holder).tagName.setText(tagItem.getTagName());
            ((TextViewHolder) holder).tagName.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //mOnTagCatClickedListener.OnTatCatClicked(tagItem.getTagName(), tagItem.getTagSlug(), "Tag");
                    TagCatFragment tagCatFragment = TagCatFragment.newInstance(tagItem.getTagName(), tagItem.getTagSlug(), "Tag");
                    try {
                        tagCatFragment.show(getChildFragmentManager(), "TagCatFragment");
                    } catch (IllegalStateException e) {
                        e.printStackTrace();
                    }
                }
            });

        }
    }
}

Tried Solutions:

I have tried the popular Marcus Forsell Stahre soluion.

I override onDetach in DetailsFragment

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

But this didn't work.

1条回答
唯我独甜
2楼-- · 2019-07-23 15:59

Try modifying your DialogFragment to this

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

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

and minimize the number of DialogFragment instances created in the DetailsFragment

protected static TagCatFragment tagCatFragment;

private void parseJson(JSONObject jsonObject) {
    //your json stuff here
    if (tagCatFragment == null)
        tagCatFragment = TagCatFragment.newInstance("xxx", "yyyy",
                "Author");
    button.setOnClickListener(
            new OnClickListener() {

                @Override
                public void onClick(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.

查看更多
登录 后发表回答