SharedPreferences preferences = getSharedPreferences("MyPrefs", MODE_PRIVATE); SharedPreferences.Editor editor = preferences.edit(); editor.putString("name", "John"); editor.apply();
SharedPreferences preferences = getSharedPreferences("MyPrefs", MODE_PRIVATE); String name = preferences.getString("name", "");In this example, we retrieve the SharedPreferences object by calling the `getSharedPreferences()` method with the same name and mode as in the previous example. We then use the `getString()` method on the SharedPreferences object to retrieve the value stored under the key "name". If the key is not found in the preferences file, we provide a default value of an empty string (""). Package Library: The android.content package is part of the Android SDK, which is a set of libraries and tools for developing Android applications. The SharedPreferences class is part of the Android framework and is therefore included in the android.jar library.