Setting values in Preference:
// Global Variable Declare SharedPreferences sharedpreferences; SharedPreferences.Editor; // In onCreate Bundle sharedpreferences = getSharedPreferences(MyPREFERENCES, MODE_PRIVATE); // or sharedpreferences = getSharedPreferences("", ""); editor.sharedpreferences.edit(); // Data Put editor.putString(Name, n); editor.apply();
Retrieve data from preference:
// Global Variable Declare SharedPreferences sharedpreferences ; //Data Get Strings sharedpreferences = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); //or sharedpreferences = getSharedPreferences("", ""); String name = sharedpreferences.getString("name", "No name defined"); //"No name defined" is the default value. int idName = sharedpreferences.getInt("idName", 0); //0 is the default value.
Tags:
Android Studio