I am having problems on using SharedPreferences in different activities and the XML file is no created (and I don't know why).
I'm using SharedPreferences in two activities:
Here is the first one, where I just need to read the file:
TextView screenCurrency = findViewById(R.id.dollar); preferences = getSharedPreferences("UserPreference", Context.MODE_PRIVATE); if(preferences.contains("currency")){ screenCurrency.setText(preferences.getString("currency","defect_currency")); if(preferences.getString("currency","defect_currency").equals("USD")){ screenCurrency.setText("$"); } else if(preferences.getString("currency","defect_currency").equals("EUR")){ screenCurrency.setText("€"); } } else { screenCurrency.setText("$"); }
This one is the other activity, where I read user preferences and I need to save them and include them in SharedPreferences:
//get user preferences Spinner spinnerCurrency = (Spinner) findViewById(R.id.spinnerCurrency); Spinner spinnerRefresh = (Spinner) findViewById(R.id.spinnerRefresh); currency = spinnerCurrency.getSelectedItem().toString(); refresh = spinnerRefresh.getSelectedItem().toString(); Button saveButton = findViewById(R.id.saveChangesButton); saveButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { preferences=getSharedPreferences("UserPreference", Context.MODE_PRIVATE); SharedPreferences.Editor editor = preferences.edit(); editor.putString("currency",currency); editor.putString("refresh",refresh); } });
Anyone knows what is wrong?