Williams Williams -3 years ago 140
Android Question

PreferenceDataStore in Android O

I read this article https://medium.com/@ianhlake/hidden-gems-of-android-o-7def63136629 . This is what is written there:

SharedPreferences is dead. Long live SharedPreferences.

keep working in Android O ? Do we need to implement own mechanism for storing data in key value pairs by implementing

Can anyone help how would be approach to implement new
& What is use case of developing own implementation ? Any drawback in current approach ?

Answer Source

I can't think of a case where it's applicable only to use PreferenceDataStore instead of SharedPreferences completely but i do think that this can be helpful if you would like to use them together.

SharedPreferences provide you with a great service which is when your app is updated the data still stays in the preferences, but with PreferenceDataStore you also store the data in the same format as in SharedPreferences. Now suppose if you want to use the same preference interface but you would like to store these values in the cloud instead of the device, well because, devices can break down.

What PreferenceDataStore can help you with is that it provides you with the flexibility to store data in any place and write your own implementation. It's not meant to replace SharedPreferences completely though you can do it if you like.

For an example you can use access token of the app in shared preferences, and all the other data in cloud or local db or cloud or maybe file system if you want, and you can use the PreferenceDataStore interface, write down your own implementation and then use it.

even in the link of developer documentation in Google PreferenceDataStore https://developer.android.com/reference/android/preference/PreferenceDataStore.html it's written

In most cases you want to use SharedPreferences as it is automatically backed up and migrated to new devices. However, providing custom data store to preferences can be useful if your app stores its preferences in a local db, cloud or they are device specific like "Developer settings". It might be also useful when you want to use the preferences UI but the data are not supposed to be stored at all because they are valid per session only.

So you can see that even google doesn't want you to use PreferenceDataStore all the time, it's only for the time that when you need to use the same Preferences style of storing key value pairs, but you want to implement your own data store which will provide you with more flexibility then you currently have in SharedPreferences.

For example what if you want to do get SharedPreferences and then put the SharedPreferences data on a cloud server. You want it on the device but also backed up on cloud, In this scenario PreferenceDataStore can help you out.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download