android fragment 方向改变

标签 android android-fragments android-orientation android-dialogfragment

编辑:

我有一个 DialogFragment,在我更改设备方向后,父级 Activity 中的引用为 NULL。我该如何解决这个问题?

fragment 创建:

MyFragment myFragment = MyFragment.newInstance(title);
myFragment.show(getFragmentManager(), tag);

方向改变后:

MyFragment myFragment = (MyFragment)getFragmentManager().findFragmentByTag(tag);
myFragment.dismiss();  //NULL pointer exception here

最佳答案

myFragment.setRetainInstance(true); 用于救援。


编辑:

同时重写 DialogFragment 中的 onDestroyView:

@Override
public void onDestroyView() {
  if (getDialog() != null && getRetainInstance())
    getDialog().setOnDismissListener(null);
  super.onDestroyView();
}

这是以下问题的结果:http://code.google.com/p/android/issues/detail?id=17423

关于android fragment 方向改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12490986/

相关文章:

android - 非法状态异常 : Fragment already added in the tabhost fragment

Android 方向更改 : Different Layout, 相同的 fragment

android - RecyclerView 在旋转时为空

android - 如何将库项目添加到 Android Studio?

android - Android 版 OpenCV 中的不兼容垫

android - ViewPager 中可见的两个 fragment

java - 使用 Fragment 强制关闭图库

android - 为什么我们需要将颜色传递给每个顶点?

java - 无法在 Kotlin 中调用 FileProvider 的 getUriForFile

android - 即使在操作系统中禁用了屏幕旋转,我的 Android 应用程序也会旋转。我怎样才能解决这个问题?