6

My app uses a DefaultHttpClient to make network requests. On occasion (usually after resuming the app after a long period of time) the app will stop loading data and I need to clear it from the recent apps list before it loads content again.

My question is, what's the difference between: - Closing an app by tapping the back button - Clearing the app from the recent app list

When my app stops loading content, exiting via the back button and reopening does not fix the problem. Only killing the app by clearing it from the recent app list works.

Is there a way to "kill" the app when a user exits with the back button?

Thanks

Michael J
  • 825
  • 1
  • 9
  • 18

3 Answers3

7

Closing an app by tapping the back button

Pressing the BACK button, by default, destroys whatever the foreground activity is, returning control to the previous activity (or the home screen if there is no previous activity). It does not "close" an app.

Clearing the app from the recent app list

Usually, this will terminate the app's process. Contrast that with pressing BACK to destroy all of your activities, where the process remains running (at least for a while).

Is there a way to "kill" the app when a user exits with the back button?

Not really. You are better served fixing the bugs in your app.

usually after resuming the app after a long period of time

If "a long period of time" is less than 30 minutes or so, your process may have been terminated while your app was in the background, but Android will try to return the user to wherever the user had been in your app. This involves forking a fresh process for you and recreating your last activity. Sometimes, developers make assumptions that their process always starts with the launcher activity, with bugs being uncovered when the process starts with some other activity.

Also note that the HttpClient implementation in the Android SDK was deprecated in API Level 22 and removed in API Level 23. Either use an independent packaging of HttpClient or use some other HTTP client API (HttpURLConnection, OkHttp, etc.).

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • "Also note that the HttpClient implementation in the Android SDK was deprecated in API Level 22 and removed in API Level 23". Yes I'm aware of this, I use `useLibrary 'org.apache.http.legacy'` to ensure I can continue using the HttpClient. "Sometimes, developers make assumptions that their process always starts with the launcher activity" I don't believe I've fell foul of this but I shall start my debugging with this. Thanks. Lastly, when you say "not really" for having a way to kill an app is that because known methods are hacky or it's not possible? – Michael J Aug 10 '16 at 19:53
  • @MichaelJ: First, known methods transcend "hacky" and are more like "may cause harm". Second, it may not solve your problem, at least for all scenarios in which your problem may occur. "I use useLibrary 'org.apache.http.legacy'" -- that's not a good plan. There are *reasons* why Google got rid of the baked-in HttpClient, such as it being based on an eight-year-old beta edition of that library. `org.apache.http.legacy` is for legacy apps (hence, the name); new development should be using something else. – CommonsWare Aug 10 '16 at 19:59
  • Switching to 'HttpURLConnection' everywhere may fix this issue if the problem lies with the 'DefaultHttpClient'. I've been meaning to drop use of the 'DefaultHttpClient' so I'll start here. Without running the risk of going off topic is it possible to use 'HttpURLConnection' as a factory method and use a static call to it? The reason I ask is my app has many classes which make network requests and rewriting all requests using 'HttpURLConnection' and adding request headers etc is going to be a major undertaking. Thanks – Michael J Aug 10 '16 at 20:14
  • @MichaelJ: "is it possible to use 'HttpURLConnection' as a factory method and use a static call to it?" -- I am not quite certain what you mean by that. You can certainly create a static method to configure an `HttpURLConnection` instance if you like. There is no need for a `Context`, for example. – CommonsWare Aug 10 '16 at 20:15
  • @CommonsWare your answer is more than 2 years old, but I see that you are still very active in SO, so I decided to ask: Is there any reason not to call BackHandler.exitApp() when the back button is clicked? I would like to do that since the current situation with my app (that is using redux) is that after clicking the back button, some action is called and I am not sure why. If I exit the app then when the user clicks on its icon it will start clean. I understand that the drawback is that the app will not be seen in the 'recent apps' stack. – Yossi Oct 31 '19 at 09:53
  • 1
    @Yossi: I do not know what "BackHandler.exitApp()" is and therefore cannot comment on it. – CommonsWare Oct 31 '19 at 11:33
1

when you press back button then onBackpress() calls and app exit by normal actitvy life cycle and you can override onBackpress() (such that some apps give message that press again to exit) and do anything programmatic such that save any data or clear shared preference etc and when you clear it recent list then OS kills the app ,free the resources and there is no guaranty that activity life cycle proceed correctly such that some times onDestroy() not call. so basicly back press is part of our app and application self close by finish() method on other hand clear recent list is part of OS which forcefully kills the app

https://developer.android.com/guide/components/tasks-and-back-stack.html

follow this offical android link for more details

Mayank Sharma
  • 2,735
  • 21
  • 26
0

Is there a way to "kill" the app when a user exits with the back button?

For the sake of curiosity, there is. its System.exit(0). What will happen if you execute this? The VM stops further execution and program(Activity) will be exit/killed right away.

NEVER DO THIS! There is a good chance your app will loose some data if its doing something while you trigger System.exit(0).

fluffyBatman
  • 6,524
  • 3
  • 24
  • 25