-6

I've read there are a lot of ways to close an application, so I would like to know if this way is correct or there are defects

main_activity.java

public boolean onOptionsItemSelected(MenuItem item) {
    .....
    .....
    else if (id == R.id.exit) {
        onDestroy();
    }
}

@Override
protected void onDestroy() {
    System.exit(0);
    super.onDestroy();
    finish();
}

Is this conceptually correct?

Marko
  • 20,385
  • 13
  • 48
  • 64

2 Answers2

1

To close your app, this pinch of code will help you do the job.

android.os.Process.killProcess(android.os.Process.myPid()); 

Android How to programmatically close an app..

But you may want to know that I have never seen this approach in use anywhere. Even apps from certified developers do not try to close and remove themselves from processes. What you can really do and preferred is finishing all the running activities and services and hence allow user to kill apps from recent apps himself (default behaviour of any app).

This functions closes all activities but not app -

finishAffinity(); // API 16

Or you can call

finish();

everytime you end an activity.

These do not close Services to best of my knowledge and you programmatically have to stop running Services.

Cheers!

0

The best way to exit an app is using

finishActivity();

You don't want to kill the whole process, it is not good practice.

Swami
  • 163
  • 2
  • 10