Using Shared Preferences in Android

前端 未结 5 1113
别跟我提以往
别跟我提以往 2021-01-27 05:05

I have three activities, A, B & C. Where A is a splash Activity and B Contains Login screen which consist of user Id and Password Text Field and one button to login. When I

5条回答
  •  隐瞒了意图╮
    2021-01-27 05:44

    This is the best way to use Shared preference just call this method

    Store shared preference

    public static void setDefaults(String key, String value, Context context) {
        SharedPreferences prefs =
                PreferenceManager.getDefaultSharedPreferences(context);
    
        SharedPreferences.Editor editor = prefs.edit();
    
        editor.putString(key, value);
    
        editor.commit();
    }
    

    Call this method and pass argument like this

    Classname.setsetDefaults("key","Value",context);
    

    Get Shared Value

        public static String getDefaults(String key, Context context) {
            SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
            return preferences.getString(key, null);
        }
    

    Call this method And pass key

    ClassName.getDefaults("Key",Context);
    

提交回复
热议问题