![]() ![]() The apply method updates in the memory storage of file preference and starts asynchronously commit changes into disk storage. val prefs = PreferenceManager.getDefaultSharedPreferences(this) ![]() We can save data in two different ways which have an impact on performance. All changes are collected until we will use commit() or apply() methods. ![]() The SharedPreferences.Editor interface is used for modifying shared preferences. For any particular change, we should use SharedPreferences.Editor object. The SharedPreferences interface provides methods for accessing and modifying preferences stored in the XML file. You can find an Android implementation of these two interfaces here. Two main interfaces, which allow us, to work with preferences, are SharedPreferences and Editor. Let us take a look at preference file with properties of each type. Let us check which data we can store in preferences: In-memory layer stores everything in HashMap.īefore moving to save and getting preferences. However, these two layers will be synchronised. Under the hood, we are interacting with an in-memory layer for better performance. We have two layers of the storing preferences: in memory storage and file storage. Val appPrefs = getSharedPreferences("app_prefs", Context.MODE_PRIVATE)Īn implementation of SharedPreferences interface you can find here. val userPrefs = getSharedPreferences("user_prefs", Context.MODE_PRIVATE) The getSharedPreferences functions have a name parameter it means that we can create multiple files with preferences. Val prefs = getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)ĭefault and non-default shared preference files use the same structure: It means that the preference file can be accessed by the calling application or another application with the same User ID (UID). In the current version of API (28) only one mode is not deprecated, it’s called a MODE_PRIVATE. The mode parameter is a file creation mode.The name parameter of the function is the name of a preference file.The getDefaultSharedPreferences function returns an instance of SharedPreferences interface, which gives us the possibility to get and edit preferences.Then, any other time we use the ‘x’ variable, it’s just going to use the value stored in ‘x’ (42). )) 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. |