【问题标题】:How to interact with 2 dialog fragment lifecycle on Android?如何与 Android 上的 2 个对话框片段生命周期进行交互?
【发布时间】:2016-03-28 18:34:01
【问题描述】:

我对 Android 开发非常陌生。我已经阅读了许多 android 教程和文章,但我现在对自己的情况仍然有些困惑。

我需要做的: 我有两个对话框片段,f1 和 f2。背景中有音频播放。当任何对话框弹出时,停止播放音频,当对话框关闭时,音频恢复。

我做了什么: 我已经用 2 种方法实现了一个监听器接口:onCreateDialogonDismissDialog。它仅适用于一个对话框(f1 或 f2 弹出,音频停止。对话框关闭,音频恢复)

哪里不对: 一种情况:f1弹出,按“是”,f1会关闭,f2会弹出。音频将停止(实际上它已经停止,因为 f1 弹出),然后音频将恢复。所以我查看了日志,似乎 f2 onCreateDialog 在 f1 onDismissDialog 之前被调用,这就是为什么 f2 弹出时音频恢复的原因。

有人知道我能对这种情况做些什么吗?任何帮助表示赞赏!

非常感谢!!!

【问题讨论】:

  • 那么您是否希望音频再次停止播放?我对这个问题感到困惑。
  • 只要有对话框弹出,我希望音频停止。 F1 关闭后会弹出 F2。 @阿什顿

标签: java android android-fragments android-dialogfragment android-debug


【解决方案1】:
ArrayList <DialogFragment> dialogs = new ArrayList();

void resumeSound () {
    for (DialogFragment dialog: dialogs){
        //maybe isVisible won't work, try with isAdded() or add a custom     
        //flag like [boolean isVisible] inside the Dialog
        if (dialog.isVisible() {
           return;
        }
    }
    ....
    //Code to resume sound;
    ....
}
//Put following on each dialog fragment
onCreateDialog () {
    //Make sure dialog is added with a TAG or id, so you can find it later  
    dialogs.add(this);
}

onDismissDialog () {
    //you'll have to put following line inside an array iterator, 
    //check if TAG or id equals, and then remove 
    //(maybe also implement equals() for DialogFragment 
    dialogs.remove();
    resumeSound();
}

【讨论】:

  • 这很好。但我不确定我以后是否会有超过 2 个对话,只是不确定对话的数量。
猜你喜欢
  • 2016-07-03
  • 2014-07-28
  • 1970-01-01
  • 2013-08-31
  • 1970-01-01
  • 1970-01-01
  • 2013-04-06
  • 2011-12-06
  • 2020-07-14
相关资源
最近更新 更多