开发者

How to detect that my AlertDialog is killed by system?

开发者 https://www.devze.com 2023-04-03 07:36 出处:网络
I have my dialog based on AlertDialog. When screen rotates (or some other config change happens), the dialog is closed by OS.

I have my dialog based on AlertDialog. When screen rotates (or some other config change happens), the dialog is closed by OS.

Problem is that my dialog plays audio by MediaPlayer, and when such auto-close happens, dialog is gone but sound continues to play.

Does Dialog get some chance to cleanup before开发者_Python百科 it's killed by system? Or I'm forced to use Activity in this case?


When screen rotates your Activity is destroyed and created new one.

If you have opened dialog and this dialog is not managed, it will leak. You can suppress this behaviour in manifest by defining android:configChanges="orientation" for <activity>, then instead of recreating Activity an onConfigurationChanges callback is called instead. There you can handle screen rotation.

If you are using Activity.showDialog(int) then before activity is destroyed dialog states are saved. When new Activity is created saved information is used to restore your dialogs.


Update

If you are looking for handling when your dialog is closed, you can use AlertDialog.setOnDismissListener


Add proper permission in manifest file.

For the activity like in this post


there is onPrepareDialog, i guess there should be something like onDestroy also. Anyways, what if you handle onConfigurationChanged of the activity and recreate the dialog ?

public void onConfigurationChanged(Configuration newConfig) 
{
super.onConfigurationChanged(newConfig);
//create dialog here again? 
}

update

thanks salw, you will need android:configChanges="orientation" in manifest in activity tag

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号