绑定未运行的Service时,onCreate是否必然先于onBind执行?
onCreate() being called before onBind() when binding to a stopped Service? Great question—this is exactly the kind of lifecycle detail that’s critical for reliable business logic, so it’s smart to verify this instead of just trusting test results.
Short Answer
Yes, you can fully rely on onCreate() executing before onBind() when you bind to a Service that isn’t already running via Context.bindService(). This is a guaranteed part of Android’s Service lifecycle behavior, even if it’s not explicitly called out in every section of the documentation.
Why This Is Guaranteed
When you call bindService() for a stopped Service, the Android system follows a strict sequence:
- The system first initializes the Service by calling
onCreate()—this runs once, even if multiple clients bind simultaneously. - Only after
onCreate()completes does the system callonBind()to return theIBinder(or the Binder associated with yourMessenger) to the binding client.
This order is fundamental to how Android manages Service initialization: the Service needs to be fully set up before it can provide any binding interface to clients.
Relevance to Your Messenger Use Case
Since you’re using a Messenger to send messages to the Service, this guarantee is especially important for your logic:
- You’ll initialize your
Messenger(and any associated Handler or processing logic) insideonCreate(). - The
onBind()method returns the Binder from this already-initializedMessenger. - By the time your client receives the Binder and sends its first message,
onCreate()has already finished running. There’s no scenario where the Service will process a message beforeonCreate()completes.
Are There Any Exceptions?
No—this lifecycle sequence holds true for all standard Service bindings initiated via Context.bindService(). Even if multiple clients bind at the same time, onCreate() runs once before any onBind() calls are dispatched.
Your test results (where onCreate() always ran first) aren’t a coincidence—this is how the system is designed to work. You can safely build your business logic around this order.
内容的提问来源于stack exchange,提问作者George V.M.

