Reader small image

You're reading from  Android Programming with Kotlin for Beginners

Product typeBook
Published inApr 2019
Reading LevelIntermediate
PublisherPackt
ISBN-139781789615401
Edition1st Edition
Languages
Right arrow
Author (1)
John Horton
John Horton
author image
John Horton

John Horton is a programming and gaming enthusiast based in the UK. He has a passion for writing apps, games, books, and blog articles. He is the founder of Game Code School.
Read more about John Horton

Right arrow

Making the Note to self settings persist


We have already learned how to save data to the device's memory. As we implement saving the user's settings, we will, again, see how we handle the Switch widget input and where exactly the code we have just seen will go to make our app work the way we want it to.

Coding the SettingsActivity class

Most of the action will take place in the SettingsActivity.kt file. So, click on the appropriate tab and we will add the code a bit at a time.

First, we want a property to represent the user's option on the settings screen – whether they want decorative dividers or not.

Add the following to SettingsActivity:

private val showDividers: Boolean = true

Now, in onCreate, add the highlighted code to initialize prefs, which is inferred to be a SharedPreferences instance:

val prefs = getSharedPreferences(
               "Note to self",
                Context.MODE_PRIVATE)

Note

Import the SharedPreferences class:

import android.content.SharedPreferences

Next, still in onCreate...

lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
Android Programming with Kotlin for Beginners
Published in: Apr 2019Publisher: PacktISBN-13: 9781789615401

Author (1)

author image
John Horton

John Horton is a programming and gaming enthusiast based in the UK. He has a passion for writing apps, games, books, and blog articles. He is the founder of Game Code School.
Read more about John Horton