Editing (storing) preferences

We will start this section by providing an example of preferences editing:

    preferences.edit().putString("key", "balue").commit() 
The commit() method executes the operation immediately, while the apply() method executes it in the background. Never obtain or manipulate your shared preferences from an application's main thread if you use the commit() method. Make sure that all writing and reading is performed in the background. You can use AsyncTask for that purpose or, instead of commit(), use apply().

Get Mastering Android Development with Kotlin now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.