How to use SharedPreferences in Android to store, fetch and edit values


How to Use SharedPreferences in Android 📱💾
So, you want to store a time value in your Android app and you need to be able to retrieve and edit it? Don't worry, we've got you covered! 😎
What are SharedPreferences? 🤔
SharedPreferences is an essential feature in Android that allows you to store small amounts of data - like key-value pairs - persistently. This means that even if the user closes your app or restarts the device, the stored data will still be available. 🙌
Storing a Time Value ⏰
To store a time value using SharedPreferences, follow these simple steps:
Get an instance of SharedPreferences: First, you need to get an instance of the SharedPreferences class. You can do this by calling the
getSharedPreferences()
method and passing in a unique name for your SharedPreferences file and the desired mode.
SharedPreferences sharedPreferences = getSharedPreferences("MyPrefs", MODE_PRIVATE);
Save the time value: To save a time value, you can use the
edit()
method to obtain an instance of the SharedPreferences.Editor class. Then, you can use the variousput
methods to store the time value.
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putLong("timeValue", System.currentTimeMillis());
editor.apply();
In the example above, we use the putLong()
method to store the current time in milliseconds with the key "timeValue".
Retrieving the Stored Time Value 🕰️
To retrieve the stored time value, you can follow these steps:
Get an instance of SharedPreferences: Similar to the previous step, you need to get an instance of SharedPreferences.
SharedPreferences sharedPreferences = getSharedPreferences("MyPrefs", MODE_PRIVATE);
Retrieve the time value: To retrieve the time value, you can use the appropriate
get
method, in this case,getLong()
. Provide the key used to store the time value and a default value in case the key is not found.
long timeValue = sharedPreferences.getLong("timeValue", 0);
In the example above, we retrieve the time value using the key "timeValue" and set a default value of 0 if it is not found.
Editing the Stored Time Value 🔄
Editing a stored time value is straightforward. Simply follow the steps below:
Get an instance of SharedPreferences: As mentioned before, you need to get an instance of SharedPreferences.
SharedPreferences sharedPreferences = getSharedPreferences("MyPrefs", MODE_PRIVATE);
Edit the time value: Use the
edit()
method to obtain an instance of SharedPreferences.Editor. Then, use theput
methods to update the time value.
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putLong("timeValue", System.currentTimeMillis());
editor.apply();
In our example above, we overwrite the previous time value with the current time in milliseconds.
A Common Pitfall 🚫🪣
One common mistake when using SharedPreferences is forgetting to call apply()
or commit()
after making changes. Don't forget to do this, as changes won't be saved if you skip this step.
Conclusion 🎉
You should now have a good understanding of how to use SharedPreferences in Android to store, fetch, and edit time values. Remember to always follow the correct sequence of steps and avoid the common pitfalls. 🚀
If you have any questions or other topics you'd like us to cover, let us know in the comments below! 👇Happy coding!
Take Your Tech Career to the Next Level
Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.
