android - Saving Parcelable data -
i have class implements parcelable, , not implement serializable because contains basic android classes can not modify.
some of objects in class example location , pendingintent (which conveniently parcelable).
my problem saving information between instances of main activity.
currently, i'm holding static reference class, works well. assume when re-install app, , when updates come around, won't able trust static member won't re-initialized.
i tried write parcelable file, using marshall() not working (i'm getting binder can't marshalled error).
how can safely save information?
thanks
i use statecontrol class handle reading/writing disc:
public class statecontrol { context mcontext; thread worker; writeobjecttofile writer; // statecontrol constructor public statecontrol(context context) { mcontext = context; // construct writer hold , save data writer = new writeobjecttofile(); // construct worker thread handle writer worker = new thread(writer); }// end of statecontrol constructor // method save global data public void saveobjectdata(object object, string key) { if (object == null){ // had different action here } else { // write data disc writer.setparams(new writeparams(object, key)); worker.run(); } }// end of saveglobaldata method // method read global data public object readobjectdata(string key){ object returndata = (object) readobjectfromfile(key); if (returndata == null){ // had different action here } else { return returndata; } }// end of readglobaldata method // method erase global data public void clearobjectdata(string key){ writer.setparams(new writeparams(null, key)); worker.run(); }// end of clearglobaldata method private class writeobjecttofile implements runnable { writeparams params; public void setparams(writeparams params) { this.params = params; } public void run() { writeobjecttofile(params.getobject(), params.getfilename()); } private boolean writeobjecttofile(object object, string filename) { boolean success = true; objectoutputstream objectout = null; try { fileoutputstream fileout = mcontext.openfileoutput(filename, activity.mode_private); objectout = new objectoutputstream(fileout); objectout.writeobject(object); fileout.getfd().sync(); } catch (ioexception e) { success = false; e.printstacktrace(); } { if (objectout != null) { try { objectout.close(); } catch (ioexception e) { // nothing } }// end of if }// end of try/catch/finally block return success; } }// end of writeobjecttofile method private object readobjectfromfile(string filename) { objectinputstream objectin = null; object object = null; try { fileinputstream filein = mcontext.getapplicationcontext().openfileinput(filename); objectin = new objectinputstream(filein); object = objectin.readobject(); } catch (filenotfoundexception e) { // nothing } catch (ioexception e) { e.printstacktrace(); } catch (classnotfoundexception e) { e.printstacktrace(); } { if (objectin != null) { try { objectin.close(); } catch (ioexception e) { // nowt } } } return object; } private static class writeparams { object object; string filename; public writeparams(object object, string filename) { super(); this.object = object; this.filename = filename; } public object getobject() { return object; } public string getfilename() { return filename; } } }
then invoke public methods kick off writing/reading. version, having taking place in separate thread, modify if needed to.
Comments
Post a Comment