2

I am trying to write a string set to shared preferences and at first glance it appears to work. In other parts of the app I can access the shared preferences and read the string set correctly.

The problem comes when I leave the app. All the data from the shared preferences string set is lost and it returns an empty set again.

The fact that I can access it until the app is closed and reopened make me think that it is being stored in memory but not stored to disk.

I read a lot of the answers on here, tried changing between commit and apply but I don't know what is causing the issue.

The way I try to save it is:

  1. Retrieve the hashset from shared preference
  2. Add new string to the hashset
  3. Save the updated hashset in shared preference.

Here is the code:

public static void storeReminder (Context context, String reminderString){

    // Get the set of reminder strings
    SharedPreferences sharedPreferences = context.getSharedPreferences("AppData", Context.MODE_PRIVATE);
    Set <String> remindersStringSet = sharedPreferences.getStringSet(context.getResources().getString(R.string.reminders_hashset_key), new HashSet<String>());

    // Add the new reminder string to the reminder string set
    remindersStringSet.add(reminderString);

    // Save the reminder string set now that the new reminder string has been added
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putStringSet(context.getResources().getString(R.string.reminders_hashset_key), remindersStringSet);
    editor.commit();
}

And this is how I get the the stored hashset in other part of the app:

// Get the set of reminder strings
SharedPreferences sharedPreferences = context.getSharedPreferences("AppData", Context.MODE_PRIVATE);
Set<String> remindersStringSet = sharedPreferences.getStringSet(context.getResources().getString(R.string.reminders_hashset_key), new HashSet<String>());

Thanks in advance for your help

Nicholas Muir
  • 2,897
  • 8
  • 39
  • 89

3 Answers3

3

Ok I found out what the cause was:

You have to delete the string set stored in the stored preferences and add a new copy of in its place.

I found it on one of the answers in this post:

Android: String set preference is not persistent

I changed my storing code like this and it works fine:

public static void storeReminder (Context context, String reminderID, String reminderString){

        // Get the set of reminder strings
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        Set <String> remindersStringSet = sharedPreferences.getStringSet(context.getResources().getString(R.string.reminders_hashset_key), new HashSet<String>());

        // Add the new reminder string to the reminder string set
        remindersStringSet.add(reminderString);

        // Get the shared preferences editor
        SharedPreferences.Editor editor = sharedPreferences.edit();

        // Delete the current set in shared preferences
        editor.remove(context.getResources().getString(R.string.reminders_hashset_key));
        editor.apply();

        // Save the NEW version of reminder string set
        editor.putStringSet(context.getResources().getString(R.string.reminders_hashset_key), new HashSet<String>(remindersStringSet));
        editor.apply();
    }

Thanks everyone for the help

Nicholas Muir
  • 2,897
  • 8
  • 39
  • 89
0

make sure you not have clearing the shared preferences while opening app like login or in other methods

Rajesh Wolf
  • 1,005
  • 8
  • 12
0

Create this class:

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import java.util.Set;

public class DemoPrefs {
private SharedPreferences prefs;
private SharedPreferences.Editor prefs_edit;
private static DemoPrefs instance;

public DemoPrefs(Context context) {
    initialize(context);
}

private void initialize(Context context) {
    prefs = PreferenceManager.getDefaultSharedPreferences(context);
    prefs_edit = prefs.edit();
}

public static DemoPrefs getInstance(Context context) {
    if (instance == null) {
        instance = new DemoPrefs(context);
    }
    return instance;
}

public void setReminderSet(Set<String> reminderSet) {
    prefs_edit.putStringSet("reminderSet", reminderSet);
    clear();
    prefs_edit.commit();
}

public Set<String> getReminderSet() {
    return prefs.getStringSet("reminderSet", null);
}

public void clear() {
    prefs_edit.clear();
    prefs_edit.commit();
}
}

Use SetReminderSet(reminderSet) and getReminderSet() method for setting or getting values respectively.

MainActivity:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private DemoPrefs prefs;
private EditText editText;
private Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initialize();
    setListener();
}


private void initialize() {
    prefs = DemoPrefs.getInstance(this);
    editText = (EditText) findViewById(R.id.editText);
    button = (Button) findViewById(R.id.button);
}

private void setListener() {
    button.setOnClickListener(this);
}

public void storeReminder(String reminderString) {
    Set<String> reminderSet = null;
    if (prefs.getReminderSet() == null)
        reminderSet = new HashSet<String>();
    else
        reminderSet = prefs.getReminderSet();
    reminderSet.add(reminderString);
    prefs.setReminderSet(reminderSet);
}

@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.button:
            if (!TextUtils.isEmpty(editText.getText().toString().trim())) {
                if (prefs.getReminderSet() != null)
                    System.out.println("OLD SIZE: " + prefs.getReminderSet().size());
                storeReminder("Set: " + editText.getText().toString().trim() + "");
                if (prefs.getReminderSet() != null)
                    System.out.println("NEW SIZE: " + prefs.getReminderSet().size());
            } else {
                Toast.makeText(this, "Please enter data", Toast.LENGTH_SHORT).show();
            }
            editText.setText("");
            break;

    }
}
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.pairroxz.demoapp.MainActivity">

<EditText
    android:id="@+id/editText"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginLeft="16dp"
    android:layout_marginTop="16dp"
    android:ems="10"
    android:inputType="textPersonName"
    app:layout_constraintHorizontal_chainStyle="spread_inside"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toLeftOf="@+id/button"
    app:layout_constraintTop_toTopOf="parent"
    tools:hint="@string/edit_message" />

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="16dp"
    android:layout_marginStart="16dp"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:text="@string/button_send"
    app:layout_constraintBaseline_toBaselineOf="@+id/editText"
    app:layout_constraintLeft_toRightOf="@+id/editText"
    app:layout_constraintRight_toRightOf="parent" />

</android.support.constraint.ConstraintLayout>
Tara
  • 692
  • 5
  • 23
Chirag Jain
  • 628
  • 11
  • 24
  • I have updated the code it is giving value even after closing app. Well I have used this separate class for simplicity and manage all things at one place. Actually clear() method before commit() is doing the work for giving value for next time. I know it is not the right thing to do clear() but it is working. – Chirag Jain Dec 15 '17 at 06:52