-3

I'm using the following to change a symbol:

private void updateCurrencySymbol() {
    SharedPreferences sharedPreferences  = PreferenceManager.getDefaultSharedPreferences(this);

    String symbol = sharedPreferences.getString("preferences_currency", "$");
    Toast.makeText(getApplicationContext(),symbol,Toast.LENGTH_LONG).show();
    currencySymbol1 = (TextView) findViewById(R.id.currencySymbol1);
    currencySymbol2 = (TextView) findViewById(R.id.currencySymbol2);
    currencySymbol1.setText(symbol);
    currencySymbol2.setText(symbol);
    //refreshes the activity
    Bundle temp_bundle = new Bundle();
    onSaveInstanceState(temp_bundle);
    Intent intent = new Intent(this, MainActivity.class);
    intent.putExtra("bundle", temp_bundle);
    startActivity(intent);
    finish();
}

In the Toast it shows the right symbol that I chose from the settings but it never changes in the actual TextView. I tried changing the text in the onCreate() method and it works, changing to the string I specify.

Jackie Jones
  • 100
  • 1
  • 12
  • What are you trying to do? pass data to another activity? – EE66 Jun 07 '15 at 17:08
  • No, it's just a setting to choose between different currencies (only a symbol changes really). Then I try to set the TextView to the symbol chosen and refresh the activity (keeping all data in the fields that are there). – Jackie Jones Jun 07 '15 at 17:10
  • 2
    The reason for getting '$' sign every time is because your sharedprefs in empty. You need first to save your data before you refresh the activity. see this: http://developer.android.com/training/basics/data-storage/shared-preferences.html – Avi Levin Jun 07 '15 at 17:13

1 Answers1

2

It wont chagne becuase the data at SharedPreferences doesnt change. There is no need to restart the activity. No need AT ALL. Just setText() the textview. then save to sharedPrefrences for future starts.

EE66
  • 4,601
  • 1
  • 17
  • 20
  • Even without restarting the activity the TextView doesn't change, and I am saving it to my shared preferences in another section of the code. – Jackie Jones Jun 07 '15 at 17:17
  • If the text doesnt chagne then you are not commiting the changes some where else. Where u change the String. – EE66 Jun 07 '15 at 17:19
  • Problem is solved, it turns out only one of the symbols was being committed but not the other, thanks. – Jackie Jones Jun 07 '15 at 17:25