简体   繁体   中英

AlertDialog onDismiss not called by screen rotation

I have an AlerDialog and want to do some clean ups (releasing MediaPlayer eg). So I add an dismiss listener.

m_dialog.setOnCancelListener(new OnCancelListener()
{
    public void onCancel(DialogInterface arg0)
    {
        stop();
    }
});
m_dialog.setOnDismissListener(new OnDismissListener()
{
    public void onDismiss(DialogInterface dialog)
    {
        stop();
    }
});

This works fine when the back button is pressed. But when the screen rotates the dialog hides but is not reconstructed AND onDimiss was not called before.

Any ideas, is this a bug in Android 2.3 ?

Here is quick fix:

protected void onDestroy() {
    if(m_dialog.isShowing())
        m_dialog.dismiss();
    super.onDestroy();
}

Understand that this is what happens for you if you opened the Dialog with Activity#showDialog() . (I am guessing that you use m_dialog.show() instead, so really my advice is to use showDialog() or upgrade to Fragments.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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