2

I have a login activity. After login, the Main activity is started, which uses fragments for user navigation. I want a button in the nav drawer of my main activity which will completely close the app

Now, I have seen many many threads on this and have tried implementing their solutions. For example:

I have tried finishAffinity() in my Main activity, which should close the current activity as well as all parent activities

I have tried using finish() on the Login activity as soon as I bring up the Main Activity, and then calling finish() again when the user clicks the button

The highest voted answer for this question: Close application and remove from recent apps/, also does not seem work. First, android:autoRemoveFromRecents="true" requires API > 21, but even if I set the minimum SDK version to 21, the app still remains in the list

Finally, I have tried using an Intent when the user clicks the quit button and navigating back to the Login activity, and setting flags with an exit extra, and then finishing the Login activity (i.e. exit android application programmatically)

None of these are working. They will all close the Main Activity, and maybe even close the Login activity. But if the user clicks the app list/current apps/open apps key (the square soft key on most phones), the app is still visible there. When the app is clicked in that list it will take me back to the Login activity screen (I'm unsure if this is starting the app from fresh, or whether its just taking me to the previous Login screen which didn't close)

Out of desperation I have even tried System.exit(0), which I know is bad, but even that doesn't remove the app from the app list

So, how do I programmatically completely quit an app and remove all traces of it being open?

EDIT: I was too hasty in claiming one of the answers below didnt work (see italics above). The answer does remove the app correctly

Community
  • 1
  • 1
Simon
  • 9,762
  • 15
  • 62
  • 119
  • 1
    You're not supposed to do that. Android caches app processes so that they stay in memory as long as possible and are reused if the user wants to use the app again. App developers should expect this behavior. – Doug Stevenson Mar 28 '16 at 08:51
  • 1
    Possible duplicate of [Close application and remove from recent apps/](http://stackoverflow.com/questions/22166282/close-application-and-remove-from-recent-apps). Also see [Remove app from recent apps programmatically](https://stackoverflow.com/questions/13385289/remove-app-from-recent-apps-programmatically). – corsair992 Mar 28 '16 at 08:58

3 Answers3

1

I think this is the solution you're looking for.

You might consider having another activity named ExitActivity which will be called when you try to exit from your application. The trick here is, the ExitActivity will have android:autoRemoveFromRecents set to true in the manifest file, so that your instance will be cleared automatically from the recents.

Community
  • 1
  • 1
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
0

Based on my research:

There is no way to force quite an application in android. You can only pause an activity. The discretion to quit an app lies totally with android framework which it does on checking the memory and resource utilization of apps. I could not find the official link for now, but I had read it earlier.

Utsav Gupta
  • 3,785
  • 5
  • 30
  • 52
0

manage it by the OS. bring up the home screen.

Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_HOME); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent);

Anurag Rabadia
  • 103
  • 2
  • 12