Now we’ll be storing UI mode preference using a key (as we managed in SharedPreference).createDataStore() is extension function created on Context. This will initialize the instancedataStore field by creating DataStore using the file name as “settings_pref”. We’ll create a class - SettingsManager where we’ll be managing setting preferences set by users in our app. Dark mode or Light mode, we’ll create an enum class as below Start implementing DataStore □įor UI mode preference i.e. You can keep an eye here to get info about the latest version. Currently 1.0.0-alpha01 is the latest release. □ Light Mode or □ Dark Mode.įirst of all, let’s add a Gradle dependency in adle of your app module. We’ll develop a sample Android application which stores a UI mode preference from user i.e. You can simply clone or refer this repository to get example code demonstrating DataStore□. I think that’s enough introduction to DataStore. Proto DataStore - It stores data as a custom type with specified schema using Protocol Buffers (We’ll see about it in the next article). Preference DataStore - This uses key-value pairs to store data. # That’s not only the reason -ĭataStore provides two different types of implementations to store data. These are some reasons which encourage us to use DataStore and finally say goodbye to beloved SharedPreferences □. It provides Type safety! (Using Protocol buffers). It also provides a way to migrate from SharedPreferences □. SharedPreference has some drawbacks like it provided synchronous APIs -but it’s not MAIN-thread-safe! whereas DataStore is safe to use in UI thread because it uses Dispatchers.IO under the hood□. If you have used SharedPreferences you might abuse or blamed it for something □ then DataStore is here to rescue! In short, it’s the new data storage solution which is the replacement of SharedPreferences.įirst and my favourite reason □ - Built with❤️ Kotlin, Coroutines and Flow. It allows us to store key-value pairs (like SharedPreferences) or typed objects with protocol buffers (We’ll see it in next article).ĭataStore uses Kotlin, Coroutines and Flow to store data synchronously with consistency and transaction support □. Jetpack DataStore is a data storage solution. Let’s see what’s DataStore and why DataStore. Currently, it’s alpha version is released. This article is the first part of a series article based on the new Jetpack library□ i.e. Then, any other time we use the ‘x’ variable, it’s just going to use the value stored in ‘x’ (42).Welcome Android developers □. )) class MainActivity : AppCompatActivity() ), and then store that result in ‘x’. (Don’t worry if the layout part doesn’t totally make sense it’s in Anko and there will be a course on that shortly. Color.GREEN), and when a color is selected the background will update to that color. Then we’ll add a preference for the background color, and start exploring the ways that Kotlin can help us write less code.Īnd here’s the app! It’s just a list of all the color constants from the Color class (e.g. Since Kotlin is still a fairly new language, rather than jumping right to the conclusion, we’re going to start with a simple app where we can change the background color. In this post we’re going to focus on making SharedPreferences as easy to use as possible, and by the end we’ll be able to use and modify our preferences from anywhere inside the app! Kotlin is all about letting us do more work with less code, and SharedPreferences are no exception.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |