theblitz
theblitz

Reputation: 6881

readResolve with serialisable doesn't seem to be working

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 nothing. Am I missing something?

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

Upvotes: 0

Views: 324

Answers (1)

user207421
user207421

Reputation: 310957

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.

Upvotes: 1

Related Questions