【问题标题】:DTO-s with less boiler-code for Intent/Parcable/Bundle/SharedPreferences possible?用于 Intent/Parcelable/Bundle/SharedPreferences 的锅炉代码较少的 DTO-s 可能吗?
【发布时间】:2015-07-13 09:41:48
【问题描述】:

我有一个应用程序使用 DTO 类 MyAppData 具有 21 个属性

  • 在活动之间通过 Intent 通过 Parcable 传输,
  • 在轮换和
  • 的情况下通过 Bundle 保存/恢复
  • 如果稍后重新启动应用程序,则通过 SharedPreferences(.Editor) 保留

我不得不编写很多锅炉代码来实现这个功能。

我的问题:有没有更简单的方法来实现我的目标?

为什么是这个锅炉代码?

  • 实现接口Parcelable是在Activity之间作为Intent传递的必要条件。
  • SharedPreferences(.Editor) 来管理应用程序重启:我发现无法加载/保存 Parcable,因此我不得不为其编写额外的代码
  • 第四个Bundle可以加载/保存Parcelable

锅炉代码如下所示

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

【问题讨论】:

    标签: android android-intent sharedpreferences dry parcelable


    【解决方案1】:

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

    这边

    • 在活动之间通过 Intent 传输,
    • 通过 Bundle 保存/恢复以防轮换和
    • 如果稍后重新启动应用程序,则通过 SharedPreferences(.Editor) 保留

    都是通过字符串处理的

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-01
      • 2020-08-09
      • 1970-01-01
      • 2018-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多