5

Here is code I am Using to create and store value in Preference. outgoing is String variable.

SharedPreferences sp = getSharedPreferences(outgoing, Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();                
editor.putString("PhoneNo","Hi");
editor.commit();

Here is the code to get value from SharedPreference.

SharedPreferences sp 
=getSharedPreferences(outgoing,Activity.MODE_PRIVATE);
String calln = sp.getString("PhoneNo","0");
Toast.makeText(mContext, "SHARED"+calln,Toast.LENGTH_LONG).show();
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
Vikram
  • 85
  • 1
  • 11

4 Answers4

6

You should probably call the getSharedPreferences on the context from which you are accessing them.

Source

Therefore, depending on how you can access your context, if you pass it to some other activity or in an asynchronous task, here are some examples of usage:

this.getSharedPreferences(outgoing, Activity.MODE_PRIVATE);

context.getSharedPreferences(outgoing, Activity.MODE_PRIVATE);

getApplicationContext().getSharedPreferences(outgoing, Activity.MODE_PRIVATE);

Also, one way you can test your stuff is to use a listener when SharedPreferences get changed:

onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)

Called when a shared preference is changed, added, or removed.

here is how to do that

You can also use the Preference Manager to obtain the SharedPreferences:

PreferenceManager.getSharedPreferences(YOUR_CONTEXT).getString(
                    "PhoneNo", "0");

Or to store them:

PreferenceManager.getSharedPreferences(YOUR_CONTEXT).edit().putString(
                    "PhoneNo", "Hi").commit();
Community
  • 1
  • 1
nem035
  • 34,790
  • 6
  • 87
  • 99
2

The most likely reason that Shared Preference always returns the default value is that you saved the value in one preference file and then tried to retrieve it in another preference file. This can happen if you do call getPreferences() from different Activities, because getPreferences() creates a different preference file based on the activity it is created in.

Solution

The easiest solution is to always get your shared preferences like this:

SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);

This will use a single preference file for the entire app.

Alternate solution

If for some reason you need to use different preference files, then you can do

final static String PREF_FILE_1 = "pref_file_1";
...
SharedPreferences sharedPref = context.getSharedPreferences(PREF_FILE_1, Context.MODE_PRIVATE);

Just make sure you always use the right file name for the preferences you are trying to save and retrieve.

Local preferences

If you really only need a preference for a specific Activity, then you can use getPreferences(Context.MODE_PRIVATE). Just don't expect to be able to retrieve the values from another Activity in the same way.

See also

Community
  • 1
  • 1
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
1

change this Activity.MODE_PRIVATE to this Activity.MODE_MULTI_PROCESS, issue is probably due to different context during storing value and accessing value.

Techfist
  • 4,314
  • 6
  • 22
  • 32
  • Thanks For Reply. I have changed it to Activity.MODE_MULTI_PROCESS. But it still give me DEFAULT VALUE. – Vikram Sep 17 '14 at 05:08
  • 1
    "**This constant was deprecated in API level 23.** `MODE_MULTI_PROCESS` does not work reliably in some versions of Android, and furthermore does not provide any mechanism for reconciling concurrent modifications across processes. Applications should not attempt to use it. Instead, they should use an explicit cross-process data management approach such as ContentProvider." – JorgeAmVF Apr 19 '18 at 03:42
1

When putting values, try changing this:

SharedPreferences sp = getSharedPreferences(outgoing, Activity.MODE_PRIVATE);

to this:

SharedPreferences sp = getApplicationContext().getSharedPreferences(outgoing, Activity.MODE_PRIVATE);

Same when getting values - don't forget to add getApplicationContext() in your call to SharedPreferences

EDIT: Check that your "outgoing" String is the exact same in both Activities

gilonm
  • 1,829
  • 1
  • 12
  • 22
  • Thanks For Reply. I have add getApplicationContext but there is no difference in result It still Give me DEFAULT VALUE. – Vikram Sep 17 '14 at 05:09
  • Are you calling the retrieving method (getString) from the same Activity? – gilonm Sep 17 '14 at 05:34
  • No from Different Activity. – Vikram Sep 17 '14 at 05:43
  • And are you sure you are using the exact String value of "outgoing"? Have you declared it in both Activities? Try replacing outgoing with a hardcodded String (as "test" or something) when calling SP in both Activities, and see if it makes a difference. – gilonm Sep 17 '14 at 05:47
  • Oh.. Get it. There is some problem with outgoing. It's working with "test". Thanks for the help. I make changes it will done. :) – Vikram Sep 17 '14 at 05:55
  • Glad to hear! See my edited answer, and if it has helped please accept it. 10X and Happy Coding! – gilonm Sep 17 '14 at 05:57
  • Can you help me to store sharedprference file in particular location.? When I am trying to do same it gives me Error that File Contains Path separator. – Vikram Sep 17 '14 at 06:13
  • Ya It is possible. I am trying but get some error. There is some small mistakes that I can't get. – Vikram Sep 17 '14 at 06:53
  • Please PM me so I can try help you – gilonm Sep 17 '14 at 06:56