Skip to content Skip to sidebar Skip to footer

Shared Preferences In Android Showing Default Value Not Found

I strucked in storing and getting shared preferences.When a user has been registered if that registration is success then that values has to be stored in his profile nothing but my

Solution 1:

You are using getSharedPreferences and getDefaultSharedPreferences together.

Do like this -

SharedPreferencessharedpreferences= getApplicationContext().getSharedPreferences("a2a", Context.MODE_PRIVATE);
SharedPreferences.Editoreditor= sharedpreferences.edit();
editor.putString("enteredfname", enteredfname);
editor.putString("enteredlname", enteredlname);
editor.commit();

And later -

SharedPreferencespreferences= getApplicationContext().getSharedPreferences("a2a", Context.MODE_PRIVATE);
Stringvalue= preferences.getString("enteredfname", "enteredfname");

Solution 2:

You've used SharedPreferences.Editor for storing value and then inside another Activity you're using PreferenceManager to retrieve the value. That's why you're getting default value always.

PreferenceManager is used for storing Preferences for a particular app. They are used for storing app settings where as SharedPreferences are used for saving data or values for working inside the app. They cannot be used for app settings. These are two separate files, though both use the Preferences class.

You need to use SharedPreferences object the retrieve the value in place of PreferenceManager. you've already created a SharedPreferences object named "preferences" inside your AnotherActivity. Just use this SharedPreferences object to retrieve the data you want to as shown below:

Stringvalue= preferences.getString("key", def_value);

Solution 3:

To save value:

@OverrideprotectedvoidonPostExecute(String s) {

              pd.dismiss();

                    if("SUCCESS".equals(jsonresponce)){
                        Toast.makeText(getApplicationContext(),jsonresponce,Toast.LENGTH_SHORT).show();
                         SharedPreferencessharedpreferences= getSharedPreferences("a2a", Context.MODE_PRIVATE);
                        SharedPreferences.Editoreditor= sharedpreferences.edit();

                        editor.putString("enteredfname", enteredfname);
                        editor.putString("enteredlname", enteredlname);
                        editor.commit();

                    }else{
                        Toast.makeText(getApplicationContext(),jsonresponce,Toast.LENGTH_SHORT).show();
                    }

              super.onPostExecute(s);
          }
      }

To retrive value:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    ViewrootView= inflater.inflate(R.layout.fragment_myprofile, container, false);

    mfname = (EditText)rootView.findViewById(R.id.fname);
    mlname = (EditText)rootView.findViewById(R.id.lname);
    mcontact = (EditText)rootView.findViewById(R.id.contact);
    mpassword = (EditText)rootView.findViewById(R.id.editpass);
    SharedPreferencessharedpreferences= getSharedPreferences("a2a", Context.MODE_PRIVATE)

    value = sharedpreferences.getString("enteredfname", "enteredfname")

    mfname.setText(value);
    return rootView;
}

Solution 4:

try something like this.. Create a class

publicclassSharedPreferenceCustom {
  privateContext mContext;
  privateString defValue = "";

  publicSharedPreferenceCustom(Context context) {
    this.mContext = context;
  }

  publicvoidsetSharedPref(String inputKey, Object inputValue) {
    final SharedPreferences sharedPreferences = mContext.getSharedPreferences("a2a", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString(inputKey, String.valueOf(inputValue));
    editor.apply();

    }

  publicStringgetSharedPref(String inputKey) {
    SharedPreferences sharedPreferences = mContext.getSharedPreferences("a2a", Context.MODE_PRIVATE);
    return sharedPreferences.getString(inputKey, defValue);
 }
}

and call whenever needed

Call by

SharedPreferenceCustomsp=newSharedPreferenceCustom(mContext);
   sp.setSharedPref("KEY", "VALUE");
   // or
   sp.getSharedPref("KEY");

Post a Comment for "Shared Preferences In Android Showing Default Value Not Found"