【发布时间】:2019-01-21 16:21:23
【问题描述】:
我正在尝试将自定义 Arraylist 保存在共享首选项中,但是当我重新启动或重新运行并发送新推送时,它会覆盖其他人
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
NotificationData data = new NotificationData(remoteMessage.getData());
notificationDataArray.add(data);
SharedPreferences sharedPreferences = getSharedPreferences(NotificationShared.SHARED_PREFERENCES, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
Gson gson = new Gson();
String json = gson.toJson(notificationDataArray);
editor.putString(NotificationShared.DATA_ARRAY, json);
editor.apply();
}
【问题讨论】:
-
它正在覆盖值,因为键是常量。并且解决方案是正确的,您可以使用 ROOM 保存您的对象。
标签: android shared preference