我如何preserve整个活动将重新启动一个复杂的对象? [英] How do I preserve a complex object across Activity restarts?

查看:87
本文介绍了我如何preserve整个活动将重新启动一个复杂的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个Java Bean对象,它是序列化的。我想它存储起来安全,当一个活动经过的onDestroy()的的目的的(即的onSaveInstanceState()方法的没有的称呼)。

我要寻找不涉及建立一个数据库,并写入对象的一个​​方法(主要因为)Android的DB API是可怕和b),因为数据库进行应用程序更新的噩梦,因为没有像样的支持申请迁移)。

我想过序列化对象,以一个ByteArrayOutputStream,BASE64 EN $ C C是$,并将其写入共享preferences文件作为一个字符串。或者是太遥远?

更新

也许这连载到字符串想法并不坏毕竟,似乎摸出pretty的好。这是我现在做的:

 公共静态字符串的ObjectToString(序列化对象){
    ByteArrayOutputStream OUT =新ByteArrayOutputStream();
    尝试 {
        新的ObjectOutputStream(出).writeObject(对象);
        byte []的数据= out.toByteArray();
        out.close();

        OUT =新ByteArrayOutputStream();
        Base64OutputStream B64 =新Base64OutputStream(出);
        b64.write(数据);
        b64.close();
        out.close();

        返回新的String(out.toByteArray());
    }赶上(IOException异常E){
        e.printStackTrace();
    }
    返回null;
}

公共静态对象stringToObject(字符串连接codedObject){
    尝试 {
        返回新的ObjectInputStream(新Base64InputStream(
                新ByteArrayInputStream的(EN codedObject.getBytes())))的readObject()。
    }赶上(例外五){
        e.printStackTrace();
    }
    返回null;
}
 

在的onDestroy(),然后我可以简单的写的Base64的字符串到preference文件,它是安全的,直到我在接下来的活动推出看了一遍。这是一个很多比我预想的更快,除非你的bean进行大量的数据,它的工作原理pretty的好。更妙的是,你没有保持一个数据库架构。

不过,我很好奇别人是怎么做到这一点。

解决方案
  

我要寻找它并不办法   涉及创建一个数据库,并编写   对象到(主要是由于)   Android的DB API是可怕和b)   因为数据库提出申请   更新一个噩梦,因为有   申请没有像样的支持   迁移)。

Android的API其实是相当合理的,主要是因为它是一个瘦包装在SQLite的API和SQLite的API是一个嵌入式数据库还算合理。此外,Android的确实提供了援助,对应用程序的升级模式升级,通过 SQLiteOpenHelper

  

这是一个很大比我预想的更快,   除非你的豆类携带大量   数据,它的工作原理pretty的好。

我听见了许多人跑动距离系列化尖叫更多的开发人员比我听说过有长期的成功与它的人。在刚刚过去的几天里,这里SO #android,我曾与人拼命的根部撕裂系列化他的应用程序的交换。

  

更妙的是,你没有保持一个数据库架构。

哦,是你做的。你觉得是怎么回事,当你更新你的应用程序,你的类被修改的情况发生?这样做的簿记找出如何反序列化旧版本的类从一个类的新版本是件苦差事,是原因开发商放弃序列化。另外,不要忘记,序列化是不是事务性的,而SQLite是。

Say I have a Java Bean object which is serializable. I want to store it away safely when an Activity goes through onDestroy() on purpose (i.e. onSaveInstanceState() is not called).

I am looking for a way which doesn't involve creating a database and write the object to that (mostly since a) Android's DB API is horrible and b) since databases make application updates a nightmare, because there is no decent support for applying migrations).

I thought about serializing the object to a ByteArrayOutputStream, base64 encode that and write it to a SharedPreferences file as a string. Or is that too far off?

UPDATE

Maybe that serialize-to-string idea wasn't that bad after all, seems to work out pretty well. Here's what I'm doing now:

    public static String objectToString(Serializable object) {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        new ObjectOutputStream(out).writeObject(object);
        byte[] data = out.toByteArray();
        out.close();

        out = new ByteArrayOutputStream();
        Base64OutputStream b64 = new Base64OutputStream(out);
        b64.write(data);
        b64.close();
        out.close();

        return new String(out.toByteArray());
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

public static Object stringToObject(String encodedObject) {
    try {
        return new ObjectInputStream(new Base64InputStream(
                new ByteArrayInputStream(encodedObject.getBytes()))).readObject();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

in onDestroy() I can then simply write the Base64 string to a preference file, where it's safe until I read it again during the next activity launch. It's a lot faster than I expected and unless your beans carry huge amounts of data, it works pretty well. And even better, you don't have to maintain a DB schema.

Still, I'm curious about how others do this.

解决方案

I am looking for a way which doesn't involve creating a database and write the object to that (mostly since a) Android's DB API is horrible and b) since databases make application updates a nightmare, because there is no decent support for applying migrations).

Android's API is actually fairly reasonable, mostly because it's a thin wrapper over the SQLite API, and the SQLite API is fairly reasonable for an embedded database. Moreover, Android does provide assistance for schema upgrades on app upgrades, via SQLiteOpenHelper.

It's a lot faster than I expected and unless your beans carry huge amounts of data, it works pretty well.

I have heard of many more developers running away screaming from serialization than I have heard of people having long term success with it. Just within the past few days, here on SO #android, I had an exchange with somebody trying desperately to rip serialization out of his app by the roots.

And even better, you don't have to maintain a DB schema.

Oh yes you do. What do you think is going to happen when you update your application and your class is modified? Doing the bookkeeping to figure out how to deserialize old versions of the class from a new version of a class is a chore and is one of the reasons developers abandon serialization. Also, do not forget that serialization is not transactional, whereas SQLite is.

这篇关于我如何preserve整个活动将重新启动一个复杂的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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