Is it possible, and what is the best strategy, to transfer objects by reference from one action to another?

I have a simple Android application that uses an instance of a class by calling it DataManagerto control phase-domain access to domain classes. Initially, I developed it as a singleton, which could be obtained using static methods, but in the end I was annoyed by the messiness of my implementation and reorganized into what I thought was a simpler and more understandable idea.

Now the idea is that for each open file, one is created DataManagerthat processes both file I / O and the modification of the domain classes (for example, Book). When I start a new action, I pass this one instance as Serializableextra (I have not used Parcelableit yet, but I expect it to work with the basic concept), and then I will take a new operation DataManagerfrom Intentthe method onCreate().

However, a comparison of objects indicates that the object sent from one action is not identical (different links) to the object obtained from Bundlein the second Office. Reading on Bundle(on StackOverflow, etc.) Assumes that Bundles cannot do anything other than missing a value.

So, what could be the cleanest and safest strategy for transferring an object between actions? As i see i could

  • Forget about passing by reference and live with everyone Activitywho has their own DataManager. Return a new DataManagerone every time I close an activity so that the underlying activity can use it. (A simple solution, I think.)

  • Go back to using singleton DataManagerand use the static method to get it from everyone Activity. (Not wanting to use the singleton again.)

  • Extend the application to create a kind of global link to DataManager. (Again, not keen on the idea of ​​globals.)

Is this a fair resume? Is there any other useful strategy that I could use?

+5
5

. , , .

, .

+4

Java , , . .

0

- Parcellable, .., - Singleton .

0

DataManager Singleton, Service. xml (. ), , .

parcellable , . , -, . , .

0

. DataManger , .

, , , . , , ClassLoader . singleton , , , onDestroy DataManager .

.

As an Android method, you can make your DataManager ContentProvider . This will allow you to access your data from each activity without having a global link. I'm not sure how you will create a caching content provider that will remain in memory and will not be reinstalled too often.

0
source

All Articles