编辑:
我有一个 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/