DialogFragment设备旋转后消失,尽管采用共同修复 [英] DialogFragment disappears after device rotation despite applying common fixes

查看:493
本文介绍了DialogFragment设备旋转后消失,尽管采用共同修复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有当用户点击了动作条的菜单项,我发动DialogFragment。有关该对话框的功能正常的一切 - 它启动就好了,它做的一切我已经设置它做的事。不幸的是,当我转动我的设备中,DialogFragment消失。

I have a DialogFragment that I launch when a user taps a menu item on the ActionBar. Everything about the Dialog functions properly - it launches just fine and it does everything I've set it up to do. Unfortunately, as soon as I rotate my device, the DialogFragment disappears.

这似乎是一个普遍的问题早在2012 - 我已经冲刷计算器,并试图所有已张贴在过去几年的共同修复。 <一href=\"http://stackoverflow.com/questions/13934951/dialogfragment-with-setretatininstancestatetrue-is-not-display-after-device-ro\">This SO后特别总结了所有已经提出的潜在修正:

This seemed to be a common problem back in 2012 - I've scoured StackOverflow and tried all of the common fixes that have been posted in the last couple of years. This SO post in particular summarizes all of the potential fixes that have been proposed:


  • 设置为使用中的newInstance()范例
  • 的DialogFragment
  • 添加 setRetainInstance(真)来的DialogFragment的的onCreate()

  • 添加一个解决方法 onDestroyView()来解决支持库一个潜在的错误

  • Set up the DialogFragment to use the newInstance() paradigm
  • Add setRetainInstance(true) to the DialogFragment's onCreate()
  • Add a workaround to onDestroyView() to address a potential bug in the support library

尽管有上述实施应有尽有,DialogFragment拒绝设备旋转后留下来。

Despite implementing everything above, the DialogFragment refuses to stick around after device rotation.

下面就是我如何启动DialogFragment从活动:

Here's how I launch the DialogFragment from the Activity:

DialogKanjiLookup dialog = DialogKanjiLookup.newInstance(gSearchView.getQuery());
dialog.show(getSupportFragmentManager(), "dialogKanjiLookup");

下面是DialogFragment的的newInstance()

Here is the DialogFragment's newInstance():

public DialogKanjiLookup() {}

public static DialogKanjiLookup newInstance(CharSequence searchTerm)
{
    DialogKanjiLookup dialog = new DialogKanjiLookup();
    Bundle args = new Bundle();
    args.putCharSequence(BUNDLE_SEARCH, searchTerm);
    dialog.setArguments(args);
    return dialog;
}

下面的对话框的`onCreateDialog():

Here's the dialog's `onCreateDialog():

@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
    // Specify a layout for the dialog
    LayoutInflater inflater = getActivity().getLayoutInflater();
    View layout = inflater.inflate(R.layout.dialog_kanjilookup, null);

    // SNIP
    // ...Handle savedInstanceState, set up various Listeners and adapters...
    // SNIP

    // Create the actual dialog
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    // Customize the dialog
    builder.setTitle(R.string.dialog_kanji_lookup_title);
    builder.setIcon(R.drawable.kanji_lookup);
    builder.setPositiveButton(R.string.menu_search, btnSearchListener);
    builder.setNegativeButton(R.string.cancel, null);
    builder.setView(layout);

    // Force the dialog to take up as much space as it can
    Dialog dialog = builder.create();
    dialog.show();
    dialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);

    // Display the dialog
    return dialog;
}

DialogFragment.dismiss()从片段中调用的只有一个实例,但是当用户点击对话框的按钮,一个唯一的触发,所以我已经排除说出来。为什么我的DialogFragment还是旋转后消失?我所有,但拉我的头发在这个,它工作得很好,直到一段时间后我的实施了抽屉式导航栏。莫非是问题的一部分?

There's only one instance of DialogFragment.dismiss() called from within the fragment but that's only triggered when the user taps one of the dialog's buttons, so I've ruled that out. Why does my DialogFragment still disappear after rotation? I'm all but pulling my hair out over this, it worked fine until sometime after I implemented a Navigation Drawer. Could that be a part of the problem?

编辑:虚惊一场,我发现我的答案是没有解决方案!之后,我完成了从使用支持库移动所有的片段和活动之外的问题又出现了。

False alarm, I discovered that my answer wasn't the solution! The problem reappeared after I finished moving all of my Fragments and Activities away from using the Support Libraries.

我的没有的发现,这个问题只存在于活动中的内容片段具有的的是在活动的布局宣告静态。也就是说,如果我有一个&LT;的FrameLayout方式&gt; 定义在XML中,并使用 fragmentManager.beginTransaction()取代(R.id.content_frame,FRAG ,标签).commit(); 来加载一个片段,在该活动发起任何DialogFragments失败重新加载时,该设备已被旋转

I did discover that this problem only exists in Activities in which the content fragment has not been declared statically in the Activity's layout. That is, if I have a <FrameLayout> defined in XML and use fragmentManager.beginTransaction().replace(R.id.content_frame, frag, tag).commit(); to load a fragment, any DialogFragments launched in that activity fail to reload when the device has been rotated.

下面是一个演示这一问题的屏幕录制:
https://www.youtube.com/watch?v=psK0pzMn6oc

Here's a screen recording that demonstrates the issue: https://www.youtube.com/watch?v=psK0pzMn6oc

推荐答案

一些试验后,我发现了一个解决方案。一个启动的对话框中的活动需要延长 android.support.v4.app.FragmentActivity DialogFragment 需求延长 android.support.v4.app.DialogFragment

After some experimentation I discovered a solution. The Activity that launches the dialog needs to extend android.support.v4.app.FragmentActivity, and the DialogFragment needs to extend android.support.v4.app.DialogFragment.

然后, getSupportFragmentManager()时启动必须调用在 DialogFragment

CustomDialog dialog = CustomDialog.newInstance();
dialog.show(getSupportFragmentManager(), "customDialog");

本应在旋转过程中保持对话。有需要使用 setRetainInstance(真)在弹出的对话框本身。

This should retain the dialog during rotation. There was no need to use setRetainInstance(true) in the dialog itself.

你要知道,在这种情况下,只能在其中 FragmentActivity 调用 DialogFragment 。我还在想办法苏斯到preserve一个是被通过,而不是片段称为对话框。

Mind you, this only works in instances in which an FragmentActivity calls a DialogFragment. I'm still trying to suss out a way to preserve a dialog that gets called via a Fragment instead.

这篇关于DialogFragment设备旋转后消失,尽管采用共同修复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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