Book Image

Mastering Android Development with Kotlin

By : Miloš Vasić
Book Image

Mastering Android Development with Kotlin

By: Miloš Vasić

Overview of this book

Kotlin is a programming language intended to be a better Java, and it's designed to be usable and readable across large teams with different levels of knowledge. As a language, it helps developers build amazing Android applications in an easy and effective way. This book begins by giving you a strong grasp of Kotlin's features in the context of Android development and its APIs. Moving on, you'll take steps towards building stunning applications for Android. The book will show you how to set up the environment, and the difficulty level will grow steadily with the applications covered in the upcoming chapters. Later on, the book will introduce you to the Android Studio IDE, which plays an integral role in Android development. We'll use Kotlin's basic programming concepts such as functions, lambdas, properties, object-oriented code, safety aspects, type parameterization, testing, and concurrency, which will guide you through writing Kotlin code in production. We'll also show you how to integrate Kotlin into any existing Android project.
Table of Contents (24 chapters)
Title Page
Credits
About the Author
Acknowledgments
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface

How can you use them?


To use shared preferences, you have to obtain the SharedPreferences instance from the current context:

    val prefs = ctx.getSharedPreferences(key, mode) 

Here, key represents a String that will name this shared preferences instance. The XML file in the system will have that name as well. These are modes (operation modes) that can be available from Context class:

  • MODE_PRIVATE: This is a default mode, and the created file can only be accessed by our calling application
  • MODE_WORLD_READABLE: This is deprecated
  • MODE_WORLD_WRITEABLE: This is deprecated

Then, we can store values or retrieve them as follows:

    val value = prefs.getString("key", "default value")  

There is a similar getter method for all common data types.

Editing (storing) preferences

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

    preferences.edit().putString("key", "balue").commit() 

Note

The commit() method executes the operation immediately, while the apply() method executes it in...