Saving Data on Android


Saving the user preferences



Download 19,28 Mb.
Pdf ko'rish
bet49/292
Sana24.01.2022
Hajmi19,28 Mb.
#407089
1   ...   45   46   47   48   49   50   51   52   ...   292
Bog'liq
Bailey J., Dominguez A., Djermanovic D. - Saving Data on Android (1st Edition) - 2019

Saving the user preferences
First, you have to create some constants which will represent the keys for the key—
value pairs of data you need to save. Open 
NotePrefs.kt
 and insert the following 
declarations right below the declaration of 
DEFAULT_PRIORITY_FILTER
:
  
private
 const 
val
 KEY_NOTE_SORT_PREFERENCE = 
"note_sort_preference"
 
  
private
 const 
val
 KEY_NOTE_PRIORITY_SET = 
"note_priority_set"
Next, you have to create the function to write the sort order to the prefs file.  Insert 
the code below into 
saveNoteSortOrder()
, replacing the 
TODO
:
sharedPrefs.edit() 
    .putString(KEY_NOTE_SORT_PREFERENCE, noteSortOrder.name) 
    .apply()
This code retrieves the 
Editor
 object from the app's prefs and puts the sort order in 
the preferences as a 
String
 with the key 
KEY_NOTE_SORT_PREFERENCE
. It then uses 
apply()
 so that the changes will be written asynchronously to the disk.
Now, provide the code to write the selected priority filters. Insert the code below into 
saveNotePriorityFilters()
, once again replacing the 
TODO
:
sharedPrefs.edit() 
    .putStringSet(KEY_NOTE_PRIORITY_SET, priorities) 
    .apply()
Saving Data on Android
Chapter 2: Shared Preferences
raywenderlich.com
44


The previous code puts 
priorities
, a 
Set
 of 
String
s, representing the selected 
priority filters, into the prefs using 
putStringSet()
 with the key 
NOTE_PRIORITY_SET
.
Awesome! The functions to write to the prefs are done. Next, you'll add the code to 
read the values.

Download 19,28 Mb.

Do'stlaringiz bilan baham:
1   ...   45   46   47   48   49   50   51   52   ...   292




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish