旋转时未破坏片段 [英] Fragment not destroyed on rotation

查看:86
本文介绍了旋转时未破坏片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个以编程方式添加的片段.但是,我发现,旋转后会创建一个新片段,但旧片段仍然有效(尽管未显示),我认为片段和旋转活动都被破坏了,但是这里不是这种情况.是什么导致我的片段被保留/未被破坏?请注意:

I have a fragment which is added programmatically. I have however discovered that after a rotation, a new fragment is created but the old fragment is still alive (though not being displayed) I assumed that fragments are destroyed along with the activity on rotation, but this is not the case here. What could be causing my fragment to be retained / not destroyed? Please note:

  1. 我没有设置setRetainInstance.
  2. 该片段未添加到活动onCreate中(稍后在点击按钮时添加)
  3. 我看到旋转时会为该活动调用onDestroy
  4. 尽管片段已添加,但它会模糊添加按钮,并且只有在我执行删除"时才会被清除.

创建片段的代码:

MyFragment fragment = MyFragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.content_frame, fragment, "CurrentFragment");
fragmentTransaction.show(fragment).commit();

删除片段的代码:

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.remove(fragment).commit();

更新:我无法确认这是否是问题,但是该片段在onCreate中注册为LocalBroadcast接收器.它未在onDestroy中进行广播注册,但似乎LocalBroadcast注册阻止了onDestroy被调用,或者轮播时未调用onDestroy.我现在在onStop上注销,这似乎已经解决了问题.

UPDATE: I cannot confirm if this was the problem, but the fragment registers as a LocalBroadcast receiver in onCreate. It unregistered for broadcasts in onDestroy, but it seems that either the LocalBroadcast registration was preventing onDestroy being called, or onDestroy was not called upon rotation. I now unregister in onStop and this seems to have solved the issue.

推荐答案

由于方向改变而重新启动Activity时,Android Framework会自动为您重新创建并添加Fragment.您可能在 Activity onCreate 中添加了Fragment.在那儿,您可以在方向更改后添加一个额外的片段.

When the Activity is restarted because of a orientation change, the Android Framework recreates and adds the Fragment automatically for you. You probably add the Fragment in onCreate of your Activity. That's where you add an additional Fragment after the orientation change.

您必须检查是否已添加片段:

You have to check if the Fragment is already added:

Fragment f = getFragmentManager().findFragmentById(R.id.content_frame);
if(f == null){
    //Add it, there is no Fragment
}else{
    //It's already there
}

每次您点击添加片段的按钮时,都会添加一个新的片段.如果您的容器是FrameLayout,则可能看不到它.尝试替换碎片:

Every time you tapp the button that adds the Fragment, a new Fragment will be added. If your container is a FrameLayout you probably won't see it. Try to replace the Fragments:

fragmentTransaction.replace(R.id.content_frame, fragment, "CurrentFragment");

方法 setRetainInstance(true)具有另一个目的.如果启用它,方向改变时, Fragment 实例将不会被销毁(但其视图将被销毁).这意味着您可以将数据存储在 Fragment 的成员变量中,并且在更改方向后这些数据仍然可用.

The method setRetainInstance(true) has another purpose. If you enable it, the Fragment instance will not be destroyed when the orientation changes (but its Views will be destroyed). That means you can store data in the member variables of a Fragment and this data will still be available after an orientation change.

设置 setRetainInstance(true)时,Fragment的生命周期略有不同.IE. onCreate() onDestroy()不会被调用(因为这些事件不会发生)

When setting setRetainInstance(true) the life-cycle of the Fragment is slightly different. I.e. onCreate() and onDestroy() are not called (Because those event do not happen)

这篇关于旋转时未破坏片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆