1

i have an android application .. i want whrn the user clicks on exit button it will displays a confirmation message and if he clicked yes the close the whole application ..

tried this code but didn't work:

AlertDialog.Builder alertBuilder=new AlertDialog.Builder(AdminEditProfile.this);
alertBuilder.setTitle("Exit");
alertBuilder.setMessage("Are you sure you want to exit from the application?");
alertBuilder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

    public void onClick(DialogInterface dialog, int which) {

        dialog.cancel();
        android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(1);
    }
});
alertBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {

    public void onClick(DialogInterface dialog, int which) {

        dialog.cancel();    
    }
});
alertBuilder.create().show();

is there a way to do it >

please help, thank you :).

Luciano Rodríguez
  • 2,239
  • 3
  • 19
  • 32
tatwany
  • 47
  • 7
  • post you log-cat error. – Yugesh Nov 25 '14 at 07:55
  • 1
    It is a **bad practice** to forcibly close your app. Use the `Home` and the `Back` buttons, instead. – Phantômaxx Nov 25 '14 at 07:56
  • 1
    Here's why it's bad practice: http://android.nextapp.com/site/fx/doc/exit – Kevin Krumwiede Nov 25 '14 at 07:58
  • And Google has a habit of deprecating and eventually crippling parts of their API that they feel are being misused. Like in 5.0, they added a legit (but somewhat limited) kiosk mode, and nuked the hacks that people used to use to simulate it. So even if you find a hack to fully exit your application, don't count on it always working. – Kevin Krumwiede Nov 25 '14 at 08:00
  • You can find your answer here: http://stackoverflow.com/a/5036668/1557187 – Assaf Gamliel Nov 25 '14 at 08:00

4 Answers4

3

You are killing your process and that a bad idea. please take a look at this post to find out the reason:

Why calling Process.killProcess(Process.myPid()) is a bad idea?

I suggest you just using finish(); instead.

And if you want to finish the whole application (depends on what you need exactly) I suggest you see this post:

How to close Android application?

Community
  • 1
  • 1
Milad Faridnia
  • 9,113
  • 13
  • 65
  • 78
2

Just finish(); can do the work to close Activity. Is there any Service running in background ?

Don Chakkappan
  • 7,397
  • 5
  • 44
  • 59
1

If it is a simple app you can call finish() and close all of yours opened activities. Maybe you should log out user from your app?

Unii
  • 1,587
  • 15
  • 33
  • finish(); will close the current activity and return me to the previous one .. i want to close the whole appliction .. is there a way ? – tatwany Nov 26 '14 at 12:36
  • use broadcastreceiver and register your all activities for specific action - then close all activities that obtain this info – Unii Nov 26 '14 at 12:49
  • thanks for the replay .. and i found a correct answer :) – tatwany Nov 26 '14 at 12:56
-1

My Sample code try this

    new AlertDialog.Builder(MainActivity.this)
      .setTitle("Application Informaiton")
       .setMessage("" )
       .setPositiveButton("Got it", new 
         DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) { 
         // continue with delete
        }
        })
      .setCancelable(false)
      .show();

        }
        else if(item.getTitle().equals("Stop this Application"))
        {
            new AlertDialog.Builder(MainActivity.this)
           .setTitle("Stop application?")
           .setMessage("Stop this application and exit? You'll need to
            re-launch the application to use it again.")
           .setPositiveButton("Don't stop", new 
            DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int which) { 
            // continue with delete
            }
            })
            .setNegativeButton("Stop and exit", new 
              DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int which) { 
                // continue with delete
                MainActivity.this.finish();
                Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.addCategory(Intent.CATEGORY_HOME);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                    startActivity(intent);
                                }
                             })
                            .setCancelable(false)
                             .show();
krishnan muthiah pillai
  • 2,711
  • 2
  • 29
  • 35