I created an android application with a logout option in onCreateOptionsMenu
. The Logout works perfectly but when I press the back button again it takes me to the previous activity, and when it gets a null value it redirects to login screen. Now my login screen appears again and again. I want to close the app completely when I press the back button but don't know how.
Here is my code to handle the back button press in the login screen:
@Override
public void onBackPressed() {
new AlertDialog.Builder(this)
.setTitle("BidNEMO")
.setMessage("Are you sure you want to exit?")
.setNegativeButton(android.R.string.no, null)
.setPositiveButton(android.R.string.yes, new OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
Main.super.onBackPressed();
finish();
}
}).create().show();
}
please help guyss..
To Finish an Activity I'm using this code:
or kill Process with this code:
I think this is better. How about it ?
Even if it looks ok killing process will cause you headache in future. You can ensure this by following use case:
OBSERVED: Your application opens not from main activity in inconsistent way. This is because Android consider killing of process like crash.
You can find more detailed information here: Is quitting an application frowned upon?
To
Quit Application
on Button click use this code :Try it..
To kill the complete app and remove it from
Running
app list kill the app through itspid
(its nasty)... use this lines before above code.For API 21 and up
finishAndRemoveTask()
You can call this to close the app completely. All activities will
finish()
and the app is removed from the task list.For Xamarin Users:
put it in your
OnDestroy()
function.Edit:
After investigating it thoroughly, I found out that even the above code I wrote does not "Kill" the app totally (deleting it from task manager - "recent apps"). Eventually, after a lot of code tryouts, I managed to figure something out, Overriding "Finish" functions with this code:
this is the sole solution for that question!