Skip to content Skip to sidebar Skip to footer

Previously Added Items Are Added Again When Saving An ArrayList Using SharedPreferences

In my app I have 3 activities A,B and C. In Activity A when a button is pressed it starts Activity B and an empty arraylist is passed to it using putExtra. In Activity B, if the ar

Solution 1:

A previous comment:

You shouldn't use an uppercase letter at the begining of a variable name. It is confusing, since it looks like a class name, and also it can lead you to some troubles. You should rename your RecipientArray to recipientArray everywhere.

Lets get into your problem.

Im not sure if I understand your code, but what i can see (look at my coments:)

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //here you retrieve the array coming from Activity A

            RecipientArray = (ArrayList<Person>) getIntent().getSerializableExtra(
            "RecArray");
    if (RecipientArray.size() == 0) {
               ///...
    } else {
        //here, you add to the array, all the preferences from the array
        SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(this.getApplicationContext());
        int size = prefs.getInt("size", 0);
        for (int i = 0; i < size; i++) {
            String json = prefs.getString("RecList_" + i, "");
            Gson gson = new Gson();
            Person p = gson.fromJson(json, Person.class);
            RecipientArray.add(p);
        }
        // ...
    }
}

//here, when the activity is going to be closed, you save all the items in the array to the sharedpreferences
protected void onPause() {      
    super.onPause();
    SharedPreferences prefs = PreferenceManager
            .getDefaultSharedPreferences(this.getApplicationContext());
    Editor editor = prefs.edit();
    Gson gson = new Gson();
    // String json = gson.toJson(RecipientArray);
    for (int i = 0; i < RecipientArray.size(); i++) {
        String json = gson.toJson(RecipientArray.get(i));
        editor.putString("RecList_" + i, json);
        editor.putInt("size", i);
    }

    editor.apply();
}

So what you are doing,

  1. from A to B: you pass the array and add the elements in saved preferences.
  2. from B to A: save all the elements to shared preferences (now you have the elements from the array coming from A, and also the previous elements from shared preferences, so your shared preferences is bigger)
  3. from A to B: you pass the array again, and adds the elements from that bigger sharedpreferences, that as we saw in point 2, already contains all the elements, also the ones form the array, so you are duplicating them.

so think about what do you need, probably you dont have to pass the array between A and B, or you dont have to save it to Shared preferences, or you have to check what items do you save. Think about what you really need, the problem is in those lines


Post a Comment for "Previously Added Items Are Added Again When Saving An ArrayList Using SharedPreferences"