How to store nested Arraylists in a Bundle in Android

410 views Asked by At

I have this object in my Android App source code:

ArrayList<ArrayList<MyObject>> rsp

and i want to store it in a Bundle and use it in OnSaveInstanceState() method in my activity. Then i want to retrieve it from the bundle, too.

Should i use json? Is there another way?

2

There are 2 answers

0
Zakir hussain On BEST ANSWER

you can use Parcelable to store the data in Bundle during OnSaveInstanceState()

use the below link for more details :

How to save custom ArrayList on Android screen rotate?

0
Mayur Raval On

If possible use Gson Library

 Gson gson = new Gson();
    String output = gson.toJson(rsp); // Create String and pass through bundle

Retrive that list from String

    //convert from string
String output =  // get that string from bundle
    ArrayList<ArrayList<MyObject>> fromString = gson.fromJson(output,new TypeToken<List<ArrayList<ArrayList<MyObject>>>>(){}.getType());