Window Leaked Exception

android.view.WindowLeakedException 

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;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.play_screen);
// 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(R.id.scanBtn_id);
dialog = new Dialog(PlayActivity.this, R.style.FullHeightDialog);

}
@Override

public void onClick(View v) {

switch (v.getId()) {

case R.id.scanBtn_id:

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

sound_stream1 = mSoundPool.play(mSoundPoolMap.get(1), streamVolume,

streamVolume, 1, 0, 1f);

dialog.show();
}

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, R.style.FullHeightDialog);

dialog1.setContentView(R.layout.gender_dialog_screen);

dialog1.findViewById(R.id.femaleBtn_id).setOnClickListener(

new OnClickListener() {

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

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

}

});

dialog1.findViewById(R.id.maleBtn_id).setOnClickListener(

new OnClickListener() {

public void onClick(View v) {

gender_val = "male";

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

}

});

dialog1.show();//showing the dialog box
}

@Override

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

super.onDestroy();

if (dialog.isShowing()) {

dialog.dismiss();

}

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().