1

I understand that when you call an Intent to start a service, the service continues until it is explicitly ended.

I would like the service to persist multiple activities. How do I end it when all activities of the application is destroyed?

Or does it get destroyed automatically?

mushroom
  • 1,909
  • 3
  • 16
  • 33

1 Answers1

2

In the main Activity, override onDestroy and call stopService

@Override
public void onDestroy()
{
    super.onDestroy();
    Toast.makeText(<Context>, "Stopping service", Toast.LENGTH_SHORT).show();
    Intent intent = new Intent(<MainActivity>.this, <YourService>.class);
    stopService(intent);
}
Green goblin
  • 9,898
  • 13
  • 71
  • 100
  • Would the main `Activity` be destroyed if it launches another activity (part of the same app) via an `Intent`? – mushroom May 12 '14 at 15:54
  • 1
    No. If you observe the `Activity` lifecycle, an activity is created and destroyed once. If you call `Activity` B from `Activity` A, the calling `Activity` is paused. – Green goblin May 12 '14 at 15:56
  • Why, then, does `onCreate()` get called again when I return from `Activity` B to `Activity` A? – mushroom May 12 '14 at 16:00
  • @Aashish The `Activity` will stay on `onPause()` for as long as it is visible (i.e. if `Activity` B is a themed as a `Dialog` for example), otherwise it will go to `onStop()` as it can be seen from [here](http://developer.android.com/training/basics/activity-lifecycle/starting.html#lifecycle-states) – Emmanuel May 12 '14 at 16:00
  • @Emmanuel, Thanks fro correcting me. The statement for `onDestroy` holds true. – Green goblin May 12 '14 at 16:02
  • @Emmanuel Thanks, I have seen that page too before coming here, hence my question. I know how to stop a service. I just don't know how to make it stop when the last app activity is destroyed. If I were to follow @Aashish's solution, the service would terminate when my app launches another activity via an `Intent`. The new activity relies on this service. – mushroom May 12 '14 at 16:05
  • @mushroom, my assumption is that, the last activity to be destroyed will be launcher activity. `Android` maintains a stack of activities. Did you try my solution, BTW? – Green goblin May 12 '14 at 16:10
  • @Aashish Yes, I did. The service got destroyed when my Main activity launched another activity. – mushroom May 12 '14 at 16:26
  • @Aashish what prevents you from using the method provided by this answer in the `onDestroy()` of your last `Activity`? – Emmanuel May 12 '14 at 17:02
  • @mushroom, can you try ` isFinishing` instead of `onDestroy`? http://developer.android.com/reference/android/app/Activity.html#isFinishing() – Green goblin May 12 '14 at 17:19
  • @Emmanuel `isFinishing()` also gets called when transitioning to another Activity within the app. – mushroom May 12 '14 at 17:38
  • It wasn't me who suggested that answer. – Emmanuel May 12 '14 at 17:43
  • @Aashish I may not have explained the situation as clear as it should be. So as an extension to this question, here's what I believe the underlying problem is: http://stackoverflow.com/questions/23615566/android-application-lifecycle. – mushroom May 12 '14 at 17:49