android - DTO-s with less boiler-code for Intent/Parcable/Bundle/SharedPreferences possible? -
i have app uses dto class myappdata 21 attributes
- transfered via intent via parcable between activities,
- saved/restored via bundle in case of rotation ,
- persisted via sharedpreferences(.editor) if app restarted later
i had write lot of boiler-code implement functionality.
my question: there simpler way less boiler-code achive goal?
why boiler-code?
- implementing interface
parcelableneccessary transfer intent between activities. - sharedpreferences(.editor) survice app-restart: found no way load/save parcable therefore had write code
- fourtunately bundle can load/save
parcelable
the boiler-code looks this
public class myappdata implements parcelable { /****** here data want use ******************/ private string path = null; // ... 20 more attibutes public string getpath() {return path;} public void setpath(string path) {this.path = path;} // ... 20 more attibutes /****** here start lot of boiler-code necessary handle data ******************/ /********** code needed implement interface parcelable *************/ public static final parcelable.creator<myappdata> creator = new parcelable.creator<myappdata>() { public myappdata createfromparcel(parcel in) { return new myappdata(in); } public myappdata[] newarray(int size) { return new myappdata[size]; } }; public myappdata() {}; private myappdata(parcel in) { setpath(in.readstring()); // ... 20 more attibutes } @override public void writetoparcel(parcel dest, int flags) { dest.writestring(getpath()); // ... 20 more attibutes } @override public int describecontents() {return 0;} /************ code neccessary handle sharedpreferences(.editor) because sharedpreferences cannot handle parcable ********/ private static final string shared_key_path = "filter_path"; // ... 20 more attibutes public void savesettings(sharedpreferences.editor edit) { if (edit != null) { edit.putstring(shared_key_path, this.getpath()); // ... 20 more attibutes } } public void loadsettings(sharedpreferences sharedpref) { if (sharedpref != null) { this.setpath(sharedpref.getstring(shared_key_path, this.getpath())); // ... 20 more attibutes } } } this code uses myappdata , boiler-code
myappdata mmydata; // survive recreate on rotation @override public void onsaveinstancestate(bundle savedinstancestate) { this.mmydata.saveinstancestate(this, savedinstancestate); super.onsaveinstancestate(savedinstancestate); } // survive recreate on rotation or remember settings on last app start @override protected void oncreate(bundle savedinstancestate) { ... this.mmydata.loadsettingsandinstancestate(this, savedinstancestate); } @override protected void onpause () { ... this.mmydata.savesettings(this); } // pass other activity private void opensomeactivity() { final intent intent = new intent().setclass(context, someactivity.class); intent.putextra(extra_filter, filter); context.startactivityforresult(intent, 0815); } // receive other activity @override protected void onactivityresult(final int requestcode, final int resultcode, final intent intent) { super.onactivityresult(requestcode, resultcode, intent); switch (requestcode) { case 0815 : mydata = intent.getparcelableextra(extra_filter); break; } }
i have solved problem implementing string representation of dto: having tostring() , parse() method convert between dto , string.
this way
- transfered via intent between activities,
- saved/restored via bundle in case of rotation and
- persisted via sharedpreferences(.editor) if app restarted later
is handled through string
Comments
Post a Comment