3

My Application has an Activity for the UI and a Service for background polling fun. Seems like standard fare.

  1. Can AlarmManager trigger the Service Intent without Activity OnCreate being called?

  2. Is there any benefit to putting the Activity & Service into different Applications? Would this create 2 apk's and make it impossible to put into Market as one app? Can you put 2 applications into one manifest somehow?

  3. Regarding communication between the two:

-If Activity & Service are part of the same Application - can't I just store common objects (like User object) at the Application scope for the 2 to share?

-It seems like I don't even need to bother with AIDL - the two could just have weak references to each other at the Application scope as well - and they can call methods on each other that way? Or should they pub/sub each other with some kind of Observer Pattern or BroadcastListener thing?

ajma
  • 12,106
  • 12
  • 71
  • 90
paulpooch
  • 651
  • 1
  • 7
  • 15

1 Answers1

3

Can AlarmManager trigger the Service Intent without Activity OnCreate being called?

Yes.

Is there any benefit to putting the Activity & Service into different Applications?

IMHO, no.

Would this create 2 apk's and make it impossible to put into Market as one app?

Yes.

Can you put 2 applications into one manifest somehow?

From a pure XML standpoint, there is room in the manifest for more than one <application> element. However, AFAIK, only one is supported.

If Activity & Service are part of the same Application - can't I just store common objects (like User object) at the Application scope for the 2 to share?

For very quick things, yes. However, bear in mind that your service may get shut down (by Android, by user, etc.), after which your process may get terminated, and your Application object goes poof. I'd use this for light caching only.

It seems like I don't even need to bother with AIDL

Correct -- that is only needed for inter-process service binding.

the two could just have weak references to each other at the Application scope as well

I wouldn't do that in a million years. Please use the platform responsibly. There are plenty of ways for activities and services to communicate yet remain loosely coupled (or, in the case of the local binding pattern, tightly-coupled in an Android-aware fashion).

Or should they pub/sub each other with some kind of Observer Pattern or BroadcastListener thing?

Something along those lines would be preferable. While the activity and the service may be co-resident in the same process at the same time, they are not designed to be directly linked to one another.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks again CommonsWare - I'm really making progress towards proper design. More follow ups on the way. – paulpooch Feb 05 '11 at 17:04
  • Question has led here: http://stackoverflow.com/questions/4908267/communicate-with-activity-from-service-localservice-android-best-practices – paulpooch Feb 05 '11 at 17:13