Qwertie
Qwertie

Reputation: 17186

Proper inter-activity communication in Android?

I would like to share multi-megabyte data structures and other objects (that refer to said data structures) between Activities in a single process (Parcelable is out of the question; much of the data lives in C++ anyway). I want to not only send arbitrary Objects to new activities, but also return arbitrary result Objects.

But activities start each other and return results via Intents, and Intents seem to have no way to include arbitrary Objects. Currently I am using static variables as a workaround, but it leaves me concerned about memory leaks, and although only one instance of the app is intended to run (for now), I shudder to think about what would happen if two instances of the same Activity somehow get on the Activity stack.

So, SO community, what communication mechanism would you recommend instead?

P.S. does anyone know if Android ever spawns multiple copies of a process, e.g. when two apps independently start a third app?

Upvotes: 1

Views: 1349

Answers (2)

Cheesebaron
Cheesebaron

Reputation: 24460

Just like when communicating arbitrary objects between a web service and a client, you can serialize your objects and deserialize them on the other side, and pass these objects in the Intent bundles. You can do this using a XML or JSON serializer/deserializer.

EDIT: OK, after seeing CommonWares comment I did some digging on Intents and my first suggestion, might not be so good after all. Though it would work fine for smaller objects.

So there are a couple of alternatives I can think of.

  1. As you already do, use a static data member by using a custom Application object
  2. Serialize your data and store it on the disk
  3. Store it in a database
  4. Use a service
  5. Don't pass that big size of data between activities

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1006819

I would like to share multi-megabyte data structures and other objects (that refer to said data structures) between Activities in a single process

I would hope that what you really want is a centralized data model that all of your activities can reference.

I want to not only send arbitrary Objects to new activities, but also return arbitrary result Objects.

Again, I would hope that what you really want is a centralized data model that all of your activities can reference. Saying that you want to pass "multi-megabyte data structures" between activities is akin to saying you want to pass "multi-megabyte data structures" via query parameters on a URL in a Web app.

Currently I am using static variables as a workaround, but it leaves me concerned about memory leaks, and although only one instance of the app is intended to run (for now), I shudder to think about what would happen if two instances of the same Activity somehow get on the Activity stack.

Well, if you would actually have a centralized data model (whether via static data members or some other means), you will not have to worry about data copying. Moreover, every time you pass data via Intent extras you are making copies already.

So, SO community, what communication mechanism would you recommend instead?

Have a centralized data model and pass identifiers in Intent extras.

does anyone know if Android ever spawns multiple copies of a process, e.g. when two apps independently start a third app?

Each app, by default, runs in a single process, no matter who "starts" them or how many times they are "started".

Upvotes: 2

Related Questions