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(); } }