'Delay between bindService() and onBind() when starting a Service

I'm facing a strange problem with my app. I have a LocationService that runs in the background. The way I manage its lifecycle is:

  1. In onResume, every activity uses bindService(intent,serviceConnection, BIND_AUTO_CREATE) like that the service is automatically created when needed.
  2. In onStop, every activity uses unbindService(serviceConnection)
  3. This works because when switching activities, the new Activity onResume is called before the old Activity onStop method

The problem I have is, lets say I start from the home screen and I launch the app with an Activity that has a fragment in it. The order of the function call is as follows

  1. Activity onCreate --> setContentView is called here
  2. Activity onResume --> here bindService is called and should create the Service
  3. Fragment onResume
  4. Service onBind method is called

My question is why is there a something else between my bindServiceand onBind calls?? I have a feeling this has something to do with threading issues.



Solution 1:[1]

In my case, my issue was using android:process attribute for <service> element within Android Manifest, which is supposed to improve performance, but in reallity, maybe it does once the service is running, but it takes a very long while to reach onCreate() (and so also to reach onBind()). For me it was taking minutes. Now Apps and services run smooth and as expected.

More info: https://developer.android.com/guide/topics/manifest/service-element

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1