I have two different activities. The first launches the second one.
Intent intent = new Intent(this, Activity2.class);
startActivity(intent);
In the second activity I call System.exit(0). The first activity comes back caused by 'page stack' I think. But I found two things happened.
applicationContext.openFileOutput(fileName, Context.MODE_PRIVATE);
Was sandbox cleaned in that situation? The normal exit by 'return key' or even by android.os.Process.killProcess(android.os.Process.myPid())
, the file in sandbox was kept.
So, what actually happened when System.exit(0) execute?
Thanks!
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 :)
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With