What is Shared Preferences and How to use it in Android
What is Shared Preferences in Android?
If you want to save data all across your application then Shared Preferences may be your choice. You can save data by using Shared Preferences and access the data anywhere in your application.
Shared Preferences data is associated with your application and if the user uninstalls the app or clears its data from the mobile setting, then you will lose the data.
Enough talking, now see how you can use Shared Preferences?
How to use Shared Preferences?
First, let's create a new project in the android studio.
So we will make two activities where we will save data in the first activity and show saved data in the second activity.
In the first activity xml, we will add an EditText, a Button to save trigger save data and another one Button to go to the second activity. See the below code.
Now, go to the first activity java file and make shared preferences save functionality and then we will show data to the next activity.
In the above code, the important part is in our save button click listener.
In the first line in the listener, we initialize Shared Preferences and pass the 2 parameters.
One for the name of the shared preference file name and second is for operation mode, we pass 0 which means private mode. You can use below other modes as per your requirement.
- MODE_PRIVATE: the default mode, where the created file can only be accessed by the calling application
- MODE_WORLD_READABLE: Creating world-readable files is very dangerous, and likely to cause security holes in applications
- MODE_WORLD_WRITEABLE: Creating world-writable files is very dangerous, and likely to cause security holes in applications
- MODE_MULTI_PROCESS: This method will check for modification of preferences even if the Shared Preference instance has already been loaded
- MODE_APPEND: This will append the new preferences with the already existing preferences
- MODE_ENABLE_WRITE_AHEAD_LOGGING: Database open flag. When it is set, it would enable write-ahead logging by default
In the next line, we need editor to write in the shared preference.
Shared Preferences saves the value in key-value pair, so we add the edittext's text in the shared preference and set the key as "editTextData".
After adding the value now we have to save the data by calling the apply method of the editor.
We show a toast message which will tell us that data has been saved.
Next, we add a click listener to our other button to start the second activity.
See Also: make an Image Slider in Android
See Also: make an Image Slider in Android
Now open your second activity xml file and add this code.
In the xml, we have added a textview to show saved data and a button to trigger to load and show data in textview.
Go to your second activity java file to write the functionality to display data.
Add this code in the onCreate method in your second activity java file.
Here, we added our textview and button from xml to java and set on click listener to the button.
In the click listener, we initialize our shared preferences object with the same parameters as we did in our first activity.
In the next line, we are getting our data with we saved earlier in the first activity with the key name "editTextData" and store its value in sharedPreferenceData variable also we have pass the null value as default value in case if we don't get any data from shared preferences.
After getting the value we will set the text to textView.
Now its time to run your app and see the results.
It should be similar to this.
later I have changed the second activity textview text size and gravity to make it center.
If this article helps you somewhere, please share this article with your friends and mates.
Thank you for reading, have a nice day.