-1

In my application I'm using Shared Preferences to save my app settings.

here is the way I define a Shared Prefrences:

sharedPreferences = context.getSharedPreferences(getString(R.string.last_update_key),MODE_PRIVATE);
String shared = sharedPreferences.getString(getString(R.string.last_update_key),"0");

and my ASyncTask onPostExecute block that uses passed Shared Preferences:

protected void onPostExecute(String result) {
    if(result == "true"){

        textView.setText(R.string.updated_message);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(context.getString(R.string.last_update_key),getCurrentTimeStamp());
        try {
            editor.putString("s_app_name",json3.getJSONObject(0).getString("value"));
            editor.putString("s_township_name",json3.getJSONObject(1).getString("value"));
            editor.putString("s_welcome_message",json3.getJSONObject(2).getString("value"));

        }catch (JSONException e){
        }
        editor.commit();

    }

but the strings that I put them in try block will not display in my activity and I do not get any error in catch. here is how I get my saved value :

SharedPreferences sharedPref = context.getSharedPreferences(
            getString(R.string.last_update_key), Context.MODE_PRIVATE);
String welcome_message = sharedPref.getString("s_welcome_message","");

the problem is that I can get the value of R.string.last_update_key but not the others.

Szymon Stepniak
  • 40,216
  • 10
  • 104
  • 131
Artin GH
  • 546
  • 2
  • 10
  • 21

1 Answers1

0

It was my bad. I have to place my code outside of "if" block

Artin GH
  • 546
  • 2
  • 10
  • 21
  • the problem that you should not use == in strings, because it always false, instead, you should use the method .equals() the strings in java is object and == matching the references not the value – Amjad Omari Oct 07 '17 at 21:15
  • @AmjadOmari you're not quite right. comparing string with == is true if the string is the same. E.g: `String a = "test"; String b = "test"; if (a == b) // this is will return true`. More at https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – ישו אוהב אותך Oct 08 '17 at 07:21