如何修复IllegalStateException:在轮换后显示Dialogfragment时,Activity已被销毁

时间:2016-07-06 16:51:34

标签: android illegalstateexception

我有一个DialogFragment,除了发出一个Volley请求并将数据填充到RecyclerView之外什么都没有。我从片段启动(通过单击TextView链接)此对话框。当对话框第一次启动时,它会启动并执行它应该执行的操作,没有任何障碍。

然后我旋转屏幕,再次启动对话框,然后嗖!我受到了欢迎“IllegalStateException:活动已被破坏”。

关于这个错误的一个奇怪之处是我也在 DetailsFragment 内的适配器类的onBindViewHolder中启动了同样的DialogFragment,即使我将屏幕旋转十次,同样的对话框也会无故障地打开。

这是堆栈跟踪,

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)

这是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;
    }
}

这是我发布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();
                    }
                }
            });

        }
    }
}

尝试过的解决方案:

我尝试过热门的Marcus Forsell Stahre soluion

我在 DetailsFragment

中覆盖onDetach
@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);
        }
    }

但这不起作用。

1 个答案:

答案 0 :(得分:1)

尝试将DialogFragment修改为此

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

并最小化DetailsFragment中创建的DialogFragment实例的数量

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

不确定保存mFragment实例的位置。 但是,您可能希望在onDestroy方法的DialogFragment中保留数据。让我知道事情的后续。