简体   繁体   English

在Android中使用sharedpreferences传递Java对象

[英]Passing a Java object using sharedpreferences in Android

我可以使用Android的sharedpreferences在不同活动之间传递Java对象吗?

"Bundles" are probably the best way to go: “捆绑”可能是最好的选择:

Intent and Bundle are two classes to transfer object's from one activity to another activity. Intent和Bundle是用于将对象的一个​​活动转移到另一个活动的两个类。 You can create bundle object and put them into intent 您可以创建捆绑对象并将其放入意图

If you want to pass an "object" in your Bundle, then the object must implement "Parcelable": 如果要在捆绑包中传递“对象”,则该对象必须实现“ Parcelable”:

Yet another alternative is to use global state: 另一种选择是使用全局状态:

No. For that purpose I suggest creating an implementation of the Application class as a singleton and keeping a reference to your object there. 否。为此,我建议将Application类的实现创建为单例并在那里保留对您对象的引用。

Edit after reading comments: Keep in mind though that a singleton has many drawbacks and should be used only if the other solutions are insufficient. 阅读评论后进行编辑:请记住,尽管单例有很多缺点,并且仅在其他解决方案不足的情况下才应使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM