0

I have a project where I need to use a shared preference. I'm just trying the shared preference at the moment, so instead of passing a string value through an intent, I'm passing it through a shared preference. in my first activity I have this:

SharedPreferences prefs = this.getSharedPreferences("com.example.mayankthakur.personalprojecttrial2", Context.MODE_PRIVATE);
Intent nameSave = getIntent(); 
name = nameSave.getStringExtra("name") 
prefs.edit().putString(name, "name").apply();

in my second activity

SharedPreferences preferences = getSharedPreferences("prefs", Context.MODE_PRIVATE);
name = preferences.getString("name", name);
example.setText(name);

for now, I just have a textview in the second activity where I would like to show the value of string name. the app doesn't crash, but there is one bug. instead of showing the value of string name, the textview just shows "name" without the quotes. I looked this up and turns out I need a string key for the get string method and the put string. so I would like to know what a string key is (so I don't have this problem later) and how I can fix this issue in my code.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Mayank Thakur
  • 61
  • 1
  • 12
  • Both Preferences name is different. use same name – ρяσѕρєя K Jul 26 '17 at 06:02
  • Keys are used to identify values. For example in this case you are storing your preference against `name`. this will be used whenever you want to get the name value from your preference. – M.Waqas Pervez Jul 26 '17 at 06:04
  • @Mayank Your keys are to identify what value you have saved. I guess what you were trying to do is save the name in fields name in the key "name". Check my answer below on which parameter is key and which is value. Your key should remain constant for both put and get methods. – Kapil G Jul 26 '17 at 06:07
  • 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) – Abhinash Jul 26 '17 at 09:20

6 Answers6

0

In first Activity;

 SharedPreferences prefs = this.getSharedPreferences("prefs", Context.MODE_PRIVATE);
prefs.edit().putString(name, "name").apply();

In Second Activity;

SharedPreferences preferences = getSharedPreferences("prefs", Context.MODE_PRIVATE);
name = preferences.getString("name", "");
example.setText(name);
Vishal Vaishnav
  • 3,346
  • 3
  • 26
  • 57
0

You just have to reverse it. Change

prefs.edit().putString(name, "name").apply();

to

prefs.edit().putString("name", name).apply();

In Shared preference the first parameter you put is key and second is value. And when you get it the first parameter is key and the second parameter is default value in case first parameter key value is null.

name = preferences.getString("name", "default value");
Kapil G
  • 4,081
  • 2
  • 20
  • 32
0

there are two changes here..

prefs.edit().putString("name", name).apply();

instead of writing name in getstring you have to pass default value,so change your code like this,

   SharedPreferences preferences = getSharedPreferences("prefs", 
  Context.MODE_PRIVATE);

        example.setText(preferences.getString("name",null);

this will solve your problem

Deep Naik
  • 491
  • 1
  • 3
  • 10
  • does this mean that the defualt value of the string is null? because the string has a value and that value is whatever the user provided at the begginning of the app – Mayank Thakur Jul 26 '17 at 06:18
  • not really. you see, the user provides their name at the start of the app, and that name is assigned to the string variable name, which is then transferred using the shared preference. so the value isnt null, is it? – Mayank Thakur Jul 26 '17 at 06:23
  • no that means if you dont provide any username it will take null instead it wil l take the value from "name" key. – Deep Naik Jul 26 '17 at 06:24
  • turns out, i had to put name instead of null as a string because i noticed that the second parameter was the one that was being transferred to the textview. instead of null i wrote name as a string value and it works great! thanks deep! – Mayank Thakur Jul 26 '17 at 06:46
0

SharedPreferences prefs = this.getSharedPreferences("com.example.mayankthakur.personalprojecttrial2", Context.MODE_PRIVATE);

Intent nameSave = getIntent(); 
name = nameSave.getStringExtra("name") 

prefs.edit().putString("name", name).apply();

Seeing your above code you need to make changes as mentioned by me above. The difference is the "name" with quotes is the key name and simply variable name depicts the value for "name" key. This will set the value you are getting through getStringExtra("name") in "name" key in shared Preference.

Aditi
  • 389
  • 4
  • 13
0

Try this code :

First activity

public static final String MY_PREFS_NAME = "MyPrefsFile";
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
Intent nameSave = getIntent(); 
name = nameSave.getStringExtra("name") 
Log.d("TEST name = "+name);
editor.putString("name", name);
editor.apply();

Second Activity :

public static final String MY_PREFS_NAME = "MyPrefsFile";
SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 
String name = prefs.getString("name", "No name defined");//"No name defined" is the default value.
Log.d("TEST name = "+name);
Ranjith KP
  • 858
  • 6
  • 18
0

You did wrong in giving key value first comes key then value. Check Below

Instead of this

prefs.edit().putString(name, "name").apply();

Change to

prefs.edit().putString("name", name).apply();
Sunil P
  • 3,698
  • 3
  • 13
  • 20