Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to close android app completely

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..

like image 586
Brett Avatar asked Jan 15 '14 06:01

Brett


People also ask

Does closing apps on Android do anything?

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.

Should you force close apps on Android?

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.


Video Answer


2 Answers

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 Runningapp 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);
like image 176
Amresh Avatar answered Sep 23 '22 22:09

Amresh


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.

like image 45
Anonymous Avatar answered Sep 25 '22 22:09

Anonymous