-3

I save some EditText data into shared preferences and then on another activity I show it in TextView (it works - easy) - but when I want to use saved variable in a method it does not work - do you have any idea how to retrieve e.g. IP from shared pref?

sharedpreferences = PreferenceManager.getDefaultSharedPreferences(this);
String AJPI  = Text1.getText().toString();
editor.putString(IPv4, AJPI);
editor.commit();
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Trew laz
  • 29
  • 4
  • 2
    check demo http://www.androidhive.info/2012/08/android-session-management-using-shared-preferences/ – IntelliJ Amiya Jun 16 '17 at 08:17
  • Possible duplicate of [How to use SharedPreferences in Android to store, fetch and edit values](https://stackoverflow.com/questions/3624280/how-to-use-sharedpreferences-in-android-to-store-fetch-and-edit-values) – Sufian Jun 19 '17 at 08:59

3 Answers3

0

Did you add this line?

editor = sharedpreferences.edit();
dustblue
  • 557
  • 5
  • 14
0

Save:

PreferenceManager.getDefaultSharedPreferences(context)
.edit()
.putString(IPv4, "myString").apply();

Retrieve:

String s =
PreferenceManager.getDefaultSharedPreferences(context)
.getString("myString", "defaultStringIfNothingFound");
S.R
  • 2,819
  • 2
  • 22
  • 49
  • It works for showing the string on the layout/Activity but it is not working for using the variable String in the method... – Trew laz Jun 16 '17 at 08:45
  • is this `IPv4` variable you want to save into `Sharedpreferences` or this `AJPI` ? because as I can see in your code you are not saving the `editText` variable in `Preferences`. So it should be like this: `.putString(AJPI, "myString").apply();` – S.R Jun 16 '17 at 09:47
0

Save to SharePreference

 SharedPreferences pref = getSharedPreferences("Preference", MODE_PRIVATE);
 SharedPreferences.Editor editor = pref.edit();
 editor.putString("Value", your_value);
 editor.commit();

Read from SharePreference

 SharedPreferences pref = getSharedPreferences("Preference", MODE_PRIVATE);
 String phone_number = pref .getString("Value", "");
Fakhriddin Abdullaev
  • 4,169
  • 2
  • 35
  • 37