2

Sorry for my poor English.

I have a HashMap<String, Parcelable> to save some InstanceStates of a RecyclerView,and I want to save the HashMap<String, Parcelable> into a file.

I use the ObjectOutputStream,it can write into a file,but when I use ObjectInputStream,it seems that it doesn't have read some useful data,because the state of the RecyclerView doesn't change to the state that has been saved.

Why this doesn't work?

How can I save and restore InstanceState of view by "key-value"???

I want to save InstanceState permanently.

Thank you for your help~

my code:

private HashMap <String, Parcelable> hmRecyclerViewState = new HashMap<String, Parcelable>();    
private Parcelable recyclerViewState;
ObjectOutputStream oos;
ObjectInputStream ois;

1.

    try {
        ois = new ObjectInputStream(new FileInputStream(stateFile));
        hmRecyclerViewState = (HashMap<String, Parcelable>)ois.readObject();
    } catch (StreamCorruptedException e) {
        e.printStackTrace();
    } catch (OptionalDataException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {  
        if (ois != null) {  
            try {  
                ois.close();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }
    }

2.

    try {
        oos = new ObjectOutputStream(new FileOutputStream(stateFile));
        if (hmRecyclerViewState != null)
            oos.writeObject(hmRecyclerViewState);
        oos.close();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {  
        try {  
            if (oos != null)  
                oos.close();  
        } catch (IOException ex) {  
            ex.printStackTrace();  
        }  
    }  

3.

public void saveRecyclerViewState(String tableName) {
    recyclerViewState = recyclerView.getLayoutManager().onSaveInstanceState();
    hmRecyclerViewState.put(tableName, recyclerViewState);
}

4.

public void restoreRecyclerViewState(String tableName) {
    if ( hmRecyclerViewState.get(tableName) != null) {
        recyclerViewState = hmRecyclerViewState.get(tableName);
        recyclerView.getLayoutManager().onRestoreInstanceState(recyclerViewState);
        recyclerViewState = null;
    }
}
DawnYu
  • 2,278
  • 1
  • 12
  • 14

2 Answers2

0

You can save Parcelable objects to the String or XML or JSON type. And then you can save them to files. By the way, the HashMap Object implements the Serializable, so you can save HashMap Object to file without any problems.

There are some informations about PARCELABLE VS. JAVA SERIALIZATION IN ANDROID APP DEVELOPMENT

Bruce Lan
  • 347
  • 2
  • 6
  • thanks,but I don't understand how to **save Parcelable objects to the String** ? I save instancestate in a Parcelable object.Can you show some code?Very thanks – DawnYu Sep 12 '15 at 02:57
0

If i followed your question than you can do something like.. Iterate over your map >> put into a String builder >> save it into file. In your case create a toString() method in your parcelable class that returns all fields like(id,name,address...) in a format like comma separated.

StringBuilder sb = new StringBuilder();
for (Map.Entry<String, Parcelable> entry : map.entrySet()) {
    String key = entry.getKey();
    Parcelable value = entry.getValue();
   // append it to String builder
    sb.append(key+":"value.toString()+"\n");
}
// write here to a file..

and when you want to restore read from file >> put it into Map >> restore it.

SRB Bans
  • 3,096
  • 1
  • 10
  • 21
  • When I restore it, how can I transfer the `value.toString()` to the `Parcelable` object ? Sorry, I am new. – DawnYu Sep 12 '15 at 04:57
  • you can set the values to object using setter methods of your bean class. – SRB Bans Sep 12 '15 at 05:00
  • the object is not customed by me, it is from `recyclerView.getLayoutManager().onSaveInstanceState()` which returns a `Parcelable` object, I open the source code, it is an Interface. I don't how to do it? – DawnYu Sep 12 '15 at 05:41