开发者

readResolve with serialisable doesn't seem to be working

开发者 https://www.devze.com 2023-03-10 06:00 出处:网络
I have a singleton class which I am saving and restoring: private void saveData(){ FileOutputStream saveFile;

I have a singleton class which I am saving and restoring:

private void saveData(){
    FileOutputStream saveFile;
    saveFile = this.openFileOutput(STATE_SAVE_FILE_NAME, Context.MODE_PRIVATE);
    ObjectOutput output = new ObjectOutputStream(saveFile);
    output.writeObject(deck); 
}


private void restoreData(){
    FileInputStream restoreFile;

    restoreFile = this.openFileInput(STATE_SAVE_FILE_NAME);

    ObjectInput input = new ObjectInputStream(restoreFile);
    deck = (Deck) input.readObject();
}

The object deck is a singleton so I have a readRestore method defined in it:

private Object readResolve() { 
    return deck; 
}

When I save there is definitely data but the restore gives me noth开发者_JS百科ing. Am I missing something?

The save definitely works because I am able to save and restore other objects. Only the singleton is failing.


That readResolve() method causes the deserialized object to be completely thrown away and the current value of 'deck' to be returned by readObject() instead. If that's not your intention perhaps it should update 'deck' with data from the deserialized object, which is 'this' when in the readResolve() method.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号