I'm popping up an AlertDialog so users can type in a new item to add to a list. However, whenever the screen orientation changes, the LogCat spits out a bunch of errors regarding a window leak. I can't quite figure out what I'm doing wrong here.
private void launchPopup() {
if (mTypedText == null) {
mTypedText = "";
}
LayoutInflater inflater = (LayoutInflater) ActivityTags.this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.layout_additem, (ViewGroup) findViewById(R.id.additem_root));
final TextView text = (TextView)layout.findViewById(R.id.additem_text);
text.setText("Type in the name of the new item.");
text.setLines(1);
final EditText name = (EditText)layout.findViewById(R.id.additem_name);
name.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void afterTextChanged(Editable s) {
if (s != null) {
mTypedText = s.toString();
}
else {
mTypedText = null;
}
}
});
if (mTypedText != null) {
name.setText(mTypedText);
}
mAddDialog = new AlertDialog.Builder(ActivityItems.this);
mAddDialog.setTitle("Add Item");
mAddDialog.setView(layout);
mAddDialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i) {
// TODO
}
});
mAddDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i) {
mTypedText = null;
}
});
mAddDialog.show();
}
In onStop(), mAddDialog is set to null if it is not already null.
Here is the error:
Activity com.myapp.app.activity.ActivityItems has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@45164f30 that was originally added here
android.view.WindowLeaked: Activity com.myapp.app.activity.ActivityItems has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@45164f30 that was originally added here
at android.view.ViewRoot.<init>(ViewRoot.java:247)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
at android.view.Window$LocalWindowManager.addView(Window.java:424)
at android.app.Dialog.show(Dialog.java:241)
at android.app.AlertDialog$Builder.show(AlertDialog.java:802)
...