How to save a string in sharedPreferences

旧时模样 提交于 2020-03-06 09:38:13

问题


I have an application where I need to save a string in a sharedpreferences so that the user has already opened the application once and registered his email he does not have to go through the same screen again and instead go to the home screen directly.

My Class PreferencesHelpers

public class PreferencesHelpers {

private static final String SHARED_PREFS = "sharedPrefs";
private static final String TEXT = "ahhsaushhuuashu"; //I want to save this string

public String text;


public static void saveData(Context context) {
    SharedPreferences sharedPreferences = context.getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();

    editor.putString("", TEXT);

}

public static String loadData(Context context) {
    SharedPreferences sharedPreferences = context.getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
    String text = sharedPreferences.getString("ahhsaushhuuashu", "");
    return text;
   }

}

MyLogic in MainActivity to save and retrieve sharedPreferences value

  if (!preferencesHelpers.loadData(getApplicationContext()).contains("ahhsaushhuuashu")) {
                webView.loadUrl(URL_);
                preferencesHelpers.saveData(getApplicationContext());
            } else {
                switch (urlMessage) {
                    case "REDR":
                        webView.loadUrl(URL + "cira");
                        break;
                    default:
                        webView.loadUrl(URL + "?UDI=" + getInstance().getRegistrationManager().getSystemToken() + "&dev=" + getInstance().getRegistrationManager().getDeviceId() + "&source=app");
                }
            }

I looked for an answer that fit my condition but I did not find and forgive me for my English


回答1:


You need a fixed key to save and read your preference and you forgot to apply the modifications of the SharedPreference.

You need to do like this:

private static final String SHARED_PREFS = "sharedPrefs";
private static final String TEXT = "ahhsaushhuuashu";
private static final String KEY = "myKey";

public static void saveData(Context context) {
    SharedPreferences sharedPreferences = context.getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString(KEY, TEXT);
    editor.apply();
}

public static String loadData(Context context) {
    SharedPreferences sharedPreferences = context.getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
    String text = sharedPreferences.getString(KEY, "");
    return text;
}



回答2:


As mentioned by others in the comments you need Keys for storing a value, but also I see that you are not saving the value in the saveData method.

You need to type in this line:

editor.apply()

after

editor.putString("", TEXT);

in the saveData method



来源:https://stackoverflow.com/questions/54075490/how-to-save-a-string-in-sharedpreferences

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!