"Activity has leaked window that was originally added here"

19,507

You call Assignment_Create.this.finish(); directly after calling dialog.show(). This means that the dialog is on screen when your activity gets destroyed, which causes the leaked window exception. You need to close all dialogs by calling dialog.dismiss() before calling this.finish().

You have two options:

  1. Move this.finish() to inside your onClickListener for the alert dialog. This is what I think you want, so that the activity will finish only if the user clicks ok in the dialog.
  2. Call alertdialog.finish() directly before calling this.finish().
Share:
19,507
InnocentKiller
Author by

InnocentKiller

@Android Developer

Updated on June 27, 2022

Comments

  • InnocentKiller
    InnocentKiller about 2 years

    So below is my code,

    public class Assignment_Create extends Activity implements OnClickListener {
    
    DataManipulator dataManipulator;
    static final int DIALOG_ID = 1;
    AlertDialog.Builder dialogAc;
    
    ImageView imageViewDateAssign, imageViewDueDate, imageViewSubmit;
    TextView textViewDtAssign, textViewDueDt;
    EditText editTextTitle, editTextDesc;
    
    static final int DATE_DIALOG_ID = 0;
    int cDay, cMonth, cYear;
    
    private TextView activeDateDisplay;
    private Calendar activeDate;
    
    // Update database
    String updateId;
    public boolean isEdit;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.assignment_create);
    
        imageViewDateAssign = (ImageView) findViewById(R.id.dateassign);
        imageViewDueDate = (ImageView) findViewById(R.id.duedate);
        imageViewSubmit = (ImageView) findViewById(R.id.submit);
    
        textViewDtAssign = (TextView) findViewById(R.id.textViewDateAssign);
        textViewDueDt = (TextView) findViewById(R.id.textViewDueDate);
    
        editTextTitle = (EditText) findViewById(R.id.title);
        editTextDesc = (EditText) findViewById(R.id.description);
    
        isEdit = getIntent().getExtras().getBoolean("isEdit");
        updateId = getIntent().getExtras().getString("idNo");
    
        if (isEdit) {
            editTextTitle.setText(getIntent().getExtras().getString(
                    "AsmntTitle"));
            editTextDesc
                    .setText(getIntent().getExtras().getString("AsmntDesc"));
        }
    
        Code.AssignDate = Calendar.getInstance();
        Code.DueDate = Calendar.getInstance();
    
        imageViewDateAssign.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                showDateDialog(textViewDtAssign, Code.AssignDate);
            }
        });
    
        imageViewDueDate.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                showDateDialog(textViewDueDt, Code.DueDate);
            }
        });
    
        imageViewSubmit.setOnClickListener(this);
    
        updateDisplay(textViewDtAssign, Code.AssignDate);
        updateDisplay(textViewDueDt, Code.DueDate);
    }
    
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.submit:
    
            Code.title = editTextTitle.getText().toString().trim();
            Code.description = editTextDesc.getText().toString().trim();
            Code.diff = Code.DueDate.getTimeInMillis()
                    - Code.AssignDate.getTimeInMillis();
            Code.days = Code.diff / (24 * 60 * 60 * 1000);
            Code.strDays = String.valueOf(Code.days);
    
            if (isEdit) {
                this.dataManipulator = new DataManipulator(this);
                this.dataManipulator.update(updateId);
                this.dataManipulator.close();
            } else {
                this.dataManipulator = new DataManipulator(this);
                this.dataManipulator.insert(Code.title, Code.description,
                        Code.strDays);
                this.dataManipulator.close();
            }
    
            if (Code.title.length() <= 0 || Code.description.length() <= 0) {
                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                        Assignment_Create.this);
                alertDialogBuilder.setTitle("Your Title");
                alertDialogBuilder
                        .setMessage("Click yes to exit!")
                        .setCancelable(false)
                        .setPositiveButton("Ok",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog,
                                            int id) {
                                    }
                                })
                        .setNegativeButton("Cancel",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog,
                                            int id) {
                                        dialog.cancel();
                                    }
                                });
                AlertDialog alertDialog = alertDialogBuilder.create();
                alertDialog.show();
    
                Toast.makeText(getApplicationContext(), "Not proper date",
                        Toast.LENGTH_LONG).show();
            }
    
            Toast.makeText(getApplicationContext(),
                    "Details are saved successfully", Toast.LENGTH_LONG).show();
            Toast.makeText(getApplicationContext(),
                    "Assignment Created Succesfully", Toast.LENGTH_LONG).show();
            Assignment_Create.this.finish();
            break;
        }
    }
    
    private void updateDisplay(TextView dateDisplay, Calendar date) {
        dateDisplay.setText(new StringBuilder()
                // Month is 0 based so add 1
                .append(date.get(Calendar.MONTH) + 1).append("-")
                .append(date.get(Calendar.DAY_OF_MONTH)).append("-")
                .append(date.get(Calendar.YEAR)).append(" "));
    }
    
    @SuppressWarnings("deprecation")
    public void showDateDialog(TextView dateDisplay, Calendar date) {
        activeDateDisplay = dateDisplay;
        activeDate = date;
        showDialog(DATE_DIALOG_ID);
    }
    
    private OnDateSetListener dateSetListener = new OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                int dayOfMonth) {
            activeDate.set(Calendar.YEAR, year);
            activeDate.set(Calendar.MONTH, monthOfYear);
            activeDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
            updateDisplay(activeDateDisplay, activeDate);
    
            unregisterDateDisplay();
        }
    };
    
    private void unregisterDateDisplay() {
        activeDateDisplay = null;
        activeDate = null;
    }
    
    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DATE_DIALOG_ID:
            return new DatePickerDialog(this, dateSetListener,
                    activeDate.get(Calendar.YEAR),
                    activeDate.get(Calendar.MONTH),
                    activeDate.get(Calendar.DAY_OF_MONTH));
        }
        return null;
    }
    
    @SuppressWarnings("deprecation")
    @Override
    protected void onPrepareDialog(int id, Dialog dialog) {
        super.onPrepareDialog(id, dialog);
        switch (id) {
        case DATE_DIALOG_ID:
            ((DatePickerDialog) dialog).updateDate(
                    activeDate.get(Calendar.YEAR),
                    activeDate.get(Calendar.MONTH),
                    activeDate.get(Calendar.DAY_OF_MONTH));
            break;
        }
    }
    }
    

    Everything work's fine but when i leave both edit-text empty it should show alert-dialog but instead of this it is giving error of Activity has leaked window.

    Below is my logcat...

    12-24 11:32:55.995: E/WindowManager(1940): Activity iqualtech.skirr.Assignment_Create has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@420cf5b0 that was originally added here
    12-24 11:32:55.995: E/WindowManager(1940): android.view.WindowLeaked: Activity iqualtech.skirr.Assignment_Create has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@420cf5b0 that was originally added here
    12-24 11:32:55.995: E/WindowManager(1940):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:363)
    12-24 11:32:55.995: E/WindowManager(1940):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:268)
    12-24 11:32:55.995: E/WindowManager(1940):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:216)
    12-24 11:32:55.995: E/WindowManager(1940):  at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:141)
    12-24 11:32:55.995: E/WindowManager(1940):  at android.view.Window$LocalWindowManager.addView(Window.java:537)
    12-24 11:32:55.995: E/WindowManager(1940):  at android.app.Dialog.show(Dialog.java:278)
    12-24 11:32:55.995: E/WindowManager(1940):  at iqualtech.skirr.Assignment_Create.onClick(Assignment_Create.java:132)
    12-24 11:32:55.995: E/WindowManager(1940):  at android.view.View.performClick(View.java:3517)
    12-24 11:32:55.995: E/WindowManager(1940):  at android.view.View$PerformClick.run(View.java:14155)
    12-24 11:32:55.995: E/WindowManager(1940):  at android.os.Handler.handleCallback(Handler.java:605)
    12-24 11:32:55.995: E/WindowManager(1940):  at android.os.Handler.dispatchMessage(Handler.java:92)
    12-24 11:32:55.995: E/WindowManager(1940):  at android.os.Looper.loop(Looper.java:154)
    12-24 11:32:55.995: E/WindowManager(1940):  at android.app.ActivityThread.main(ActivityThread.java:4624)
    12-24 11:32:55.995: E/WindowManager(1940):  at java.lang.reflect.Method.invokeNative(Native Method)
    12-24 11:32:55.995: E/WindowManager(1940):  at java.lang.reflect.Method.invoke(Method.java:511)
    12-24 11:32:55.995: E/WindowManager(1940):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
    12-24 11:32:55.995: E/WindowManager(1940):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
    12-24 11:32:55.995: E/WindowManager(1940):  at dalvik.system.NativeStart.main(Native Method)
    

    I have tried with some of the following link

    https://stackoverflow.com/search?q=activity+has+leaked+windows+in+android

    but i am not getting what do i need to chage and where exactly. I just want to show alert dialog on submit button click event if both the edit-text is empty. Any help would be appreciated. Thank you in advance.