I want to close my application, so that it no longer runs in the background.
How to do that? Is this good practice on Android platform?
If I rely on the \"ba
Use "this.FinishAndRemoveTask();
" - it closes application properly
I think it will close your activity and all Sub activity related to it.
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();]
if (id == R.id.Exit) {
this.finishAffinity();
return true;
}
return super.onOptionsItemSelected(item);
}
Simply write the following code in onBackPressed:
@Override
public void onBackPressed() {
// super.onBackPressed();
//Creating an alert dialog to logout
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setMessage("Do you want to Exit?");
alertDialogBuilder.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
}
});
alertDialogBuilder.setNegativeButton("No",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
}
});
//Showing the alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
Use of finishAffinity()
may be an good option if you want to close all Activity of the app. As per the Android Docs-
Finish this activity as well as all activities immediately below it in the current task that have the same affinity.