Wes*_*ton 25 android android-fragments
我有一个Activity,用这个XML调用setContentView:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
>
<fragment android:name="org.vt.indiatab.GroupFragment"
android:id="@+id/home_groups"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />
<..some other fragments ...>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
GroupFragment扩展了Fragment,一切都很好.但是,我在GroupFragment中显示了一个DialogFragment.这显示正确,但是当屏幕旋转时,我得到一个强制关闭.
从DialogFragment.show(FragmentManager,String)以外的其他片段中显示DialogFragment的正确方法是什么?
Zso*_*agy 54
兼容性库中存在可能导致此问题的错误.试着把它放在你的dialogfragment中:
@Override
public void onDestroyView() {
if (getDialog() != null && getRetainInstance())
getDialog().setOnDismissListener(null);
super.onDestroyView();
}
Run Code Online (Sandbox Code Playgroud)
我还建议将您的dialogfragment设置为保留,因此在轮换后不会被解除.把"setRetainInstance(true);" 例如在onCreate()方法中.
Wes*_*ton 11
好吧,虽然Zsombor的方法有效,但这是由于我对片段缺乏经验而且他的解决方案导致了问题saveInstanceState Bundle
.
显然(至少对于DialogFragment),它应该是一个public static class
.你也必须编写自己的static DialogFragment newInstance()
方法.这是因为Fragment类newInstance
在其instantiate()
方法中调用该方法.
总而言之,你必须像这样编写DialogFragments:
public static class MyDialogFragment extends DialogFragment {
static MyDialogFragment newInstance() {
MyDialogFragment d = new MyDialogFragment();
return d;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
...
}
}
Run Code Online (Sandbox Code Playgroud)
并向他们展示:
private void showMyDialog() {
MyDialogFragment d = MyDialogFragment.newInstance();
d.show(getFragmentManager(), "dialog");
}
Run Code Online (Sandbox Code Playgroud)
这可能是ActionBarSherlock库所独有的,但SDK文档中的官方示例也使用此范例.
归档时间: |
|
查看次数: |
28827 次 |
最近记录: |