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

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for 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:

  1. 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);
  1. 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 various put 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:

  1. Get an instance of SharedPreferences: Similar to the previous step, you need to get an instance of SharedPreferences.

SharedPreferences sharedPreferences = getSharedPreferences("MyPrefs", MODE_PRIVATE);
  1. 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:

  1. Get an instance of SharedPreferences: As mentioned before, you need to get an instance of SharedPreferences.

SharedPreferences sharedPreferences = getSharedPreferences("MyPrefs", MODE_PRIVATE);
  1. Edit the time value: Use the edit() method to obtain an instance of SharedPreferences.Editor. Then, use the put 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.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my