When using System.exit() the app does't closes

1.3k Views Asked by At

I have a app in which at Home Screen onBackPressed() i have used System.exit() but nither my app closes infact it gives me forceClose.

Home.Class

 @Override
    public void onBackPressed() {
        super.onBackPressed ();
        System.exit (0);
    }  
5

There are 5 best solutions below

8
On

Use finish() method instead of System.exit (0);

1
On

Use finish() instead of System.exit(0).

0
On

System.exit(0) or finish() will not close the app it will exit from the current activity and will take you to the last activity.

If you want to exit from app try this may be it help

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

for more information look over this link

1
On

It suggested that you finish the current activity after you start a new activity

For example :

//MainActivity
{
//somecode there

startActivity(new Intent(MainActivity.this,SecondActivity.class)); //start a new activity
finish();// finish the activity so that when you press the backbutton it exits the app.
}
0
On

try this code

 @Override
public void onBackPressed() {
    super.onBackPressed();
      if (backpressvalue == 0) {
      Intent intent = new Intent(ControlScreen.this, MyCameras.class);
      finish();
      }
}