<bdo id='obVn0'></bdo><ul id='obVn0'></ul>

  1. <legend id='obVn0'><style id='obVn0'><dir id='obVn0'><q id='obVn0'></q></dir></style></legend>
    <tfoot id='obVn0'></tfoot>
  2. <small id='obVn0'></small><noframes id='obVn0'>

    <i id='obVn0'><tr id='obVn0'><dt id='obVn0'><q id='obVn0'><span id='obVn0'><b id='obVn0'><form id='obVn0'><ins id='obVn0'></ins><ul id='obVn0'></ul><sub id='obVn0'></sub></form><legend id='obVn0'></legend><bdo id='obVn0'><pre id='obVn0'><center id='obVn0'></center></pre></bdo></b><th id='obVn0'></th></span></q></dt></tr></i><div id='obVn0'><tfoot id='obVn0'></tfoot><dl id='obVn0'><fieldset id='obVn0'></fieldset></dl></div>

    1. 'IllegalStateException: Activity 已被销毁'当 Activity 重启后

      时间:2023-10-04

        <small id='WPMDF'></small><noframes id='WPMDF'>

        <tfoot id='WPMDF'></tfoot>
          <bdo id='WPMDF'></bdo><ul id='WPMDF'></ul>

              <tbody id='WPMDF'></tbody>
              <legend id='WPMDF'><style id='WPMDF'><dir id='WPMDF'><q id='WPMDF'></q></dir></style></legend>
              <i id='WPMDF'><tr id='WPMDF'><dt id='WPMDF'><q id='WPMDF'><span id='WPMDF'><b id='WPMDF'><form id='WPMDF'><ins id='WPMDF'></ins><ul id='WPMDF'></ul><sub id='WPMDF'></sub></form><legend id='WPMDF'></legend><bdo id='WPMDF'><pre id='WPMDF'><center id='WPMDF'></center></pre></bdo></b><th id='WPMDF'></th></span></q></dt></tr></i><div id='WPMDF'><tfoot id='WPMDF'></tfoot><dl id='WPMDF'><fieldset id='WPMDF'></fieldset></dl></div>

              1. 本文介绍了'IllegalStateException: Activity 已被销毁'当 Activity 重启后调用'getSupportFragmentManager()'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个包含子 ViewPager 的 ViewPager 的父 Fragment Activity.子 ViewPager 包含每个页面的片段.我使用回调接口在这些子页面片段和顶级父片段活动之间进行通信,例如

                I have a parent Fragment Activity that has a ViewPager which contains a child ViewPager. The child ViewPager contains Fragments for each page. I communicate between these child page fragments and the top parent Fragment Activity using a callback interface e.g.

                public interface Callbacks {
                    public void onItemSelected(Link link);
                }
                

                在父片段活动中,我侦听 onItemSelected 事件,例如

                In the parent Fragment Activity I listen for onItemSelected events e.g.

                @Override
                public void onItemSelected(Link link) {
                    Bundle argumentsFront = new Bundle();
                    argumentsFront.putParcelable(FragmentComments.ARG_ITEM_ID, link);
                    fragmentComments = new FragmentComments();
                    fragmentComments.setArguments(argumentsFront);
                    getSupportFragmentManager().beginTransaction().replace(R.id.post_container, fragmentComments).commitAllowingStateLoss();
                }
                

                现在,当应用首次启动时,这可以正常工作.

                Now this works fine when the app is first launched.

                如果您转动设备以更改方向,则 Activity 会重新启动.当我使用 setRetainInstance(true); 时,所有片段都会自行重新初始化(我不在子 ViewPager 的页面片段中调用 setRetainInstance(true),因为它不受支持).但是,如果我单击子 ViewPager 的片段中的列表项,则会出现此异常:

                If you turn the device to change the orientation the Activity restarts. All fragments reinitialise themselves as I use setRetainInstance(true); (I do not call setRetainInstance(true) in the page Fragments of the child ViewPager as it is not supported). However if I click a list item in the Fragment of the child ViewPager I get this exception:

                FATAL EXCEPTION: main
                java.lang.IllegalStateException: Activity has been destroyed
                at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1342)
                at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
                at android.support.v4.app.BackStackRecord.commitAllowingStateLoss(BackStackRecord.java:578)
                

                有人知道为什么会这样吗?

                Does anyone know why this happens?

                谢谢

                推荐答案

                当您旋转设备时,Android 会保存、销毁并重新创建您的 Activity 及其 ViewPager片段.由于 ViewPager 使用您的 ActivityFragmentManager,它会为您保存和重用这些 Fragment(并且不会创建新的),因此它们将保存对您(现已销毁)原始 Activity 的旧引用,并且您会得到该 IllegalStateException.

                When you rotate the device, Android saves, destroys, and recreates your Activity and its ViewPager of Fragments. Since the ViewPager uses the FragmentManager of your Activity, it saves and reuses those Fragments for you (and does not create new ones), so they will hold the old references to your (now destroyed) original Activity, and you get that IllegalStateException.

                在您的孩子 Fragments 中,尝试如下操作:

                In your child Fragments, try something like this:

                @Override
                public void onAttach(Activity activity) {
                    super.onAttach(activity);
                    Log.v(TAG, "onAttach");
                
                    // Check if parent activity implements our callback interface
                    if (activity != null) {
                        try {
                            mParentCallback = (Callbacks) activity;
                        }
                        catch (ClassCastException e) {
                        }
                    }
                }
                

                然后发生选择时:

                if(mParentCallback != null) {
                    mParentCallback.onItemSelected(selectedLink);
                }
                

                由于 onAttach 作为 Fragment 生命周期的一部分被调用,您的 Fragments 将在轮换时更新其回调引用.

                Since onAttach gets called as part of the Fragment lifecycle, your Fragments will update their callback reference on rotation.

                这篇关于'IllegalStateException: Activity 已被销毁'当 Activity 重启后调用'getSupportFragmentManager()'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:通过按钮单击更改 viewpager 片段 下一篇:PageTransformer 转换中的 ViewPager 边距

                相关文章

                <small id='MxSDL'></small><noframes id='MxSDL'>

              2. <legend id='MxSDL'><style id='MxSDL'><dir id='MxSDL'><q id='MxSDL'></q></dir></style></legend>

                <tfoot id='MxSDL'></tfoot>

                    • <bdo id='MxSDL'></bdo><ul id='MxSDL'></ul>
                    <i id='MxSDL'><tr id='MxSDL'><dt id='MxSDL'><q id='MxSDL'><span id='MxSDL'><b id='MxSDL'><form id='MxSDL'><ins id='MxSDL'></ins><ul id='MxSDL'></ul><sub id='MxSDL'></sub></form><legend id='MxSDL'></legend><bdo id='MxSDL'><pre id='MxSDL'><center id='MxSDL'></center></pre></bdo></b><th id='MxSDL'></th></span></q></dt></tr></i><div id='MxSDL'><tfoot id='MxSDL'></tfoot><dl id='MxSDL'><fieldset id='MxSDL'></fieldset></dl></div>