1

I m trying to exit my application on touching the back button. I ve used the usual code, but it only takes out the app from the screen and when i Click on the square box on the bottom right (also tell me whats it called) i can still see the app running the background i want to remove it totally from the system. i ve tried all of them finish(); and System.exit(0); they just pass the app to the background from the fore ground. I want to remove it completely from the system.

The following the code i am using.

@Override
public void onBackPressed() {

        new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_alert).setTitle("Exit")
                .setMessage("Are you sure?")
                .setPositiveButton("yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        Intent intent = new Intent(Intent.ACTION_MAIN);
                        intent.addCategory(Intent.CATEGORY_HOME);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                        finish();
                        System.exit(0);
                    }
                }).setNegativeButton("no", null).show();

    }
Karan Jtv
  • 25
  • 1
  • 11
  • 1
    take a look at the similar question http://stackoverflow.com/questions/6330200/how-to-quit-android-application-programmatically – Volodymyr Oct 04 '16 at 18:38
  • these are giving the same answers as finish(); or System.exit(0); its not working it just takes my app in the background. Than i ve to manually remove it from the background apps by swiping it right. – Karan Jtv Oct 04 '16 at 18:47
  • Ya i ve tried all of them finish(); and System.exit(0); they just pass the app to the background from the fore ground. I want to remove it compeletly from the system. @VolodymyrKhodonovych – Karan Jtv Oct 04 '16 at 18:49

2 Answers2

4

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); will clear previous activities. Using this, finish() call won't be necessary.

And System.exit(0); is used in java, not android, so don't use this anymore. Use finish() or getActivity().finish() in fragment to close current activity.

Update:

If you want to kill the app in order to not run any more in background, you need to do something like this

ActivityManager am = (ActivityManager) getSystemService(Activity.ACTIVITY_SERVICE); am.killBackgroundProcesses(packageName);

Where packagename is your app package. And you need to provide permission <uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />. I think this need to be requested on M. Have a try.

Cătălin Florescu
  • 5,012
  • 1
  • 25
  • 36
2

if you wanna exit the app from main activity , you can just write :

finish();

or for exit the app from other activity , you should do like this in other activity :

    //in other activity onBackPressed()     
    Intent intent = new Intent(OtherActivity.this, MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.putExtra("EXIT", true);
    startActivity(intent);

and write this in onCreate() for MainActivity

 if (getIntent().getBooleanExtra("EXIT", false)) {
        finish();
        System.exit(0);
    }

that's work for me and I'll hope it work for you too.

Nickan
  • 466
  • 5
  • 17
  • Don't use `System.exit(0)`. Just `finish()`. This is Android, not java. – Cătălin Florescu Oct 04 '16 at 19:05
  • finish() - finishes the activity where it is called from and you see the previous activity. System.exit(0) - restarts the app with one fewer activity on the stack. So, if you called ActivityB from ActivityA, and System.exit(0) is called in ActivityB, then the application will be killed and started immediately with only one activity ActivityA – Cătălin Florescu Oct 04 '16 at 21:08
  • its not working just taking it from foreground to background. – Karan Jtv Oct 04 '16 at 21:09
  • thats what exactly is happening everytime..! – Karan Jtv Oct 04 '16 at 21:10