DialogFragment从onContextItemSelected显示不下去的onPause / onResume [英] DialogFragment displayed from onContextItemSelected doesn't survive onPause/onResume

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

问题描述

我有一个DialogDragment,我可以证明以下两种方法之一:

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

1)通过利用来自其OnItemClickListener

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的表现成为一个处理程序至少为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;
}

更换300ms的第二延迟了0毫秒或250ms的延迟会导致它被再次打破。这可重复的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),和摩托罗拉RAZR I(4.0)。我还可以通过修改我自己的书样品之一重现该问题。我在上面提出的问题,用你的样品: HTTP:/ /$c$c.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毫秒的作品,那么我们那些可爱的时间问题之一,我没有你如何解决它,总之不使用菜单来显示它的foggiest想法。例如,您的示例应用程序,只需切换到 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.

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

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