0

I need to finish an application when it goes to the background, I'm using method finishAffinity() but it seems it does not work, someone can tell me another alternative

  @Override
    protected void onPause() {
    finishAffinity()
    super.onPause();
}
Dr Mido
  • 2,414
  • 4
  • 32
  • 72
W1ll
  • 177
  • 9
  • If you call `System.exit(0);` *directly after* `finishAffinity();` then the shutdown works *properly* and data members are reset to their initial default values. – Jon Goodwin Mar 05 '19 at 00:08

1 Answers1

1

Here's answer

finishAffinity() is not used to "shutdown an application". It is used to remove a number of Activitys belonging to a specific application from the current task (which may contain Activitys belonging to multiple applications).

Even if you finish all of the Activitys in your application, the OS process hosting your app does not automatically go away (as it does when you call System.exit()). Android will eventually kill your process when it gets around to it. You have no control over this (and that is intentional).

you can use this

    public void endTask() {
    // Is the user running Lollipop or above?
    if (Build.VERSION.SDK_INT >= 21) { 
        // If yes, run the fancy new function to end the app and
        //  remove it from the task list.
        finishAndRemoveTask();
    } else {
        // If not, then just end the app without removing it from
        //  the task list.
        finish();
    }
}

Source and read more

Community
  • 1
  • 1
Dr Mido
  • 2,414
  • 4
  • 32
  • 72