views:

274

answers:

1

Hi

I have an application App1 which defines class A and uses instances of this class. What I want to achieve is - after App1 is installed on the device to be able to load App2 which defines and implements class B which is subclass of class A (imported from App1 package); and be able to get an instance of class B in the App1. Class B doesn't add any new interfaces, so using it as class A is ok. But I am not sure how to pass the instance of class B to App1 (preferrably without any user interaction).

Can anyone advise if this is feasible?

+1  A: 

Taken literally, it is impossible. App1 and App2 will be running on different virtual machines, probably in different processes. There is no way to transport an object between them, any more than you can transport a Java object between an applet and a JavaEE server.

Your options:

  1. Use remote services and AIDL to implement a remote procedure call, effectively giving you "pass by reference" between apps
  2. Use Parceable and Intent extras, effectively giving you "pass by value" between apps
CommonsWare
Thank you for your reply! Very helpful. However one question for each of the optionI need my App1 to have an activity, run and interact with the user. And App2 (if possible) needs to be completely transparent for the user. So App1 needs to query which App2 is installed in the system and request it to provide an instance of class B. So.1. (AIDL) If App1 is activity + service and App2 is for example a BroadcastReceiver. How should App2 access the service? And if App2 is also an Activity, how can I make sure App1's service will continue to run when activity of App2 is started on top of App1?
Dennis K
2. (Parcelable) if App1 and App2 are both activities - everything is clear. But if again I want App2 to be a BroadcastReceiver, how do I get the result Intent from it? Should I also create a BroadcastReceiver in App1?Additional question: apart from marshaling the objects between processes, is it possible to instantiate class of App2 from App1 (which effectively will put them into one process) and then directly request for instance of class B. had to break it in two comments
Dennis K
You really need to ask these sorts of followups as fresh questions. A `BroadcastReceiver` has limited ability to interact with Services -- it can call `startService()` and pass data via the supplied `Intent`, but that's it. AIDL is not an option, and a `BroadcastReceiver` cannot receive a result, period. I don't know what business problem you are trying to solve, but IMHO your proposed setup screams "over-architecture".
CommonsWare
Thanks again! Business case is - being able to create and publish an App and have 3rd parties to be able to develop "plug-ins", which act as subcomponents for the App. These plug-ins could be downloaded and installed separately and connected to the main app.May be this can be achieved some other way, but I think it's more or less doable with the approaches you proposed. At least as: a) App1 starts service, sends broadcasat to plugings, plugin(s) start same service with arguments and return class B. Or b) App1 sends broadcast, plugin sends back a broadcast with B. Parcelable in both cases.
Dennis K