Does Android guarantee order of intents?
When sending two intents (One through startService
followed by another through startActivityForResult
for the sake of an example), does Android guarantee that the first intent (service) is processed before the second (activity)? More specifically开发者_StackOverflow中文版 in this case, does it guarantee that the Service.onStartCommand
is called before the Activity.onCreate
?
This is assuming that everything runs within the same process so threading issues from several main threads don't interfere.
I know that Android doesn't guarantee the processing order for a single intent (The order of recipients isn't guaranteed but can be affected by the intent filter priority) but I couldn't find anything mentioning the order between intents.
Personally I would have thought that, given the single main application thread, the order would be maintained, but I'm facing some bugs that I have hard time explaining unless the order of intents changes.
This should help. BroadcastReceiver Developer Page
The key bit being that an intent which starts an activity is very different semantically.
It seems like it'd be quite easy to test order by logging.
- I do not believe that Android makes any kind of guarantee -
in particular, I believe that. Edit: After your comment, I believe that you are right in thatContext.startService
would be asynchronousstartService
will only return after the service has called itsonCreate
method. However, if theService
is in another application, then it will usually not run in the same process, let alone in the same thread. - Android probably does not make this guarantee because it has already stated that while it will try to keep services running, it could theoretically kill the service between your call to
Context.startService
and your call toActivity.startActivityForResult
. - Why would you need such a guarantee in the first place? Get the service by
startService
orbindService
when you need it, specify what should happen to your application when it hits its lifecycle phases, and let Android manage the lifecycle itself.
Footnote: Also, if your Activity
is already running, the onCreate
method may not be called when you call onActivityStart
.
精彩评论