0

I'm trying to get the main activity for my app to finish (not remain an active application in background) while keeping the process alive for a service.

If the user presses the home button and leaves my app or another activity is launched from outside of my app, I want the activity to be closed and NOT listed as an active application.

It is important, however, that the process stays alive. I don't want my background service to die as well.

What's the best way to go about doing this?

bgroenks
  • 1,859
  • 5
  • 34
  • 63
  • Closing an app is popular topic, read this: [Quitting an application - is that frowned upon?](http://stackoverflow.com/q/2033914/1267661). In short, it is not essential to close an app yourself. – Sam Jul 30 '12 at 21:32
  • "I want the activity to be closed and NOT listed as an active application. It is important, however, that the process stays alive. I don't want my background service to die as well." -- Those are mutually exclusive concepts. Either your app is running, or it is not running. It cannot simultaneously be both running and not running, outside of applications of Schrödinger's cat scenarios. Anything that reports processes running will report your running process. – CommonsWare Jul 30 '12 at 21:39
  • There is a difference between a running process and an "active" application as listed in the task manager. – bgroenks Jul 31 '12 at 00:51

2 Answers2

0

You should not forcibly close the application as the system does well in handling this itself. Instead you should call finish() to signal that the app is done and can be disposed of(your service will continue running).

David Esteves
  • 1,614
  • 2
  • 15
  • 22
0

By default Services don't have a UI. Once started they run until they crash or are killed . The user can close your app and launch a new one and the Service will persist.

Activities on the other hand are only running when they are visible. When the user navigates to another activity the previous activity is paused, stopped, or killed.

A simple way to do what you've briefly described above would be to create an Activity that starts a Service. That way when use navigates away form your Activity the Service will keep running in the background.

If you want your activity to die completely whenever a new Activity comes into view simply put a call to finish() in the onPause() or onStop() methods for your activity (which ever is more appropriate for your app).

slayton
  • 20,123
  • 10
  • 60
  • 89
  • The problem with this is that if the Activity launches another Activity, the onPause method finish() call will close the application. – bgroenks Jul 31 '12 at 00:52
  • No it won't close the application it will only close that `Activity`. Any other `Activities` or `Services` that you've already started will continue to run. Additionally where you place your call to `finish` depends on your specific needs. You need to figure out if its more appropriate in `onPause()` or `onStop()`. – slayton Jul 31 '12 at 03:49
  • Doing it in onStop throws an exception and doing it in onPause causes the application to exit if the activity launch is a child activity. – bgroenks Jul 31 '12 at 04:30