Window Leaked Exception


This exception occurs when we try to create a View when the Activity that holds the view has already exited. I used dialog boxes in my activity, and sometimes when I abruptly exit the activity by pressing onKey down , this exception was thrown. Actually, I clicked on a button that shows a dialog box but before the dialog could appear/dismiss, I exited the activity. Hence, the solution is this is to dismiss all the dialog boxes /views if they are active in the onDestroy() method . It a very good practice to use onDestroy() to release all the resources used in the activity and to call the garbage collector for clean up.

public class PlayActivity extends Activity implements OnClickListener {
ImageButton scanBtn;

Dialog dialog;

SoundPool mSoundPool;

HashMap<Integer, Integer> mSoundPoolMap;

AudioManager mAudioManager;

Context mContext;

int sound_stream1 = 0, sound_stream2 = 0;

float streamVolume;

Boolean activity_destroyed=false;


public void onCreate(Bundle savedInstanceState) {


// code for sound pool

mContext = getApplicationContext();// getting current context

mSoundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);

mSoundPoolMap = new HashMap<Integer, Integer>();

// adding sounds to soundPool

// 1 is the index of sound

mSoundPoolMap.put(1, mSoundPool.load(this, R.raw.bottel, 1));// 1 is the

mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

// current volume index

streamVolume = AudioManager.getStreamVolume(AudioManager.STREAM_MUSIC

scanBtn = (ImageButton) findViewById(;
dialog = new Dialog(PlayActivity.this,;


public void onClick(View v) {

switch (v.getId()) {


//play sound with index 1 from the sound pool,0 means no looping

sound_stream1 =, streamVolume,

streamVolume, 1, 0, 1f);;

private void DialogGender() {

// creating a dialog box that appears on PlayMenuactivity and the dialog

// box will cover the entire screen

dialog = new Dialog(PlayActivity.this,;



new OnClickListener() {

public void onClick(View v) {     gender_val = "female";

dialog1.dismiss();// closes the dialog box




new OnClickListener() {

public void onClick(View v) {

gender_val = "male";

dialog.dismiss();// closes the dialog box


});;//showing the dialog box


protected void onDestroy() {

// closes any open dialogs before exiting to prevent window leaked

// exception

// caused when user try to dismiss a dialog after the activity has

// exited


if (dialog.isShowing()) {



mSoundPool.release();//releasing resources


Hence, it is very important to understand the life cycle of an Activity. The entire lifetime of an activity happens between the call to onCreate() and the call to onDestroy().The visible lifetime of an activity happens between the call to onStart() and the call to onStop().The foreground lifetime of an activity happens between the call to onResume() and the call to onPause().You should know how to manage resources in the Activity lifetime. For example, If you have a media sound playing then you should stop it in onPause() aswell as onDestroy().If you have a animation running then it should stop in onPause() and resume in onresume().You should also release sound resources , nullify bitmaps created , unbind views etc in the onDestroy().