Saving state of ArrayList of custom objects

前端 未结 2 561
清歌不尽
清歌不尽 2020-12-18 16:33

I have a member variable in my Activity which is an ArrayList. The objects populating this ArrayList are objects I have defined called RatingItem. RatingItem has several m

2条回答
  •  清歌不尽
    2020-12-18 16:51

    You should probably have your object implement Parcelable. Basically, you have to add methods to write and read the members of your object from a Parcel, and then add custom CREATOR inner class that has a couple of static factory methods. There's a demo of it on the first linked page, but it's not too hard.

    Then you can just save the whole list in one go with Bundle.putParcelableArrayList(). (You'll have to cast the list when you get it back out with Bundle.getParcelableArrayList() but it will work fine.)

提交回复
热议问题