25

我竭尽全力将我的 Android 游戏的所有数据都放入一个 savedInstanceState Bundle。总共有很多数据,包括许多 Parcelable 对象。这可确保当应用程序暂停或方向更改时,重新创建的 Activity 不会丢失任何数据。

但是,我最近才发现 savedInstanceState 包显然不适合长期存储。所以我正在寻找一种方法,可以将现有的保存方法调整为长期解决方案,以便始终可以恢复游戏状态。

到目前为止,我听说过两种解决方案:

1) 使用 savedInstanceState 包进行方向更改,但也包含 SharedPrefs 以在应用程序需要完全关闭时使用。

这似乎会适得其反,因为它使用两种完全不同的方法来完成基本相同的事情。此外,由于我的 savedInstanceState Bundle 使用 Parcelable 对象,我必须为这些对象中的每一个提供另一种方法,以使它们能够写入 SharedPrefs。本质上是大量重复且难以管理的代码。

2) 序列化 savedInstanceState Bundle,直接写入文件。

我对此持开放态度,但我实际上不知道如何去做。但是,我仍然希望可能有更好的解决方案,因为我听说 Android 中的序列化是“可笑/无法使用的缓慢”。

如果有人可以为我提供解决方案,我将不胜感激。

4

3 回答 3

17

有趣的是,本周,Android Weekly第 47 期发布了这个库: android complex preferences

它应该适合你。

于 2012-12-01T15:44:05.813 回答
4

我现在想出了我自己的解决方案来解决这个问题,这是一种将 Bundles 保存到 SharedPreferences 的半自动方法。我说半自动是因为,虽然保存 Bundle 只需要一种方法,但再次检索数据并将其转回 Bundle 需要一些工作。

这是保存Bundle的代码:

SharedPreferences save = getSharedPreferences(SAVE, MODE_PRIVATE);
Editor ed = save.edit();
saveBundle(ed, "", gameState);

/**
 * Manually save a Bundle object to SharedPreferences.
 * @param ed
 * @param header
 * @param gameState
 */
private void saveBundle(Editor ed, String header, Bundle gameState) {
    Set<String> keySet = gameState.keySet();
    Iterator<String> it = keySet.iterator();

    while (it.hasNext()){
        key = it.next();
        o = gameState.get(key);
        if (o == null){
            ed.remove(header + key);
        } else if (o instanceof Integer){
            ed.putInt(header + key, (Integer) o);
        } else if (o instanceof Long){
            ed.putLong(header + key, (Long) o);
        } else if (o instanceof Boolean){
            ed.putBoolean(header + key, (Boolean) o);
        } else if (o instanceof CharSequence){
            ed.putString(header + key, ((CharSequence) o).toString());
        } else if (o instanceof Bundle){
            saveBundle(header + key, ((Bundle) o));
        }
    }

    ed.commit();
}

请注意,我只为我需要的类型编写了案例,但是如果您有还包含其他类型的 Bundle,这应该很容易适应。

此方法将递归保存存储在给定 Bundle 中的其他 Bundle 对象。但是,它不适用于 Parcelable 对象,因此我必须更改我的 Parcelable 对象以使它们将自己存储到 Bundle 中。由于 Parcels 和 Bundles 非常相似,这并不难。不幸的是,我认为捆绑包也可能比包裹稍慢。

然后,我在所有以前的 Parcelable 对象中编写了构造函数,以使它们能够从存储的 SharedPreferences 数据中重新捆绑自己。重建所需数据的密钥很容易。假设您有以下数据结构:

Bundle b {
    KEY_X -> int x;
    KEY_Y -> Bundle y {
                 KEY_Z -> int z;
             }
}

这些将按如下方式保存到 SharedPreferences:

KEY_X -> x
KEY_YKEY_Z -> z

它可能不是世界上最漂亮的方法,但它很有效,而且它比其他方法花费的代码少得多,因为现在我的 onSaveInstanceState 方法和我的 onPause 方法使用相同的技术。

于 2012-12-03T21:42:47.273 回答
2

我扩展了 Dan 的答案,增加了自动重新创建 Bundles 的功能,并使名称不太可能发生冲突。

private static final String SAVED_PREFS_BUNDLE_KEY_SEPARATOR = "§§";

/**
 * Save a Bundle object to SharedPreferences.
 *
 * NOTE: The editor must be writable, and this function does not commit.
 *
 * @param editor SharedPreferences Editor
 * @param key SharedPreferences key under which to store the bundle data. Note this key must
 *            not contain '§§' as it's used as a delimiter
 * @param preferences Bundled preferences
 */
public static void savePreferencesBundle(SharedPreferences.Editor editor, String key, Bundle preferences) {
    Set<String> keySet = preferences.keySet();
    Iterator<String> it = keySet.iterator();
    String prefKeyPrefix = key + SAVED_PREFS_BUNDLE_KEY_SEPARATOR;

    while (it.hasNext()){
        String bundleKey = it.next();
        Object o = preferences.get(bundleKey);
        if (o == null){
            editor.remove(prefKeyPrefix + bundleKey);
        } else if (o instanceof Integer){
            editor.putInt(prefKeyPrefix + bundleKey, (Integer) o);
        } else if (o instanceof Long){
            editor.putLong(prefKeyPrefix + bundleKey, (Long) o);
        } else if (o instanceof Boolean){
            editor.putBoolean(prefKeyPrefix + bundleKey, (Boolean) o);
        } else if (o instanceof CharSequence){
            editor.putString(prefKeyPrefix + bundleKey, ((CharSequence) o).toString());
        } else if (o instanceof Bundle){
            savePreferencesBundle(editor, prefKeyPrefix + bundleKey, ((Bundle) o));
        }
    }
}

/**
 * Load a Bundle object from SharedPreferences.
 * (that was previously stored using savePreferencesBundle())
 *
 * NOTE: The editor must be writable, and this function does not commit.
 *
 * @param sharedPreferences SharedPreferences
 * @param key SharedPreferences key under which to store the bundle data. Note this key must
 *            not contain '§§' as it's used as a delimiter
 *
 * @return bundle loaded from SharedPreferences
 */
public static Bundle loadPreferencesBundle(SharedPreferences sharedPreferences, String key) {
    Bundle bundle = new Bundle();
    Map<String, ?> all = sharedPreferences.getAll();
    Iterator<String> it = all.keySet().iterator();
    String prefKeyPrefix = key + SAVED_PREFS_BUNDLE_KEY_SEPARATOR;
    Set<String> subBundleKeys = new HashSet<String>();

    while (it.hasNext()) {

        String prefKey = it.next();

        if (prefKey.startsWith(prefKeyPrefix)) {
            String bundleKey = StringUtils.removeStart(prefKey, prefKeyPrefix);

            if (!bundleKey.contains(SAVED_PREFS_BUNDLE_KEY_SEPARATOR)) {

                Object o = all.get(prefKey);
                if (o == null) {
                    // Ignore null keys
                } else if (o instanceof Integer) {
                    bundle.putInt(bundleKey, (Integer) o);
                } else if (o instanceof Long) {
                    bundle.putLong(bundleKey, (Long) o);
                } else if (o instanceof Boolean) {
                    bundle.putBoolean(bundleKey, (Boolean) o);
                } else if (o instanceof CharSequence) {
                    bundle.putString(bundleKey, ((CharSequence) o).toString());
                }
            }
            else {
                // Key is for a sub bundle
                String subBundleKey = StringUtils.substringBefore(bundleKey, SAVED_PREFS_BUNDLE_KEY_SEPARATOR);
                subBundleKeys.add(subBundleKey);
            }
        }
        else {
            // Key is not related to this bundle.
        }
    }

    // Recursively process the sub-bundles
    for (String subBundleKey : subBundleKeys) {
        Bundle subBundle = loadPreferencesBundle(sharedPreferences, prefKeyPrefix + subBundleKey);
        bundle.putBundle(subBundleKey, subBundle);
    }


    return bundle;
}
于 2015-01-29T17:05:34.143 回答