Android provides several storage options for you to store persistent application data. This article covers one of the most used storage options for this purpose of storing persistent data which is Android SharedPreferences. Last time we have discussed SQLite in Android if you have not seen that tutorial you can check it here Android SQLite Database tutorial.
Steps involved in this Android SharedPreferences Tutorial are:
1. Introduction to Android SharedPreferences.
2. Storing data to shared preference at the activity level.
3. Storing data to shared preference at the application level.
4. Different operations in shared preference.
So before going further let me explain various storage options for storing persistent data. And what persistent data actually is.
Persistence data is to store our data for a longer duration of time and make it available for your application to use whenever your application needs it. Android SharedPreferences store data in a key-value pair and this file has the format of XML.
SharedPreferences file can be both public and private, which means making the file private block the unauthorized access to our shared preferences file from other applications, for this purpose we need to use MODE_PRIVATE flag. And making the file public helps other application to access the shared preferences file in which, we will be using MODE_WORLD_READABLE flag.
Since the public mode is not used anymore we will be using private mode in this tutorial.
1.Creating the project and adding resources
1. Open Android studio click on file->New->NewProject then enter the details of the project like name, backward compatibility, and package name.
2. Create an empty activity and name it the default one which is MainActivity (I just want to make it simple) then click finish. Now you have created your project. Make sure that there is no error by tapping on the RUN button at the top.
3. In the styles.xml file located under res->values change the line
Theme.AppCompat.Light.DarkActionBar to Theme.AppCompat.Light.NoActionBar to hide the default action bar(This step is optional).
2. Creating Layout
In the activity_main.xml add the following code.
Now run the app and you should get a screen similar to this.
Initializing layout components in MainActivity.java
Activity level Shared Preferences
It is used to create a separate Shared Preferences file for your Activity.
Set up onclick listener for the save button. In that onclick method initialize the activity-level Shared Preferences. Also get the data from edit text and store in string file which, later we will use to add to the shared preferences file.
now run the app and type the fields and click save. It will save the entered data to the shared preferences file. now let’s retrieve those values from the file and load them into the text view.
For that, you need to implement onClicklistener on the load button and add the following code to it.
Now run the app. when clicked on load button your textview should show the exact same text in edit text. Like below
This shared preference is limited to a single activity which is MainActivity.java
now lets jump on to application level shared preferences
Application level Shared Preferences
First, let’s create another activity by clicking on yourpackagename -> new ->activity -> empty activity and name it SecondActivity.
To navigate from MainActivity to our MainActivity.java change the code inside the save button click listener to following.
And also change the code in the load button onClick method with following
Now implement onclicklistener on the second activity button for navigating to the second activity on clicking it.
Now open second_activity.xml and add the following code
In SecondActivity.java initialize the layout components just like we did in MainActivity.java also add a click listener to the button to get data from shared preference and load them in text view.
final SecondActivity.java look like below.
final MainActivity.java look like below.
Run the app and I hope you got a screen as follows