SimonSays
SimonSays

Reputation: 10977

What is the best way to store a non serializable object instance in android?

i have a class that inherits from BroadcastReceiver and is bound to listen for PHONE_STATE events. inside of the onReceive method, i need an object instance that has to be always the exact same (at least between the state ringing and the next occurrence of ide / offhook). that means i need to store the object somewhere. it can not be serialized nor anyhow be stored in a database or in the SharedPreferences.

i thought about 2 different approaches:

  1. using a static variable. downside: no one knows at which point android is going to delete it.
  2. using a service. downside: the service needs to be started at the first call and then bound. this is an async call and i might have to wait for an uncertain time. also it seems kinda wrong to use a service just to store one single object.

any other, better ideas?

Upvotes: 1

Views: 814

Answers (2)

Emmanuel Devaux
Emmanuel Devaux

Reputation: 3407

third 3) Use a singleton instance of a custom class, then you may get variable from call to call , but not persistant (if application stop).. But useful from a time to another time in the runtime application life. To avoid as much as possible to have wiped data by android framework you may tie your singleton to a service that is "foreground" see http://developer.android.com/reference/android/app/Service.html#startForeground(int,%20android.app.Notification) by this way you get a higher memory detruction protection .. That is the way I currently use singleton in service.. made long time execution (~2 weeks with normal and heavy load activity) without any trouble ...



here an singleton example

class MyData {
   private static MyData mMydata= null; // unique reference ( singleton objet container)

   private MyObject myobject = null; // inside the unique object container we have the unique working object to be use  by the application
   // can't make instance from outside... we want to have single instance
   // we want that outside use method "getInstance" to be able to use the object
   private MyData() {
   }

   // retrieve and/or create new unique instance
   public static MyData getInstance() {
     if (mMydata ==  null) mMyData = new MyData();
     return   mMyData;
   }

   // Works with your memory stored object
   // get...
   public myObject getMyObject() {
   return myobject;
   }
   // set ...
   public void setMyObject(MyObject obj) {
    myobject = obj;
   }
}


in your application to handle your "working" object your may access it like

// get object
MyObject obj =  MyData.getInstance().getMyObject();
// or set a object
MyData.getInstance().setMyObject(obj);

Upvotes: 0

Egor
Egor

Reputation: 40228

Don't know if it will work in your situation, but I'm usually storing an object's string representation in SharedPreferences. You can override the toString() method, which will create the string representation, and implement a parse() method that will parse the saved string and initialize an object based on its saved state. Hope this helps.

Upvotes: 1

Related Questions