How can I exit from my app?
I want that when the user will clicked on the button, the app will exit completely, I saw any answers about it, but they took the app to back
in this method you have can call exit()
function anywhere and get exit from the app. so For closing an app at any point use FLAG_ACTIVITY_CLEAR_TOP
flag in Intent & then system.exit();
NOTE : I think in way it will not break the Android Life cycle
In a place where you need to execute EXIT from the app
public void exit() {
startActivity(new Intent(this, HomeActivity.class).
setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK).putExtra(EXIT_FLAG, true));
}
MainActivity.onCreate()
protected void onCreate(Bundle savedInstanceState) {
if (getIntent().getBooleanExtra(EXIT_FLAG, false)) {
if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == 0) {
finish();
}
}
You may use,
this.finishAffinity();
then according to the Doc.
Finish this activity as well as all activities immediately below it in the current task that have the same affinity. This is typically used when an application can be launched on to another task (such as from an ACTION_VIEW of a content type it understands) and the user has used the up navigation to switch out of the current task and in to its own task. In this case, if the user has navigated down into any other activities of the second application, all of those should be removed from the original task as part of the task switch.
Note that this finish does not allow you to deliver results to the previous activity, and an exception will be thrown if you are trying to do so.
for more info about the Android docs, take a look at: http://developer.android.com/reference/android/app/Activity.html