I have two different activities. The first launches the second one. In the second activity, I call System.exit(0)
in order to force the application to close, bu
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.
The easiest way for achieving this is given below (without affecting Android's native memory management. There is no process killing involved).
Launch an activity using this Intent:
Intent intent = new Intent(this, FinActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
In the target activity FinActivity.class
, call finish() in onCreate
.
Steps Explained:
You create an intent that erases all other activities (FLAG_ACTIVITY_CLEAR_TOP)
and delete the current activity.
The activity destroys itself. An alternative is that you can make an splash screen in finActivity. This is optional.
Short answer: call moveTaskToBack(true) 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.
As long as you're responding to lifecycle events appropriately, neither you nor the user needs to care if your application is still running or not.
So if you want to hide your application call moveTaskToBack()
and let Android decide when to kill it.
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:
AppClass.setkillApplication(true);
finish();
return;
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);
Add this function to the first Activity:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(rquestCode == REQUEST_CODE)
if(resultCode == RESULT_CANCELED)
finish();
}
And add this to the second Activity:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
Log.i(TAG, "Back key pressed");
setResult(RESULT_CANCELED);
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
Try the following. It works for me.
ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
ComponentName componentInfo = taskInfo.get(0).topActivity;
am.restartPackage(componentInfo.getPackageName());