2015-07-13 52 views
0

我有一个使用一个DTO类MyAppData 21个属性的应用程式是DTO-s可以使用Intent/Parcable/Bundle/SharedPreferences的较少的锅炉代码?

  • 经由Parcable经由意图转移活动之间,
  • 保存/通过捆绑在旋转的情况下,恢复和
  • 经由SharedPreferences持续(.Editor)如果应用程序稍后重新启动

我不得不写很多锅炉代码才能实现此功能。

我的问题:是否有一个更简单的方法,以较少的锅炉代码来实现我的目标?

为什么这个锅炉代码?

  • 实施界面Parcelable需要作为活动之间的意图转移。
  • SharedPreferences(。编辑)到站务应用程序重新启动:我发现没有办法加载/保存Parcable因此我不得不写额外的代码为它
  • Fourtunately包可以加载/保存Parcelable

锅炉-code看起来像这样

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 
      } 
     } 
    } 

这是一个使用MyAppData和锅炉码码

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; 
    } 
} 

回答

0

我已经通过实现dto的字符串表示法解决了我的问题:使用toString()parse()方法在dto和字符串之间进行转换。

这样

  • 经由活动之间意图转移,
  • 保存/通过捆绑在旋转的情况下,恢复和
  • 经由SharedPreferences(。编辑)持续如果应用程序稍后重启

全部通过字符串处理