Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving an entire Bundle to SharedPreferences

Assuming a method of mine was passed a Bundle already filled with data to be saved, is there a way to save it to SharedPreferences without taking it apart to ints, floats, Strings, etc.?

I prefer the convenience of writing/committing it all in "one fell swoop", so if this isn't possible using SharedPreferences, what other persistent storage approach would you recommend?

like image 378
ateiob Avatar asked Aug 10 '12 14:08

ateiob


People also ask

Can I save array list in SharedPreferences?

You can save String and custom array list using Gson library. =>First you need to create function to save array list to SharedPreferences. public void saveListInLocal(ArrayList<String> list, String key) { SharedPreferences prefs = getSharedPreferences("AppName", Context. MODE_PRIVATE); SharedPreferences.

What is the maximum number of shared preferences you can create?

there is no limit in Shared Preference. Save this answer.

Is jetpack DataStore a replacement for SharedPreferences?

DataStore is a new and improved data storage solution aimed at replacing SharedPreferences. Built on Kotlin coroutines and Flow, DataStore provides two different implementations: Proto DataStore, that stores typed objects (backed by protocol buffers) and Preferences DataStore, that stores key-value pairs.


1 Answers

SharedPreferences are serialized to XML, which is why you can only use simple types. You'll either have to do your own conversion of the bundle to individual SharePreferences properties or serialize the bundle some other way to disk.

Here's a good reference document discussing the various possibilities of storing data in Android: http://developer.android.com/guide/topics/data/data-storage.html

like image 102
Edward Dale Avatar answered Sep 28 '22 05:09

Edward Dale