4

I'm trying to save a custom Arraylist in shared preference, but when i restart or re-run and send a new push ,it overwritte the others

 @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();
}
Ervin
  • 336
  • 1
  • 10
  • 1
    It is overwritting the value because the key is a constant. And the solution is correct, you could save uour object using ROOM. – cutiko May 25 '19 at 18:56

1 Answers1

1

You shouldn't save the data from the Notification in a custom ArrayList, but save the data into SQLite database

Griseld Gerveni
  • 482
  • 2
  • 5
  • 17