简体   繁体   中英

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. 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":

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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