43

I have made a shared preference activity that store the user settings, now i want to get values in a activity or normal java class.please provide a solution or example i have already tried this code but failed.

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    SharedPreferences channel=this.getSharedPreferences(strFile, Context.MODE_PRIVATE);
    strChannel=channel.getString(keyChannel,"Default").toString();
    Toast.makeText(getApplicationContext(), strChannel, Toast.LENGTH_LONG).show();
}                     

in this code strfile for eg. com.android.pack.ClassName is SharedPreference Activity from values to be fetched, and keyChannel is key that is same in SharedPreference Activity.

Kindly provide the solution.

VMAtm
  • 27,943
  • 17
  • 79
  • 125
Sandeep
  • 2,573
  • 3
  • 21
  • 28

4 Answers4

80

If you have a SharedPreferenceActivity by which you have saved your values

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
String imgSett = prefs.getString(keyChannel, "");

if the value is saved in a SharedPreference in an Activity then this is the correct way to saving it.

SharedPreferences shared = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
SharedPreferences.Editor editor = shared.edit();
editor.putString(keyChannel, email);
editor.commit();// commit is important here.

and this is how you can retrieve the values.

SharedPreferences shared = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
String channel = (shared.getString(keyChannel, ""));

Also be aware that you can do so in a non-Activity class too but the only condition is that you need to pass the context of the Activity. use this context in to get the SharedPreferences.

mContext.getSharedPreferences(PREF_NAME, MODE_PRIVATE);
Umesh
  • 4,406
  • 2
  • 25
  • 37
  • I am not able to understand one thing that is the difference between SharedPreferences shared = getSharedPreferences(PREF_NAME, MODE_PRIVATE); String channel = (shared.getString(keyChannel, "")); and this SharedPreferences channel=this.getSharedPreferences(strFile, Context.MODE_PRIVATE); strChannel=channel.getString(keyChannel,"Default").toString(); kindly tell one thing what will be the value of PREF_NAME, please provide the example, may be i am giving wrong path of class. I gave String strFile="com.android.pref.PrefClass"; – Sandeep Sep 21 '11 at 09:39
  • 1
    There PRE_NAME is only a String. It could be anything eg: your application name, your name, your class name etc. There is absolutely no diff betwwen the two statements. what is your output BTW – Umesh Sep 21 '11 at 09:52
  • it shows blank toast, but i confused, how can new activity know which preference to be fetched. please explain what will the matching paramter by which a perticular value can be get... – Sandeep Sep 21 '11 at 09:56
  • 1
    if i putt some value in getString(key,"Default") then is give Default on Toast. – Sandeep Sep 21 '11 at 10:05
  • the PREF_NAME should be the same in all your Activities accross your application. It is the key that changes. SharedPreferences shared = getSharedPreferences(PREF_NAME, MODE_PRIVATE); this line reserves a pool of memory space inside the app memory. when you use the following line ... editor.putString(keyChannel, val); a space is created and val is stored in it, and can be identified by the variable keyChannel. – Umesh Sep 21 '11 at 10:11
  • can u please give me samlpe of this code, means there should be a shared pref activity and one normal activity or class, – Sandeep Sep 21 '11 at 10:18
  • read this.. this post has an in depth discussion on SharedPreference and PreferenceActivity... http://stackoverflow.com/questions/2614719/how-do-i-get-the-sharedpreferences-from-a-preferenceactivity-in-android – Umesh Sep 21 '11 at 10:32
  • Finaly i found the solution and now its working fine. i just add SharedPreferences shared = PreferenceManager.getDefaultSharedPreferences(this); String val=shared.getString("key", "defValue"); Thanks – Sandeep Sep 22 '11 at 08:02
  • What has been missing is the default name, since it isn't specified anywhere. "com.package.name_preferences.xml" This little bread crumb solved the problem for me. – Naidim May 14 '15 at 01:40
5

I tried this code, to retrieve shared preferences from an activity, and could not get it to work:

        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    sharedPreferences.getAll();
    Log.d("AddNewRecord", "getAll: " + sharedPreferences.getAll());
    Log.d("AddNewRecord", "Size: " + sharedPreferences.getAll().size());

Every time I tried, my preferences returned 0, even though I have 14 preferences saved by the preference activity. I finally found the answer. I added this to the preferences in the onCreate section.

getPreferenceManager().setSharedPreferencesName("defaultPreferences");

After I added this statement, my saved preferences returned as expected. I hope that this helps someone else who may experience the same issue that I did.

Dave Cox
  • 91
  • 1
  • 3
1

This is the procedure that seems simplest to me:

SharedPreferences sp = getSharedPreferences("MySharedPrefs", MODE_PRIVATE);
SharedPreferences.Editor e = sp.edit();

    if (sp.getString("sharedString", null).equals("true")
            || sp.getString("sharedString", null) == null) {
        e.putString("sharedString", "false").commit();
        // Do something
    } else {
        // Do something else
    }
simeg
  • 1,889
  • 2
  • 26
  • 34
samreen
  • 209
  • 2
  • 6
1

You use uninstall the app and change the sharedPreferences name then run this application. I think it will resolve the issue.

A sample code to retrieve values from sharedPreferences you can use the following set of code,

SharedPreferences shared = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
String channel = (shared.getString(keyValue, ""));
Codemaker2015
  • 12,190
  • 6
  • 97
  • 81