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 -

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

And later -

SharedPreferences preferences = getApplicationContext().getSharedPreferences("a2a", Context.MODE_PRIVATE);
String value = 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:

String value = preferences.getString("key", def_value);

Solution 3:

To save value:

  @Override
          protected void onPostExecute(String s) {

              pd.dismiss();

                    if("SUCCESS".equals(jsonresponce)){
                        Toast.makeText(getApplicationContext(),jsonresponce,Toast.LENGTH_SHORT).show();
                         SharedPreferences sharedpreferences = getSharedPreferences("a2a", Context.MODE_PRIVATE);
                        SharedPreferences.Editor editor = 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) {
    View rootView = 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);
    SharedPreferences sharedpreferences = getSharedPreferences("a2a", Context.MODE_PRIVATE)

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

    mfname.setText(value);
    return rootView;
}

Solution 4:

try something like this.. Create a class

public class SharedPreferenceCustom {
  private Context mContext;
  private String defValue = "";

  public SharedPreferenceCustom(Context context) {
    this.mContext = context;
  }

  public void setSharedPref(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();

    }

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

and call whenever needed

Call by

   SharedPreferenceCustom sp = new SharedPreferenceCustom(mContext);
   sp.setSharedPref("KEY", "VALUE");
   // or
   sp.getSharedPref("KEY");

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