0

I am building an app which uses either some activities in which the user can navigate or uses one specific activity which starts the app when a deeplink is used. In this DeeplinkActivity I have a button with which the app should exit the entire app and not put it into the background. I tried the following code, which only closes the app, but it still remains in the background?!

public void onStopButton(View view) {
    log.info("UI -> Stop this app");
    // this.finishAffinity();
    android.os.Process.killProcess(android.os.Process.myPid());
    // finish();
    System.exit(0);
}

I tried all kinds of suggested combinations, but none of them really exits the app. Anybody have some other (working) code suggestions? I have set minSdkVersion=19 and targetSdkVersion=26

iOS-Coder
  • 1,221
  • 1
  • 16
  • 28
  • What precisely do you mean by "remains in the background?" Do you mean that the task remains in the recent-tasks list (a.k.a., overview screen)? – CommonsWare Aug 22 '18 at 22:58
  • I mean that although the app disappears visibly from the main screen, I can still see it in the list of background apps and if I select it from this list, it will become active. So I suppose that the app is not really 'killed/stopped'. I do remember reading somewhere on StackOverflow that somebody said that Android does not have any means/mechanism to really kill a running app. If you look for answers to questions "How to stop an app by code", I see quite a lot of 'solutions' claiming that one of the above code parts I tested solve the issue. It definitely does not work for my setup (yet). – iOS-Coder Aug 23 '18 at 10:34
  • "I can still see it in the list of background apps" -- what *precisely* is this "list of background apps"? Do you mean the overview screen, formerly known as the recent-tasks list? If so, that is not a list of background apps. It is list of recently-used apps. Showing up in the overview screen does not mean that the app has a running process. – CommonsWare Aug 23 '18 at 10:42
  • Apologies for the confusion I'm actually not an Android (power)user! I mean the task-list in which my closed-app still appears to be visible (or in the background), for which I (and my preferred users/audience) might get confused. Do I need to look for some code-solution to clear the recent-task list then? – iOS-Coder Aug 23 '18 at 12:33

1 Answers1

0

First, note that having a button such as you describe is considered to be an anti-pattern in Android.

That being said, if your minSdkVersion is 21 or higher, replace your onStopButton() with:

public void onStopButton(View view) {
  finishAndRemoveTask();
}

If your minSdkVersion is lower than 21, there are some clunky workarounds that I do not recommend.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I am aware of the anti-pattern and since the client does want this feature in my app I need to implement it somehow. Currently I found a solution, adding the android:excludeFromRecents="true" in my manifest, to the DeepLinkActivity. This at least prevent the app showing-up in the recent-taks list, provided that the app hasn't been started manually. However this behaviour doesn't occur when the app is already started manually and acts by a deeplink-activation. Still it is being shown in the recent-task list for this use-case. – iOS-Coder Aug 24 '18 at 16:18