【问题标题】:Saving ArrayList<Custom Object> in Fragment onPause Method将 ArrayList<Custom Object> 保存在 Fragment onPause 方法中
【发布时间】:2016-01-10 07:22:51
【问题描述】:

我正在尝试使用自定义对象保存 ArrayList,以便当我移动到另一个片段并返回到它时,它不必重新加载数据。到目前为止,尽管将对象设为 Parcelable 并将其保存在 onSaveInstanceState 方法中,我仍然无法实现这一点。

【问题讨论】:

    标签: android arraylist onpause


    【解决方案1】:

    您还可以在 ApplicationContext 中保存对该列表的引用并以这种方式获取数据 - 因此您不需要序列化/反序列化

    【讨论】:

      【解决方案2】:

      如果你不想使用parcelable对象,你可以试试这个:

      1- By using GSON library, create jsonString from your Custom Arraylist.
      2- Send this jsonString to Fragment by using Bundle.
      3- Recieve jsonString from bundle and parse it to Custom Arraylist by using GSON Library again.
      4- You can also save it to SharedPreferences as a String.
      

      注意:这不是一个好方法,但对您来说很容易。

      【讨论】:

        猜你喜欢
        • 2015-06-21
        • 2021-09-15
        • 2018-03-16
        • 1970-01-01
        • 2014-11-20
        • 1970-01-01
        • 2014-06-14
        • 1970-01-01
        • 2013-02-15
        相关资源
        最近更新 更多