11

I have 2 activity, so activity 1 go to activity 2 then on activity 2 I have an exit button. But when I click it, all it only exited the activity number 2 and return to activity 1 again. Its basically felt like I just started the application again. I am not sure why?

This is my code.

Button btExit = (Button) findViewById(R.id.btExit);
    btExit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
            System.exit(0);
        }
    });
Lucifer
  • 28,933
  • 23
  • 88
  • 140
zBaoAnhLe
  • 253
  • 1
  • 6
  • 19

7 Answers7

47
System.exit(0);

is a bad way of termination of android apps. Android manages it in its own os

You can bring up the Home application by its corresponding Intent:

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

hope this helps

EDIT :-

Then I suppose you are aiming at finishing all the stacked up activity..

Here it is :-

Closing all the previous activities as follows:

Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("Exit me", true);
startActivity(intent);
finish();

Then in MainActivity onCreate() method add this to finish the MainActivity

if( getIntent().getBooleanExtra("Exit me", false)){
    finish();
}

The result will be same as above, but because all your stacked up activities are closed, when you come back to you app it must start from your main activity i.e launcher activity.

Hope this helps.

CRUSADER
  • 6,225
  • 3
  • 32
  • 62
  • @MaciejGórski Bringing up the HOME application (or using `moveTaskToBack()`) won't change the current state of this task. That means, using this example, that when the user starts this application again from the HOME screen, it will bring the existing task (containing Activity1 and Activity2) back to the front and show the user Activity2 (which is probably **not** what OP wants to happen. – David Wasser May 10 '13 at 12:17
  • @DavidWasser I'm aware of that. That's why my answer is different. My comment was to simplify CRUSADER's and Nunu's answer. – MaciejGórski May 10 '13 at 12:30
  • CRUSADER. i tried your code it did bring me out of my application but it doesnt close it, when i use it again...it turn back on where i left it...are you able to over come this too? – zBaoAnhLe May 10 '13 at 12:57
  • please [read the post](http://stackoverflow.com/a/2034238/2345913) to clear your concept – CRUSADER May 10 '13 at 16:15
  • This method will restart your `MainActivity.class` . Check http://stackoverflow.com/a/26258455/609782 – Darpan Oct 08 '14 at 13:56
  • Again. Closin an activity does not close the application. In production environment, we dont want the app to keep login and passwords of other users... in the background to be restored and 'hacked' by others. Acitivty is not the only class that programmers use to create apps. – TomeeNS Oct 02 '15 at 02:49
7

Don't use System.exit.

If you want user to close app from any Activity I suggest using startActivityForResult, checking returned value in onActivityResult in first Activity and calling finish() there too.

MaciejGórski
  • 22,050
  • 7
  • 68
  • 93
  • 3
    Why? finish does not closes the application, only activity is closed. People, try to understand that we have many other classes that are not derived from Activity. They may be used to automate DB conection, store login id, user name, last query results... So closing activity does NOT SOLVE the problem in production environment. Aaa, cześć Maciek ;) – TomeeNS Oct 02 '15 at 02:44
4

You can either simulate hitting the home button:

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

but this will not close the app..

to close it, you can do as https://stackoverflow.com/a/9735524/1434631

Community
  • 1
  • 1
Nermeen
  • 15,770
  • 5
  • 57
  • 71
  • 1
    Bringing up the HOME application won't change the current state of this task. That means, using this example, that when the user starts this application again from the HOME screen, it will bring the existing task (containing Activity1 and Activity2) back to the front and show the user Activity2 (which is probably not what OP wants to happen. The linked solution is better. – David Wasser May 10 '13 at 12:20
2

Use
finishAffinity(); to exit from the application .

finish() will only clears the activity from the activity stack.

krishnamurthy
  • 1,524
  • 13
  • 13
1

System.exit(0) does not work for closing application

  ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
    am.killBackgroundProcesses("com.root.abc");
    
    System.runFinalizersOnExit(true);
    System.exit(0);


add Manifest permission
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
Community
  • 1
  • 1
0

use finish() and a sharedPreference flag and set the flag when you click button. on your other activity, check the flag and finish() it if the flag is set

Jithu
  • 1,458
  • 1
  • 13
  • 21
0

Finish the first activity by calling finish(); on the buttonclick after passing the intent to start the next activity.

ASP
  • 3,725
  • 1
  • 30
  • 44