I have an app that uses a DTO class MyAppData
with 21 attributes to be
- transfered via Intent via Parcable between activities,
- saved/restored via Bundle in case of rotation and
- persisted via SharedPreferences(.Editor) if the app is restarted later
I had to write a lot of boiler-code to implement this functionality.
My Question: Is there a simpler way with less boiler-code to achive my goal?
Why this boiler-code?
- Implementing interface
Parcelable
is neccessary to transfer as Intent between activities. - SharedPreferences(.Editor) to survice app-restart: i found no way to load/save Parcable therefore i had to write extra code for it
- Fourtunately Bundle can load/save
Parcelable
The boiler-code looks like this
public class MyAppData implements Parcelable {
/****** here is the data that i want to 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 a lot of boiler-code necessary to handle the data ******************/
/********** code needed to 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 to 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 is the code that uses MyAppData and its boiler-code
MyAppData mMyData;
// to survive recreate on rotation
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
this.mMyData.saveInstanceState(this, savedInstanceState);
super.onSaveInstanceState(savedInstanceState);
}
// to 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);
}
// to pass to some other activity
private void openSomeActivity() {
final Intent intent = new Intent().setClass(context,
SomeActivity.class);
intent.putExtra(EXTRA_FILTER, filter);
context.startActivityForResult(intent, 0815);
}
// to receive from some 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;
}
}