You can also specify noHistory = "true" in the tag for first activity or finish the first activity as soon as you start the second one(as David said).
AFAIK, "force close" kills the process which hosts the JVM in which your application runs and
System.exit() terminates the JVM running your application instance. Both are form of abrupt terminations and not advisable for normal application flow.
Just as catching exceptions to cover logic flows that a program might undertake, is not advisable.
Short answer: call ABC0 on your Activity instead of System.exit(). This will hide your application until the user wants to use it again.
The longer answer starts with another question: why do you want to kill your application?
The Android OS handles memory management and processes and so on so my advice is just let Android worry about this for you. If the user wants to leave your application they can press the Home button and your application will effectively disappear. If the phone needs more memory later the OS will terminate your application then.
android.os.Process.killProcess(android.os.Process.myPid()); works fine, but it is recommended to let the Android platform worry about the memory management:-)
Keep in mind that when working with applications that use persistent socket connections, the finish() method does not release the connection. Under normal circumstances, finish() is the best option, but if you absolutely need to exit an app and release all resource it's using then use killProcess. I've had no problems using it.
Android has a mechanism in place to close an application safely per its documentation. In the last Activity that is exited (usually the main Activity that first came up when the application started) just place a couple of lines in the onDestroy() method. The call to System.runFinalizersOnExit(true) ensures that all objects will be finalized and garbage collected when the the application exits. For example:
public void onDestroy() {
super.onDestroy();
/*
* Notify the system to finalize and collect all objects of the
* application on exit so that the process running the application can
* be killed by the system without causing issues. NOTE: If this is set
* to true then the process will not be killed until all of its threads
* have closed.
*/
System.runFinalizersOnExit(true);
/*
* Force the system to close the application down completely instead of
* retaining it in the background. The process that runs the application
* will be killed. The application will be completely created as a new
* application in a new process if the user starts the application
* again.
*/
System.exit(0);
}
Finally Android will not notify an application of the HOME key event, so you cannot close the application when the HOME key is pressed. Android reserves the HOME key event to itself so that a developer cannot prevent users from leaving their application.
Start the second activity with startActivityForResult and in the second activity return a value, that once in the onActivityResult method of the first activity closes the main application. I think this is the correct way Android does it.
The way I do this is by saving a flag in a static variable available to all. Then, when I exit, I set this flag and all my activities check this flag onResume. If the flag is set then I issue the System.exit on that activity.
That way all activities will check for the flag and will close gracefully if the flag is set.
Run the second activity using start activity for result:
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
//This line is important
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivityForResult(intent, REQUEST_CODE);
You are wrong. There is one way to kill an application. In a class with super class Application, we use some field, for example, killApp. When we start the splash screen (first activity) in onResume(), we set a parameter for false for field killApp. In every activity which we have when onResume() is called in the end, we call something like that:
if(AppClass.killApp())
finish();
Every activity which is getting to the screen have to call onResume(). When it is called, we have to check if our field killApp is true. If it is true, current activities call finish(). To invoke the full action, we use the next construction. For example, in the action for a button:
1) The parent activity call the secondary activity with the method "startActivityForResult"
2) In the secondary activity when is closing:
int exitCode = 1; // Select the number you want
setResult(exitCode);
finish();
3) And in the parent activity override the method "onActivityResult":
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
int exitCode = 1;
if(resultCode == exitCode) {
super.setResult(exitCode); // use this if you have more than 2 activities
finish();
}
}
This is what I did to close the application:
In my application I have a base activity class, I added a static flag called "applicationShutDown".
When I need to close the application I set it to true.
In the base activity onCreate and onResume after calling the super calls I test this flag.
If the "applicationShutDown" is true I call finish on the current Activity.
I solved a similar problem: MainActivity starts BrowserActivity, and I need to close the app, when user press Back in BrowserActivity - not to return in MainActivity. So, in MainActivity:
public class MainActivity extends AppCompatActivity {
private static final String TAG = "sm500_Rmt.MainActivity";
private boolean m_IsBrowserStarted = false;
and then, in OnResume:
@Override
protected void onResume() {
super.onResume();
if(m_IsBrowserStarted) {
Log.w(TAG, "onResume, but it's return from browser, just exit!");
finish();
return;
}
Log.w(TAG, "onResume");
... then continue OnResume. And, when start BrowserActivity: