How to save diffrent type of value into sharedpreferences android studio

sharedpreferences is most useful in android to store some information and useable any where in project

Step 1: create and put values into shared preferences

SharedPreferences preferencesbirthday = getSharedPreferences("Data", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = preferencesbirthday.edit();
            editor.putInt("number", 12);
            editor.putString("string","Hello");
            editor.putBoolean("booleanvalue",true);
            editor.commit();

step 2: create variables and get value any where make sure key names are same

   SharedPreferences theme = getActivity().getSharedPreferences("Data", Context.MODE_PRIVATE);
        String stringvalue = theme.getString("string","");
        int intvalue = theme.getInt("number", 0);
        boolean booleanvalue = theme.getBoolean("booleanvalue",false);

Thank You Keep Learning Keep Connected #AndroidShortCode

Comments