简体   繁体   中英

Is there a way to determine whether it is possible to call a dialog.dismiss() without empty try-catch block?

I am getting the well known java.lang.IllegalArgumentException: View not attached to window manager . The currently known solution is to ignore the error using empty try-catch block. But is it there a more programmer friendly solution? Eg

if (dialog.isAttached())
  dialog.dismiss();

Of course, better would be if the Android SDK would have a not failing function (because why the API should fail if it is impossible to avoid it??):

dialog.tryDismiss();

Or is the empty try-catch block architectonically justifiable? Or is it just a workaround for a bad or incomplete API?

I always use:

if(dialog != null && dialog.isShowing())
  dialog.dismiss();

you can use:

if (dialog != null && dialog.isShowing())dialog.dismiss();

better you dismiss dialog before activity disappears/closes...that means in onPause() and onDestroy() event too..

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