What Will Happen When System.exit(0) Execute?
Solution 1:
You can do one thing:
Donot use System.exit(0); instead you can just use finish() as follows:
Intent intent = new Intent(this, Activity2.class);
startActivity(intent);
finish();
Here data will not be loss.HTH :)
Solution 2:
What will happen when System.exit(0) execute?
The VM stops further execution and program will be exit.
Now, in your case the first activity comes back due to activity stack. So when you move from one activity to another using Intent
, do the finish()
of current activity like this.
Intent intent=new Intent(getApplicationContext(), NextActivity.class);
startActivity(intent);
CurrentActivity.this.finish();
This will guarantee that no activity running when we close the application.
And for exiting from application use this code:
MainActivity.this.finish();
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);
getParent().finish();
And you should not use System.exit()
if your application use any resource in background like Music player that plays song from background or any application that uses internet data in background or any widget that depends on your application.
For more information go through the following references:
Solution 3:
Read the documentation:
http://developer.android.com/reference/java/lang/System.html#exit(int)
Solution 4:
So, what actually happened when System.exit(0) execute?
android.os.Process.killProcess(android.os.Process.myPid())
and System.exit(0)
are the same. When you call any of them from the second activity, then the application will be closed and opened again with only one activity (we assume that you had 2 activities). You can check this behaviour by putting logging (Log.i("myTag", "MainActivity started");
) in onCreate menthod of your main activity.
Post a Comment for "What Will Happen When System.exit(0) Execute?"