Saving Data on Android



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

Wri/ng to preferences
Writing to the 
SharedPreferences
 is slightly more complicated. To write to the file 
you must open it for editing, by creating an 
Editor
. You can do that using 
edit()
 on 
your 
SharedPreferences
. The 
SharedPreferences.Editor
 is essentially a pointer to 
the 
SharedPreferences
 file, in the app's data directory. Then you pass key—value 
pairs to methods such as 
putInt()

putString()
 and 
putLong()
. Once the key—
value pairs have been added to the 
Editor
, call either 
apply()
 or 
commit()
 to 
finalize the changes, and save to the file.
Generally, it is a good practice to choose 
apply()
 to write the 
SharedPreferences

apply()
 will write the changes to the object out immediately, but then saves those 
changes to the disk asynchronously. If two 
Editor
s try to use 
apply()
 at the same 
time, the last one to call the function will be the one that has its changes saved. 
apply()
 will complete before the app switches states so you don't have to worry 
about it interfering with the lifecycle of your app. 
commit()
 writes the changes 
synchronously which can block the main thread and result in the app locking up, 
until everything is properly stored.
Now, it's time to look at an example of 
SharedPreferences
 in action.
GeTng started
To get started with prefs, locate this chapter's folder in the provided materials named 

Download 19,28 Mb.

Do'stlaringiz bilan baham:
1   ...   42   43   44   45   46   47   48   49   ...   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