简体   繁体   English

如何在Android上与2对话框片段生命周期进行交互?

[英]How to interact with 2 dialog fragment lifecycle on Android?

I'm very new to Android development. 我对Android开发非常陌生。 I have gone through many android tutorials and articles but I'm still a little bit confusing about my situation right now. 我已经看过许多android教程和文章,但是现在我的情况仍然有些混乱。

What I need to do: I have two dialog fragment, f1 and f2. 我需要做的是:我有两个对话框片段,f1和f2。 There is an audio playing in background. 在后台播放音频。 when any dialog pops up, stop playing audio, when dialog dismiss, audio resumes. 当弹出任何对话框时,停止播放音频,当关闭对话框时,恢复音频。

What I have done: I have implemented a listener interface with 2 methods: onCreateDialog and onDismissDialog . 我所做的:我已经用2种方法实现了一个侦听器接口: onCreateDialogonDismissDialog It worked for just one dialog (f1 or f2 pops up, and audio stops. Dialog dismisses, audio resumes) 它仅适用于一个对话框(f1或f2弹出,音频停止。对话框关闭,音频恢复)

What's not right: A situation that: f1 pops up, press "yes", f1 will dismiss, f2 will pop up. 不正确的情况:出现以下情况:f1弹出,按“是”,f1将关闭,f2将弹出。 The audio will stop (actually it's already stop because f1 poped up), then the audio will resume. 音频将停止(实际上已经停止,因为弹出了f1),然后音频将恢复。 So I checked log, it seems that f2 onCreateDialog was called before f1 onDismissDialog , that's why audio resumed when f2 poped up. 所以我检查了日志,看来f2 onCreateDialog在f1 onDismissDialog之前被调用了,这就是为什么当f2弹出时恢复音频的原因。

Does anybody have any idea what I could do about this situation? 有人知道我可以在这种情况下做什么吗? ANY help is appreciated!! 任何帮助表示赞赏!

Thanks a lot!!! 非常感谢!!!

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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM