Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
184 views
Welcome To Ask or Share your Answers For Others

1 Answer

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;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...