开发者

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.


  1. I do not believe that Android makes any kind of guarantee - in particular, I believe that Context.startService would be asynchronous. Edit: After your comment, I believe that you are right in that startService will only return after the service has called its onCreate method. However, if the Service is in another application, then it will usually not run in the same process, let alone in the same thread.
  2. 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 to Activity.startActivityForResult.
  3. Why would you need such a guarantee in the first place? Get the service by startService or bindService 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜