Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will onCreate always be called before onBind? (For a service that isn't already running)

If I try binding to a Service using Context.bindService(), and that Service isn't already running/started, will I be guaranteed that the onCreate of that Service will definitely be called before onBind? I wasn't able to find any docs on the lifecycle of a Service, at least with respect to this question of which one of these two comes first.

If not, in which cases will onCreate be called before onBind? In the tests I've done, onCreate always seems to be called before onBind. But I need to be sure of this because I have some logic that could possibly fail if this order changes

In case it helps, I'm binding to this service to get a Messenger that I will be sending messages to. So practically, my question is "can I be assured that onCreate will execute before any of my messages get handled by the service?"

like image 425
George V.M. Avatar asked May 11 '18 12:05

George V.M.


1 Answers

The onCreate will be called first. It will not be called if the Service is already running.

enter image description here

Service overview docs

Bounded Service overview

like image 57
Murat Karagöz Avatar answered Nov 15 '22 03:11

Murat Karagöz