0

My application works as follows: There is an Application class (derived from android.app.Application class) for receiving data from the network and dispatch these data to the corresponding activity.

The problem is: There is a possibility that when activity A starts activity B, there maybe network data received by my Application object, and I want to send this data to activity B which may be not started yet, how could the app guarantee that activity B receive this data? Are there any asynchronous message passing mechanism or should I manually cache the message until activity B start?

realjin
  • 1,485
  • 1
  • 19
  • 38
  • cant you use broadcast receivers to send data?? – Raghav Jun 23 '12 at 06:30
  • 2
    @realjin : Firstly I wouldn't advise having an `Application` class monitor for network activity - use a `Service` of some sort. Secondly, as @iNan suggests, use `BroadcastReceivers` in your `Activities` and have the `Service` create a 'sticky' broadcast. – Squonk Jun 23 '12 at 06:34
  • @Squonk : Sorry, I haven't described my implementation fully --- Actually I use a service to receive network data, and the service will broadcast the received data to the Application class, then the Application class will call the current activity's callback method.(The mechanism is just the same as : http://stackoverflow.com/a/2622473/816109) – realjin Jun 23 '12 at 07:11
  • @Squonk Do you mean that the ‘sticky’ broadcasting mechanism will guarantee that the message be received after the activity is started? – realjin Jun 23 '12 at 07:13
  • @realjin : Yes, the message (`Intent`) will be received but not in a 'dynamic' sense. In your `Activities` have inner `BroadcastReceivers` and have your `Activities` do something like `Intent i = registerReceiver(...)` in their `onResume()` methods. If there is a 'sticky' broadcast for that receiver, then `i` will receive the `Intent`. Check first to see if `i` is `null` - in that case there is no 'sticky' broadcast. See this for `sendStickyBroadcast(...)` http://developer.android.com/reference/android/content/ContextWrapper.html#sendStickyBroadcast(android.content.Intent) – Squonk Jun 23 '12 at 07:30
  • @Squonk: Ok, Seems that I know how to do it, thanks! – realjin Jun 23 '12 at 07:53
  • @iNan Thanks iNan too, and sorry for my obscure description. – realjin Jun 23 '12 at 07:54

0 Answers0