4

I am developing a project in that i am creating one static class's object . So when i switch off device at that time all the static objects are cleared so i want to stored into shared preference . So i can use it again . Is it possible to save it in shared preference? Or any other possibility to use that object after restart device ?

Prince John Wesley
  • 62,492
  • 12
  • 87
  • 94
Chirag
  • 56,621
  • 29
  • 151
  • 198

4 Answers4

4

You can only save primitive data types like float, int, string, set of strings or booleans. You can serialize your objects in byte stream and then reacreate them from the byte stream, you can have a look at this Answer also this Answer

Community
  • 1
  • 1
Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
  • As Lalit told,you can use serialize object.Here is the link that may help you.http://stackoverflow.com/questions/1243181/how-to-store-object-in-sqlite-database – Hitendra Oct 24 '11 at 09:54
2

You can't save a specific type object in SharedPreferences. According to the SharedPreferences.Editor interface you can only save primitive data types like: floats, ints, strings, sets of strings or booleans.

MobileCushion
  • 7,065
  • 7
  • 42
  • 62
1

You can certainly store/restore the state of a Static class SharedPreferences (vis serialization) BUT you'll need an Application/Activity context to do that which means you're effectively tying a Static Class to an instance of a non-static (Application/Activity) Class.

That's a little odd in design terms - your Static class would suddenly make more sense being created(instanced), loaded (restored) and saved(stored) by your Activity directly?

Otherwise there exists the possibility of accessing it when it's state is nonsense - such is the nature of the Static Object?

-1

You ca not directly stored class object in preference (Except inbuilt String, Boolean,integer,etc...)

But You can stored the variables from your static class in preference.

For example

When your device get switched off you have to copy all the values of static class into preference

and at the time of boot complete you can retrieve all the values from preference to your static object or at the time of re-creation of static object you can fetch the values from preferences to your static object.

Boot complete receiver

Shut down receiver

Community
  • 1
  • 1
Dharmendra
  • 33,296
  • 22
  • 86
  • 129