保存捆绑到共享preferences [英] Save Bundle to SharedPreferences

查看:112
本文介绍了保存捆绑到共享preferences的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经竭尽全力,让所有的数据为我的Andr​​oid游戏中配入savedInstanceState捆绑。有很多数据,干脆,其中包括许多Parcelable对象。这可确保当应用程序被暂停或方向改变,没有任何数据丢失的活动正在重建。

I've gone to great lengths to make all of the data for my Android game fit into a savedInstanceState Bundle. There's a lot of data altogether, including many Parcelable objects. This ensures that when the app is paused or the orientation changes, no data gets lost by the Activity being recreated.

然而,我刚最近发现,一个savedInstanceState束是显然不适合长期储存。所以,我在找一个我能适应我现有的保存方法,作为一个长期的解决方案的工作,使游戏状态总是可以被恢复的方式。

However, I have just recently discovered that a savedInstanceState bundle is apparently NOT appropriate for long-term storage. So I'm looking for a way that I can adapt my existing save method to work as a long-term solution, so that the game state can always be restored.

我听说过2解决方案至今:

I have heard of 2 solutions so far:

1)使用savedInstanceState捆绑方向的变化,也将共享preFS应用程序需要被完全关闭时的。

这似乎令人难以置信的反效果,因为它使用两种不同的方法,完全做到基本相同的事情。此外,由于我的savedInstanceState捆绑使用Parcelable对象,我必须给每个这些对象的另一种方法,使他们能够被写入到共享preFS。基本上地段重复和难以管理的code。

This seems incredibly counter-productive, as it uses 2 different completely methods to do basically the same thing. Also, since my savedInstanceState Bundle uses Parcelable objects, I would have to give each of those objects another method to enable them to be written to SharedPrefs. Essentially LOTS of duplicated and difficult-to-manage code.

2),序列化savedInstanceState捆绑,并直接写入到一个文件中。

我愿意接受这一点,但我真的不知道该怎么去这样做。不过,我仍然持有到希望,有可能是一个更好的解决方案,因为我听说连载Android是滑稽/ unusably慢。

I am open to this, but I don't actually know how to go about doing it. However, I'm still holding onto the hope that there may be a better solution, as I've heard that serialization in Android is "comically / unusably slow".

我将非常感谢,如果有人可以提供我一个解决的办法。

I would be extremely grateful if someone could provide me with a solution to this.

推荐答案

我现在拿出自己的解决这个问题,这是节省捆绑到共享preferences的半自动方式。我说的半自动因为,虽然节省了包只需要一个方法,则再次检索数据,并把它放回一个包需要一些工作。

I have now come up with my own solution to this problem, which is a semi-automatic means of saving Bundles to SharedPreferences. I say semi-automatic because, although saving the Bundle requires only one method, retrieving the data again and turning it back into a Bundle takes some work.

下面是code保存套件:

Here is the code to save the 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();
}

请注意,我只写了情况为我所需要的类型,但如果你有捆绑还包括其他类型的,这应该是很容易适应。

Note that I have only written cases for the types I needed, but this should be easily adaptable if you have Bundles that also include other types.

此方法将递归地节省存储在给定的包内的其他包的对象。但是,它不会为Parcelable对象的工作,所以我不得不改变我的Parcelable对象,使他们自己保存成束来代替。由于包裹和捆绑是pretty的相似,这是不是太辛苦。我想捆绑也可以,但包裹稍微慢一些,很遗憾。

This method will recursively save other Bundle objects stored inside the given Bundle. However, it will not work for Parcelable objects, so I had to alter my Parcelable objects to make them store themselves into a Bundle instead. Since Parcels and Bundles are pretty similar, this wasn't too hard. I think Bundles may also be slightly slower than Parcels, unfortunately.

然后我写的构造函数在我所有的previously-Parcelable的对象,使他们能够从存储的共享preferences数据重新捆绑自己。这是很容易重建的钥匙给你需要的数据。假设你有以下数据结构:

I then wrote constructors in all of my previously-Parcelable objects to enable them to re-Bundle themselves from the data stored SharedPreferences. It's easy enough to reconstruct the keys to the data you need. Say you have the following data structure:

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

这些将被保存到共享preferences如下:

These will be saved to SharedPreferences as follows:

KEY_X -> x
KEY_YKEY_Z -> z

这可能不是世界上prettiest方法,但它的工作原理,它花了我少得多code比选择,因为现在我的onSaveInstanceState方法和我的onPause方法使用相同的技术。

It may not be the prettiest method in the world, but it works, and it cost me much less code than the alternative, since now my onSaveInstanceState method and my onPause methods use the same technique.

这篇关于保存捆绑到共享preferences的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆