Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Alexandru Fediuc
Alexandru Fediuc
1,485 Points

Shared Preferences vs internal Json (or XML)

Hi there,

I haven't figured it out why would i really need to save data as Shared Preferences. Why wouldn't be more easy to save it to a JSON file and read it from there. If my app doesn't need to share data with other existing apps would it be really useful in my case?

Thanks

3 Answers

Shared Preferences data is kept secure from other apps' access; also, you can pretty easily store JSON/XML data in it.

String str = jsondata.toString();
SharedPreferences sharedPref = getSharedPreferences( "appData", Context.MODE_WORLD_WRITEABLE );
SharedPreferences.Editor prefEditor = sharedPref.edit();
prefEditor.putString( "jsondata", str);
prefEditor.commit();
Alexandru Fediuc
Alexandru Fediuc
1,485 Points

Hi Evan and thanks for the answer. I know that you can store a JSON/XML in the SharedPreferences. So encryption is the main functionality? Could an internal JSON file from the app be used by unwanted apps? or a SQLlite db?

I wouldn't say that it's the main functionality, no. From Android's System Permissions document; "no application, by default, has permission to perform any operations that would adversely impact other applications, the operating system, or the user. This includes reading or writing the user's private data (such as contacts or emails), reading or writing another application's files, performing network access, keeping the device awake, and so on."*

The android documentation on data storage provides some basics on the differences between Shared Preferences compared to other data storage options. Shared Preferences is a pretty straightforward way to store data, so it's often used, but you aren't stuck using it if your data can't easily be stored there; use whatever data storage method meets the needs of your project.

Shared Preferences: Store private primitive data in key-value pairs.

Internal Storage: Store private data on the device memory.

External Storage: Store public data on the shared external storage.

SQLite Databases: Store structured data in a private database.

Network Connection: Store data on the web with your own network server.

Also, TeamTreeHouse offers a course on data persistence in Android, which will go over your options in much greater detail.

Mazen Halawi
Mazen Halawi
7,806 Points

SharedPreferences are not saved in an encrypted file by default. You will need to use a third party library to do that. I think its a matter of preference how you save it, because you can also encrypt the file you save your Json data in. For security reason its better you use getSharedPreference than getPreference, as that points to the file that you name it unlike getPreference which uses the app_name.

You're correct, it's not actually encrypted, but is kept secure by system permissions from any app that isn't a superuser; I've updated that portion of my response. Thanks!

However, getPreferences actually calls getSharedPreferences(current_activity_class_name), and should therefore be equally secure to getSharedPreferences.

Mazen Halawi
Mazen Halawi
7,806 Points

They are both almost the same, but the fact that you can name your sharedPreference the name you want makes it more ambiguous than using the getPreference since its using like you said the current_activity_class_name, but both are saved in data/data/package... and prohibit access to normal users unless ofcourse your phone is rooted.