Android SharedPreferences using Kotlin With Examples

In this tutorial, we’ll learn how to implement SharedPreferences in our Android Application using Kotlin.

What is Android SharedPreferences?

SharedPreferences is part of the Android API since API level 1. It’s an interface that allows us to store/modify/delete data locally.

Generally, it is used to cache user local data such as login forms. The data is stored in the form of a key-value pair.

You can create multiple files to hold the SharedPreferences data.

SharedPreferences Methods

Let’s look at some important methods for SharedPreferences.

  • getSharedPreferences(String, int) method is used to retrieve an instance of the SharedPreferences.
    Here String is the name of the SharedPreferences file and int is the Context passed.
  • The SharedPreferences.Editor() is used to edit values in the SharedPreferences.
  • We can call commit() or apply() to save the values in the SharedPreferences file. The commit() saves the values immediately whereas apply() saves the values asynchronously.

SharedPreferences Setting/Retrieving Values using Kotlin

We can set values on our SharedPreference instance using Kotlin in the following way.

For retrieving a value:

The permitted types on a SharedPreference instance are:

android-shared-preference-types

Kotlin Code to Clear and Remove SharedPreferences Records

We can also clear all the values or remove a particular value by calling clear() and remove(String key) methods.

Note: Changes made to the editor after the commit or apply aren’t considered.

The above way to save and retrieve values from a SharedPreference is nearly the same as we do in Java.

So where’s the magic of Kotlin?

That’s what we’ll see next through an example android application.

Android SharedPreferences Kotlin Project Structure

android-shared-preference-types

In this application, we’ll have a login screen, which allows us to save/clear the form data.

1. Layout code

The code for the activity_main.xml layout file is given below.

2. MainActivity Kotlin Code

The code for the MainActivity.kt Kotlin class is given below.

Thanks to Kotlin Android Extensions, we don’t have to use findViewById for each XML view.

In the above code, we are creating a singleton class using the object keyword.

We are declaring an inline higher-order function named editMe(), which holds the logic for the edit operation.

We’ve created separate properties for each of the values. We are using the get and set Kotlin properties to retrieve and set the data in the shared preferences.

Kotlin has reduced the code verbosity and it looks much cleaner.

Furthermore, we can make it more concise by using another Kotlin higher-order function shown below.

And we do the following while setting the values:

This is as close as Kotlin can get you to the English Language.

The output of the above application in action is given below.

android shared preferences kotlin app output

By admin

Leave a Reply

%d bloggers like this: