MonoDroid: Sharing non-trivial data between Activities

ぐ巨炮叔叔 提交于 2019-12-22 13:30:54

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/8763810/monodroid-sharing-non-trivial-data-between-activities

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!