从 onContextItemSelected 显示的 DialogFragment 在 onPause/onResume 中不存在 [英] DialogFragment displayed from onContextItemSelected doesn't survive onPause/onResume

查看:21
本文介绍了从 onContextItemSelected 显示的 DialogFragment 在 onPause/onResume 中不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 DialogDragment,我可以通过以下两种方式之一显示:

I have a DialogDragment which I can show one of two ways:

1) 通过从其 OnItemClickListener 中点击 ListView 项

1) By tapping on a ListView item from its OnItemClickListener

2) 通过激活 ListView 的上下文菜单并选择菜单项

2) By activating a the ListView's context menu and selecting a menu item

在所有生命周期事件下执行 #1 都可以正常工作,但如果我通过 #2 调用它并暂停活动(通过返回主页)并通过任务切换器恢复活动,则不再显示该对话框.片段在那里,我可以旋转设备并显示对话框.

Doing #1 works fine under all lifecycle events, but if I invoke it via #2 and I pause the activity (by going Home) and the resuming it via the task switcher, the dialog is no longer displayed. The fragment is there, and I can rotate the device and show the dialog.

我进行了实验,如果我将 DialogFragment 的显示放入一个 Handler 中,延迟至少 1/2 秒,它会起作用.

I experimented, and if I put the showing of the DialogFragment into a Handler with a delay of at least 1/2 seconds, it works.

以下代码段失败——它显示对话框,但暂停/恢复隐藏它:

The following snippet fails -- it shows the dialog, but then pause/resume hides it:

public boolean onContextItemSelected(android.view.MenuItem item) {
    boolean consumed = false;

    switch (item.getItemId()) {
    case R.id.menu_item:
        showMyDialogFragment();
        consumed = true;
        break;
    }

    return consumed;
}

所以下面的代码片段有效.暂停/恢复再次正确显示对话框:

So the following snippet works. Pause/resume display the dialog again correctly:

public boolean onContextItemSelected(android.view.MenuItem item) {
    boolean consumed = false;

    switch (item.getItemId()) {
    case R.id.menu_item:
        new Handler().postDelayed(new Runnable() {
            public void run() {
                showMyDialogFragment();
            }
        }, 300);

        consumed = true;
        break;
    }

    return consumed;
}

用 0ms 或 250ms 延迟替换 300ms 秒延迟会导致它再次被破坏.这种可重复 100% 的时间.

Replacing the 300ms second delay with a 0ms or 250ms delay causes it to be broken again. This repeatable 100% of the time.

这显然是一个可怕的黑客行为,可能取决于设备速度的常数使情况变得更糟.

This is a terrible hack obviously, made worse by the constant that's probably depends on the speed of the device.

有人知道为什么会这样和/或提供更好的解决方案吗?我在这个问题上花了几个小时,这是我能想到的最好的方法.

Anybody know why this is going on and/or offer a better solution? I spent hours on this issue and this is the best I could come up with.

推荐答案

我可以在 Android 4.2(ARM 模拟器和 Galaxy Nexus)上重现这个.我无法在 x86 4.1 模拟器、Nexus S (4.1) 和 Motorola RAZR i (4.0) 上重现您的发现.我还可以通过修改我自己的书籍样本之一来重现该问题.我使用您的示例提交了一个问题:http://code.google.com/p/android/issues/detail?id=41901请添加您认为有助于他们诊断问题的任何其他信息.

I can reproduce this on Android 4.2 (ARM emulator and Galaxy Nexus). I am unable to reproduce your findings on an x86 4.1 emulator, a Nexus S (4.1), and a Motorola RAZR i (4.0). I can also reproduce the problem by modifying one of my own book samples. I filed an issue on it, using your sample: http://code.google.com/p/android/issues/detail?id=41901 Please add any other information you think would help them diagnose the problem.

关于解决方法,如果 300 毫秒有效,那么我们就有了一个可爱的时间问题",我不知道你将如何解决它,除了不使用菜单来显示它.例如,对于您的示例应用程序,只需切换到 SHOW_AS_ACTION_ALWAYS(因此使其成为操作栏上的项目而不是溢出菜单中的项目)就足以让 DialogFragment 举止得体.希望您有办法调整您的 UI 来弥补这个错误,或者也许有人会想出另一种解决方法并将其张贴在这里或发布在问题上.

With respect to a workaround, if 300ms works, then we have one of those lovely "timing issues", and I haven't the foggiest idea how you'd work around it, short of not using a menu to display it. For example, with your sample app, simply switching to SHOW_AS_ACTION_ALWAYS (and therefore having it be an item on the action bar rather than in an overflow menu) is sufficient to have the DialogFragment behave properly. Hopefully, you'll have a way of adjusting your UI to compensate for this bug, or perhaps somebody will cook up another workaround and post it here or on the issue.

这篇关于从 onContextItemSelected 显示的 DialogFragment 在 onPause/onResume 中不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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