how to close android app completely
Asked Answered
R

9

19

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

Realize answered 15/1, 2014 at 6:29 Comment(2)
Try thisTrencherman
possible duplicate of Android: Proper Way to use onBackPressed()Liverwort
D
62

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);
Dameron answered 15/1, 2014 at 6:33 Comment(9)
you are just calling home via intent. How is it different from calling another activity's intent. i dont think the app will close. instead it will minimize.Esch
whats your requirement ??Dameron
I just want to make sure that the above code Actually Quits the app or just brings home screen on front. just to be clear :)Esch
It Quits the app... Do check itDameron
Only if you also include super.onBackPressed() and no other activity is in the backstack other wise it does't.Esch
It was a general answer.... i have just posted the required code which can be used anywhere.. and i have clearly mentioned on Button click.Dameron
let us continue this discussion in chatEsch
KillProcess should be killProcess, the first letter K be small k, fixed.Runion
Thank you guys! Saved my neck, also the chat was helpful!Screens
J
24

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.

Jacquesjacquet answered 28/10, 2014 at 12:29 Comment(1)
finishAffinity() is only available in API level 16 or higher.Shine
L
11

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.

Laywoman answered 2/4, 2018 at 1:17 Comment(2)
it didn't work when I have another activity below current activity. finishAffinity worked however.Archean
You can check before if the activity is the root activity with isTaskRoot(). Then it works great.Qulllon
H
5

To Finish an Activity I'm using this code:

public void appExit () {
    this.finish();
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
}  //close method

or kill Process with this code:

int pid = android.os.Process.myPid();
android.os.Process.killProcess(pid);
Herwin answered 15/1, 2014 at 6:39 Comment(1)
KillProcess should be killProcess, the first letter K be small k, fixed.Runion
T
3

For Xamarin Users:

int pid = Android.OS.Process.MyPid();
Android.OS.Process.KillProcess(pid);

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:

public override void Finish()
    {
        if (Android.OS.Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
        {
            base.FinishAndRemoveTask();
        }
        else
        {
            base.Finish();
        }
    }

this is the sole solution for that question!

Translate answered 7/6, 2017 at 7:46 Comment(0)
F
0

Even if it looks ok killing process will cause you headache in future. You can ensure this by following use case:

  1. call several activities to populate activities stack
  2. call android.os.Process.killProcess(pid);
  3. open your application

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?

Fichu answered 3/6, 2014 at 12:46 Comment(0)
B
0
android.os.Process.killProcess(android.os.Process.myUid());

I think this is better. How about it ?

Belga answered 5/10, 2016 at 17:36 Comment(0)
L
0

Closing the application with process removal, it gives the result of a smooth closing and removal of all application processes after 100 milliseconds:

enter image description here


override fun exit() {
    coroutine.launch(Dispatchers.Main) {
        finishAndRemoveTask()
        delay(100)
        exitProcess(0)
    }
}
Lakin answered 23/7, 2023 at 8:40 Comment(0)
F
0

In my case i used this method. It already worked for me. I use this technique with Jetpack Compose peoject.

val context = LocalContext.current

BackHandler {
    val intent = Intent(Intent.ACTION_MAIN)
    intent.addCategory(Intent.CATEGORY_HOME)
    context.startActivity(intent)
}
Fao answered 19/2 at 17:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.