Jonathan Allen
Jonathan Allen

Reputation: 70307

MonoDroid: Sharing non-trivial data between Activities

I have some data that multiple Activities need to manipulate. Basically there is one read-only screen and multiple edit screens that hang off it.

At first I considered passing the data as a string parameter on the intent, but if the user presses the back button after editing a field those changes would be lost.

So what is the recomended way to share data across the different Activities? Should I just save it to a temp file before each page transition?

Upvotes: 2

Views: 522

Answers (2)

SohailAziz
SohailAziz

Reputation: 8034

Instead of serializing the object implements parcelable interface for the class you want to pass objects of. Parcelable is android specific concept and is recommended for best performance.

Upvotes: 1

Greg Shackles
Greg Shackles

Reputation: 10139

The easiest way to do this is probably to store the object in some globally available place, such as on the Application object for your app (creating one if there isn't one already, of course). This could either be static or instance, depending on how you want it to work.

Another option is to serialize the object down to some sort of string representation (XML, JSON, etc), pass that through to the next activity, and deserialize it on the other side. This would work, but is definitely a bit heavier.

Upvotes: 3

Related Questions