SharedPreferences preferences = context.getSharedPreferences("my_prefs", Context.MODE_PRIVATE); SharedPreferences.Editor editor = preferences.edit(); SetfavoriteColors = new HashSet<>(Arrays.asList("red", "green", "blue")); editor.putStringSet("favorite_colors", favoriteColors); editor.apply();
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences.Editor editor = preferences.edit(); SetIn this example, the user's preferences are saved under the key "user_preferences" in the default SharedPreferences file. The getDefaultSharedPreferences() method is used to get the default shared preferences for the app. Package library: android.contentuserPreferences = new HashSet<>(Arrays.asList("notifications", "dark_mode", "language")); editor.putStringSet("user_preferences", userPreferences); editor.apply();