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..
What happens when you close apps on Android? Closing an app stops it from running in the background. It won't sign you out of any apps, but opening the app again will start a new instance. You will need to keep an app running in the background if you want to continue where you left off.
The truth is you do not need to kill Android apps. In fact, closing apps can make things worse. It's unclear where this idea came from, but it's been present on Android since the very beginning.
To Quit Application
on Button click use this code :
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
Try it..
To kill the complete app and remove it from Running
app list kill the app through its pid
(its nasty)... use this lines before above code.
int pid = android.os.Process.myPid();
android.os.Process.killProcess(pid);
If you want to close application completely you should use finishAffinity();
instead of finish()
.
It will clear all stack of activities previously opened by an application.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With