Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 594
Dominik Seemayr Avatar asked Mar 31 '16 14:03

Dominik Seemayr


People also ask

How to bind activity to service?

To provide binding for a service, you must implement the onBind() callback method. This method returns an IBinder object that defines the programming interface that clients can use to interact with the service.

How to start bound service in Android?

There are three ways of creating bound services: First is by extending the binder class. Second is by using a messenger. Third, is by using AIDL or Android Interface Definition Language.

What is bindService() in Android?

stopService() or stopSelf() is called; however, services can use their stopSelf(int) method to ensure the service is not stopped until started intents have been processed. Clients can also use Context. bindService() to obtain a persistent connection to a service.


1 Answers

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".

like image 151
George Mulligan Avatar answered Sep 28 '22 07:09

George Mulligan