Android中消息隊列postDelay+Dialog引發的窗體泄露的主要緣由是,當牀窗體壓入棧或者finish掉後,Activity因爲須要處理postDelay而不能及時銷燬,所以形成了一個問題,當dialog觸發show方法時,因爲window和activity已經分開了,所以便會形成窗體泄露。android
錯誤描述:android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@536682c4 is not valid; is your activity running?post
private boolean isActive = true; public void onResume() { super.onResume(); isActive = true; } public void onPause() { super.onPause(); isActive = false; } public boolean activityIsActivity() { return isActive; } public void showAlertDialog() { if(activityIsActive()) //只有窗體處於活動狀態時執行此程序 { AlertDialog.Builder builder = new AlertDialog.Builder(activity); builder.setTitle("hello world") .setCancelable(false) .setMessage("Hello"); builder.create().show(); } }