Android SharedPreferences – Save Key Value​ data example in kotlin

Android SharedPreferences is store key-value (String, Integer, Boolean, etc) Paris data. SharedPreferences object points to a file holding a key-value pairs and provides simple methods to read and write data. It can be private or shared.

Android SharedPreferences Tutorial - Save Key Value data example in kotlin

SharedPreferences usually use for save a setting preference (push notification, alert, the default setting of the app, etc) or manage a login session in the app.

In this tutorial, you will learn following:

  1. What is Android SharedPreferences and how to create it.
  2. Saving and Retrieving data in SharedPreferences in Kotlin.

Create Shared Preferences

For a creating or accessing an existing shared preference, you can use by calling one of a method:

  • getSharedPreferences() – this method use for multiple shared preference files and it can identified by name. For call this have to use any Context of your app.
  • getPreferences() – Retrieve a default shared preferences file form the Activity (belong the activity).Its use only one shared preference file for the activity.

getSharedPreferences()

When naming your shared preference files, you should use a name that’s uniquely identifiable to your app. An easy way to do this is prefix the file name with your application ID. For example: "com.example.myapp.PREFERENCE_FILE_KEY"

getPreferences()

if you need just one shared preference file for your activity.

Note: The MODE_WORLD_READABLE and MODE_WORLD_WRITEABLE modes have been deprecated since API level 17.

Write to shared preferences

You have call edit() on yourSharedPreferences, To write to a shared preferences file.

Pass the keys and values you want to write with methods for example :  putInt() and putString(). Then call apply() orcommit() to save the changes. For example:

Check this tutorial question number #2 :  What is the difference between commit() and apply() method in SharedPreferences()?

Read from shared preferences

To retrieve values from a shared preferences file, call methods getInt() and getString() etc, providing the key for the value you want, and optionally a default value to return if the key isn’t present. For example:

Let’s Build a Simple Example of Android SharedPreferences :

Step 1. Create new project “Build Your First Android App in Kotlin
Step 2. Add following code in “activity_main.xml” layout file

adding 2 TextView and 2 Button. Where button have method to perform a Click action.

Step 3. Open the “MainActivity.kt” and add following code

In this code doing action by clicking button. Saving and getting data with same key.

Step 4. Now Run the application, in emulator or On you android device

Output screenshot Android SharedPreferences example :

Android SharedPreferences Tutorial - Save Key Value data example in kotlin output

Download source code Android SharedPreferences in kotlin

https://github.com/EyeHunts/SharedPreferences

Note: This example (Project) is developed in Android Studio 3.1.3. Tested on Android 9 ( Android-P), compile SDK version API 27: Android 8.0 (Oreo)

MinSdkVersion=”15″

TargetSdkVersion=”27″

Coding in Kotlin


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.