This question has already been answered:

SharedPreferences sPref; void saveVal(String id_val,String val) { sPref = getPreferences(MODE_PRIVATE); SharedPreferences.Editor ed = sPref.edit(); ed.putString(id_val, val); ed.commit(); Toast.makeText(this, "Address saved"+loadVal("id_address"), Toast.LENGTH_SHORT).show(); } String loadVal(String id_val) { sPref = getPreferences(MODE_PRIVATE); String savedText = sPref.getString(id_val, ""); return savedText; } 

In this activity, I write the address in id_address, and in another activity I read it through LoadVal, but I get 0. Explain why

Reported as a duplicate by YurySPb. java Apr 22 '18 at 12:12 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

1 answer 1

The Activity#getPreferences(int mode) method creates an xml file for storing key-value pairs that only this activity has access to. To get global access prefs, get them like this:

PreferencesManager.getDefaultSharedPreferences(context)