joshlf
joshlf

Reputation: 23597

Pass data references in Android

When passing data around in Android (between Activities, for example), it seems that the primary accepted method is to use Bundles and Parcelables. The problem with this approach is that it serializes all the data, which is inefficient. I'm wondering - is there way to pass around references rather than the data itself for efficiency?

Upvotes: 0

Views: 76

Answers (4)

Hades
Hades

Reputation: 3936

I would say your best approach would be to make all data model objects parceleble, and make all activities extend a base class and use a wrapper or a helper method to pass your data model objects between activities.

I use this approach in all my applications and thus far it has been very helpful and minimizes the number of references used between activities and fragments.

Upvotes: 1

user3151973
user3151973

Reputation: 86

I like to use the singleton design pattern. http://en.wikipedia.org/wiki/Singleton_pattern

Upvotes: 0

Szymon
Szymon

Reputation: 43023

This may be less efficient (though should not be a big impact in most cases) but avoids keeping a references to object between activities. That helps to keep the architecture cleaner and with less prone to memory leaks.

You can use Application class, static helper classes, singletons or services in some cases to help you but make sure you know what you're doing with them.

Upvotes: 1

tf.alves
tf.alves

Reputation: 917

There are a number of techniques you can employ, it all depends on what you are trying to achieve, for example you can use a singleton to keep a reference alive, or you may (although you really shouldn't) use a static reference. In the end it all comes down to what kind of data are you trying to pass around. Remember that sometimes is better to rebuild an object, than it is to keep it alive until (if it ever happens) the garbage collector clears it.

Upvotes: 0

Related Questions