1

I have created a foreground service, which is working perfectly and performs tasks in the background, But whenever I call startForegroundService onCreate method is getting called.

As I know and according to documentation, Service is a kind of singleton class but still, multiple instances are getting created.

Does anyone have any idea How can I avoid multiple instances of service??

Amin Pinjari
  • 2,129
  • 3
  • 25
  • 53
  • not universal, but you may check that service is already [running by checking its own static instance or flag](https://stackoverflow.com/questions/600207/how-to-check-if-a-service-is-running-on-android) (don't follow most votes ans! check out whole topic and comments) – snachmsm Oct 29 '19 at 14:12
  • You are most likely mistaken about the number of `Services` that are actually alive. How many times is `onDestroy()` being called? Just because some objects created by your `Service` are alive, and tasks are being executed in the background, does not mean the OS has not destroyed your `Service`. You should explain the binding scheme you are using. Are you ever calling `stopService()` or `stopSelf()`? – greeble31 Oct 29 '19 at 15:21

1 Answers1

1

Service was getting destroyed but the handler whose instance was getting created repeatedly here. Issue solved by removing handler in onDestroy of Service @greeble31 Thanks for your hint

Amin Pinjari
  • 2,129
  • 3
  • 25
  • 53