Search code examples
androidservicebindonserviceconnected

Is onServiceConnected only called after Service onCreate?


In my current Android Project, I am starting a Service via startService(), then afterwards I am binding to the Service with bindService(). (I do this because I want to have a started Service I am able to communicate with, nevermind)

After the Context is bound to the Service, usually onServiceConnected() of my ServiceConnection (wich I created earlier) is called if i understood this right.

Can I assume, that onServiceConnected() is only called after all of my code in the onCreate of my Service is executed?


Solution

  • Can I assume, that onServiceConnected() is only called after all of my code in the onCreate of my Service is executed?

    Yes you can.

    According to the service lifecycle diagram onBind() is not called until after onCreate() has completed.

    The documentation states that the system calls onServiceConnected() to deliver the IBinder returned by the service's onBind() method.

    Therefore onCreate() is completed before onServiceConnected() is called since it is dependent on the return value of onBind() which occurs after onCreate(). You can also see this in the diagram where it says "Clients are bound to service".