-6

Logcat:

06-13 18:25:37.534: E/WindowManager(420): Activity com.dimensionsco.thankbunny.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@43ef3c98 that was originally added here

This is the code i was using to exit application with alert dialog. But it is ending up with errors. I dont understand where i went wrong.I am running it on emulator. Could any one solve this? Thanks in advance

@Override
public void onStop() {
    super.onStop();
    Toast.makeText(getBaseContext(), "stop", Toast.LENGTH_LONG).show();
    AlertDialog.Builder alertDialogBuilde = new AlertDialog.Builder(MainActivity.this);
    alertDialogBuilde.setTitle(this.getTitle() + "EXIT");
    alertDialogBuilde.setMessage("DO you want to exit?");
    AlertDialog alertDialogr = alertDialogBuilde.create();

    alertDialogBuilde.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int id) {

            // go to a new activity of the app
            dialog.cancel();
            // finish();
        }

    });

    alertDialogBuilde.setNegativeButton("No", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();

        }
    });

    // set neutral button: Exit the app message
    alertDialogBuilde.setNeutralButton("Exit the app", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int id) {
            // exit the app and go to the HOME
            System.exit(0);
            // MainActivity.this.finish();
        }
    });

    alertDialogr.show();
}
Xaver Kapeller
  • 49,491
  • 11
  • 98
  • 86
Nikesh Devaki
  • 2,091
  • 2
  • 16
  • 24

3 Answers3

2

You can't create UI or interact with the user in onStop(). Please refer to the Activity Lifecycle documentation. By the time onStop() is executed, the activity is already invisible and is being released, so you can't interrupt that process anyway. What's worse, if you could interact here, your finish() would then invoke onStop() again...

If you need to intercept a user-initiated exit, overwrite onBackPressed() and prompt your dialog in there.

Keep in mind that activities may be paused and stopped for all sorts of reasons, including incoming phone-calls. You certainly wouldn't want a user to have to confirm your prompt in order to answer his phone...

323go
  • 14,143
  • 6
  • 33
  • 41
  • i have got anther bug here. Now dialog is popping up, but "YES","No" buttons are not present in dialog. – Nikesh Devaki Jun 13 '14 at 13:54
  • Move `AlertDialog alertDialogr = alertDialogBuilde.create();` to below the `set***Button` calls and before the `alertDialogr.show()` – 323go Jun 13 '14 at 13:58
0

You finish the activity from the dialog here, but the alert is not destroyed so it leaks. You need to close the dialog first, then finish the activity.

alertDialogBuilde.setNeutralButton("Exit the app", new DialogInterface.OnClickListener() {

    public void onClick(DialogInterface dialog, int id) {
        // exit the app and go to the HOME
        System.exit(0);
        // MainActivity.this.finish();
    }
});
mihail
  • 2,173
  • 19
  • 31
-2

System.exit(0) is not a good idea to close Activities, instead call finish(); System.exit calls the garbage collector and then, nothing else but the garbage collector runs in Your system. All other running processes will be paused during the gc. This method should only be used for very necessary reasons.

But, please post the complete Logcat output so that the poeple here can see exactly what has happend. Maybe we need to see more of Your code, to know where the issue is.

Anyway the reason why this error happens could be, that You are not closing Your dialog before finisish. So you have to do the following:

alertDialogBuilde.setNeutralButton("Exit the app", new DialogInterface.OnClickListener() {

    public void onClick(DialogInterface dialog, int id) {
        // exit the app and go to the HOME
        dialog.cancel();
        finish();
        // MainActivity.this.finish();
    }
});
Kirk Backus
  • 4,776
  • 4
  • 32
  • 52
Opiatefuchs
  • 9,800
  • 2
  • 36
  • 49
  • 1
    That's not an answer to the "question," but a comment. And it has been commented. – 323go Jun 13 '14 at 13:38
  • 1
    and that the reason to give -1? comments should not as long as this what I wrote and please read completely through my answer before judging me – Opiatefuchs Jun 13 '14 at 13:40
  • Nobody is "judging" **you**. However, your answer doesn't answer the question. And yes, an incorrect answer is a reason to downvote. What other reason should there be? – 323go Jun 13 '14 at 13:49