Access Shared Preference Boolean Value From Another Class
I have Created 2 check boxes and saved their values in boolean data type in shared preferences. How do I get it in another class and use it in if-else conditions? First Class
Solution 1:
Setting SharedPreferences
SharedPreferencessp=getSharedPreferences("checkbox", MODE_PRIVATE);
SharedPreferences.Editoret= sp.edit();
et.putBoolean("isLogin", true);
et.commit();
Getting value from SharedPreferences
SharedPreferencessp= getSharedPreferences("checkbox", 0);
booleancb1= sp.getBoolean("isLogin", false);
Solution 2:
there are like one million of questons like this, and with 1 minute of google search you would have achieved it.
this is the same question, I copy-paste the solution: you can use this code in any activity
Setting values in Preference:
// MY_PREFS_NAME - a static String variable like: //public static final String MY_PREFS_NAME = "MyPrefsFile";
SharedPreferences.Editoreditor= getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putString("name", "Elena");
editor.putInt("idName", 12);
editor.commit();
Retrieve data from preference:
SharedPreferencesprefs= getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
StringrestoredText= prefs.getString("text", null);
if (restoredText != null) {
Stringname= prefs.getString("name", "No name defined");//"No name defined" is the default value.intidName= prefs.getInt("idName", 0); //0 is the default value.
}
more info:
Post a Comment for "Access Shared Preference Boolean Value From Another Class"