-4

I have a problem with my apps,i want to close my app when clicking the back button. But When clicking back from this screen, application back to prev screen. I try to add all exit script, but not work, Help Please. My Script Is :

public void onBackPressed() {
    Exit = new AlertDialog.Builder(tab1.this).create();
    Exit.setTitle("Exit Program");
    Exit.setMessage("Exit?");
    Exit.setButton("Ya", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {
            android.os.Process.killProcess(android.os.Process.myPid());
            Intent intent = new Intent();
            System.exit(0);
            finish();
            System.runFinalizersOnExit(true);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.setAction(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_HOME);

            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);

        }
    });

    Exit.setButton2("No", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
        // here you can add functions
            Exit.dismiss();
        }
    });   


    Exit.show();
}

// Before 2.0
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        Exit = new AlertDialog.Builder(tab1.this).create();
        Exit.setTitle("Exit Program");
        Exit.setMessage("Exit?");
        Exit.setButton("Ya", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                // Progress
                android.os.Process.killProcess(android.os.Process.myPid());
                Intent intent = new Intent();
                System.exit(0);
                finish();
                System.runFinalizersOnExit(true);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent.setAction(Intent.ACTION_MAIN);
                intent.addCategory(Intent.CATEGORY_HOME);

                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        });

        Exit.setButton2("No", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            // here you can add functions
                Exit.dismiss();
            }
        });   


        Exit.show();

        return true;
    }

    return super.onKeyDown(keyCode, event);
}
SilentKiller
  • 6,944
  • 6
  • 40
  • 75
  • 3
    This doesn't seem like a very good idea since it subverts the expected behavior of the back button. – Code-Apprentice Sep 01 '14 at 04:37
  • I think if you have [searched](http://www.google.com/search?q=stop+exit+kill+android+app+programatically) enough you will have your answer. Also please see [this question](http://stackoverflow.com/questions/2033914/quitting-an-application-is-that-frowned-upon). Welcome to StackOverflow btw :) – Keale Sep 01 '14 at 04:44
  • Thx for comment and share link, this is very helpful for me :) – user3811450 Sep 01 '14 at 05:02

3 Answers3

0

Try this sequence :

    MYApplication.getInstance().clearApplicationData();
    android.os.Process.killProcess(android.os.Process.myPid());
    Intent intent1 = new Intent(Intent.ACTION_MAIN);
    intent1.addCategory(Intent.CATEGORY_HOME);
    intent1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent1);

    finish();
KOTIOS
  • 11,177
  • 3
  • 39
  • 66
  • thx for your comment, it's work, but i add script for start splash screen my apps before exit with your script. :) – user3811450 Sep 01 '14 at 05:04
0

This is a fair and simple way of exiting an android app programmatically in my opinion:

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();

Hope it helps.

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
-2

Avoid killProcess
Try this code :

Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
activity.startActivity(startMain);
System.exit(-1);
SilentKiller
  • 6,944
  • 6
  • 40
  • 75
SweetWisher ツ
  • 7,296
  • 2
  • 30
  • 74
  • You say "avoid `killProcess`, but `System.exit` is essentially the same. Neither one are typically used in Android development. – Alex Lockwood Sep 01 '14 at 05:00
  • thx for your comment, it's work, but i add script for start splash screen my apps before exit with your script. :) – user3811450 Sep 01 '14 at 05:04
  • 99.9% of apps should not "exit" at all. Using `System.exit` is just plain wrong here. Please show me the `exit` button on this web page which, when clicked, closes my browser. – Simon Sep 01 '14 at 05:20