2

I have an ArrayList filled with some custom POJO's that I'd like to persist when the user switches screen orientation (onSaveInstanceState) and when e.g. the user hits the back button (onPause).

As far as I know, the SharedPreferences can only hold primitive data types and bundles can not hold references to generic ArrayLists.

Any tips on how to tackle this?

Regards,

Marcus

Marcus
  • 6,697
  • 11
  • 46
  • 89
  • Hitting back will not only pause, but also destroy your `Activity`. With that in mind, you'll need to write the data to some sort of file if you want it to survive. Were you instead just referring to instances where the user might be interrupted in their use of your app? – NasaGeek Sep 25 '14 at 09:48
  • Answer = http://stackoverflow.com/questions/7145606/how-android-sharedpreferences-save-store-object + http://stackoverflow.com/questions/7361627/how-can-write-code-to-make-sharedpreferences-for-array-in-android/7361989#7361989 – Piyush Kukadiya Sep 25 '14 at 10:12
  • @PiyushKukadiya Gson is deprecated – Marcus Sep 25 '14 at 10:15

3 Answers3

1

i don't know this is correct method or not but i handle this like this this always success while your app lost all cache data itself then also u can get back serializable object->

for generic ArrayLists always use serializable

just look at once http://developer.android.com/reference/java/io/Serializable.html

Bhanu Sharma
  • 5,135
  • 2
  • 24
  • 49
1

1- create a class and put everything you want to store for example arraylist of your POJO and make that class implement Serializable interface.

class MyBundle  implements Serializable {

   ArrayList<POJO> mPOJO;


   MyBundle( ArrayList<POJO> pojo){

    mPOJO= pojo;


   }

}

2- write it to file:

 ObjectOutputStream oos = null;
 FileOutputStream fout = null;
 try{
        FileOutputStream fout = new FileOutputStream("Your file path");
        ObjectOutputStream oos = new ObjectOutputStream(fout);
        oos.writeObject(mb); // mb is an instance of MyBundle
 } catch (Exception ex) {
        e.printStackTrace();
 }finally {
   if(oos  != null){
     oos.close();
   } 
 }

and to get back everything:

 ObjectInputStream objectinputstream = null;
 try {
        streamIn = new FileInputStream("Your file address");
        objectinputstream = new ObjectInputStream(streamIn);
        MyBundle mb = (MyBundle) objectinputstream.readObject();

   } catch (Exception e) {
        e.printStackTrace();
   }finally {
     if(objectinputstream != null){
        objectinputstream .close();
     } 
   }
mmlooloo
  • 18,937
  • 5
  • 45
  • 64
0

Try converting the List to Json using Gson or Jackson. Store the string in the Shared preference. some thing like below code

String listString = gsonSD.toJson(list<object> instance);
SharedPreferences storeDataPref = getContext().getSharedPreferences("list_store_pref", Context.MODE_PRIVATE);
Editor storeDataEditor = storeDataPref.edit();
storeDataEditor.putString("liststringdata", listString).apply();
Harsha Vardhan
  • 3,324
  • 2
  • 17
  • 22