A Beginner’s Guide to Using SharedPreferences in Android Native Development
SharedPreferences is a powerful feature in Android native development that allows developers to store and retrieve key-value pairs persistently. It provides a simple and convenient way to save application settings, user preferences, and other data. In this article, we will explore how to use SharedPreferences in Android native development through practical code examples.
Prerequisites
To follow along with the code examples in this article, you should have a basic understanding of Android native development using Java or Kotlin.
Step 1: Adding the Dependency
To use SharedPreferences in your Android project, you need to include the necessary dependencies in your app’s build.gradle file.
android {
...
}
dependencies {
...
implementation 'androidx.preference:preference-ktx:1.1.1'
}
Step 2: Initializing SharedPreferences
To use SharedPreferences, you need to obtain an instance of SharedPreferences from the application context. It is recommended to initialize SharedPreferences in the onCreate() method of your application’s main activity or custom Application class.
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
public class MainActivity extends AppCompatActivity {
private SharedPreferences sharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize SharedPreferences
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
}
}
Step 3: Saving Data to SharedPreferences
To save data to SharedPreferences, you need to use an instance of SharedPreferences.Editor. You can retrieve the editor by calling the edit() method on the SharedPreferences instance. Then, use the put<datatype>() methods to store the values.
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("username", "John");
editor.putInt("age", 25);
editor.putBoolean("isPremiumUser", true);
editor.apply();
Step 4: Retrieving Data from SharedPreferences
To retrieve data from SharedPreferences, you can use the appropriate get<datatype>() methods provided by the SharedPreferences instance.
String username = sharedPreferences.getString("username", "Default");
int age = sharedPreferences.getInt("age", 0);
boolean isPremiumUser = sharedPreferences.getBoolean("isPremiumUser", false);
Step 5: Listening for SharedPreferences Changes
If you need to listen for changes in SharedPreferences, you can register a listener by calling the registerOnSharedPreferenceChangeListener() method on the SharedPreferences instance. You can then override the onSharedPreferenceChanged() method to handle the changes.
SharedPreferences.OnSharedPreferenceChangeListener listener =
new SharedPreferences.OnSharedPreferenceChangeListener() {
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (key.equals("username")) {
String newUsername = sharedPreferences.getString(key, "Default");
// Update UI or perform necessary actions
}
}
};
sharedPreferences.registerOnSharedPreferenceChangeListener(listener);
Conclusion
SharedPreferences is a useful feature in Android native development that allows developers to store and retrieve key-value pairs persistently.
👏🏽 Give this story a CLAP
👉🏽 Subscribe for upcoming articles
💰 Access Free Mobile Development tutorials
🔔 Follow for more
See you on next article 👋