Java tutorial
//package com.java2s; //License from project: Apache License import android.app.Activity; import android.app.Dialog; import android.content.Context; import android.content.ContextWrapper; public class Main { public static void dismissDialog(Dialog dialog) { if (dialog.isShowing()) { //check if dialog is showing. //get the Context object that was used to great the dialog Context context = ((ContextWrapper) dialog.getContext()).getBaseContext(); //if the Context used here was an activity AND it hasn't been finished //then dismiss it if (context instanceof Activity) { if (!((Activity) context).isFinishing()) { dialog.dismiss(); } } else { //if the Context used wasnt an Activity, then dismiss it too dialog.dismiss(); } } } }