SharedPreferences prefs = getSharedPreferences("MyPrefs", MODE_PRIVATE); SharedPreferences.Editor editor = prefs.edit(); editor.putString("favorite_color", "blue"); editor.apply();
SharedPreferences prefs = getSharedPreferences("MyPrefs", MODE_PRIVATE); String favoriteColor = prefs.getString("favorite_color", "not set");In this example, we retrieve the SharedPreferences object associated with a file named "MyPrefs". We then use the getString() method to retrieve the value for the key "favorite_color". If the key does not exist or has not been set, the default value "not set" will be returned. The package library for the android.content.SharedPreferences class is "android.content".