When does SharedPreferences are being read from disk? - android

I'm not actually sure the cycle of getting SharedPreferences of Android app.
Are they being read during getPreferences()(and stored in memory)?
Or they are being read during SharedPreferences#getBoolean()?(getInt(), etc)

The documentation says " Retrieve and hold the contents..." when using getSharedPreferences(), so they are stored in memory when you call it.

Related

Is Shared Preferences safe for private datas?

Im saving datas from my db/user into a gson formated ArrayList in SharedPreferences. Now my question :
Is it safe to save these datas (or data in general) into Sharedpreferences. Are users able to read these gson Arraylists out ? Maybe from SD card ,in a folder or somewhere else.
Thank you !
They are stored as xml files in your app directory, with permissions that allow only your app to access them. But on rooted device they are easily accessible. If you are concerned with security then you may use encryption, those projects might be usefull to you:
https://github.com/rtoshiro/SecureSharedPreferences
https://github.com/sveinungkb/encrypted-userprefs
still those projects does not give you 100% guarantee, hacker may decompile your apk and find keys used to encrypt shared preferences. So if your data is of use only for short time then remember to remove it from your device once user has finished using it. You may for example keep data on server and download it only when needed, caching locally only for short time - when its needed.
SharedPreferences is just a file located in phone private memory. So user can't access it but root can. Root can everything and many users have root's nowadays. You shouldn't store fragile data there
Android SharedPreference security
You can read all shared preferences Data
The SharedPreferences class provides a general framework that allows
you to save and retrieve persistent key-value pairs of primitive data
types.
To see the information in the store you need to know the important thing from the data. This will make reading through the information super easy. But as simple as it's to keep a tiny bit of data as difficult it's to keep and browse large structured data since you need to define key for every data, in addition you can't really search inside the data except you've got a certain concept for naming the secrets.
Please read Android SharedPreference security

What is the best way to store string? SharedPreferences Set Collection or Files?

I have to store some data (string) in my Android app and I'm a dilemma. What is better solution ? Use Set Collection and keep it at SharedPreferences or I should save data to File and when I need it I have to read data from file and put it for example on ArrayList.
Depends on the quantity & complexity of the data. There is no straight answer to your question.
General approach: If the data are not too sensitive, small in quantity and more frequently used then you should go with SharedPreferences.
If your data is quite large lets say few hundred KBs of String then you should go with File.
SharedPreferences ultimately stores data into a file. The advantage of using SharedPreferences is that, the data is stored as a key value pair and can be retrieved easily using a key.
It depends on what you want to store.
Use SharedPreferences when you want store simple, prmitive data. Keep in mind that SharedPreferences will be available only for your app, so anothers apps cannot get data from it.
Use File when you have more complex data. You have to take care about how file is available to others. If you put it in sdcard root directory for example it will be available for everyone. If you put it in app package it will behave like SharedPreferences.

Android how to keep static data on application relaunch?

I am having an abstract class having all static objects and function to store some globel data of the application during the execution session.
All data reset to null once I relaunch the application in ICS and above version with Systems's setting ALWAYS_FINISH_ACTIVITIES is set as true.
Whats the better way to mainitan data on application relaunch?
Regards,
Android IT
Editing my question:
I know that Sharepreference can be a better option but I dont want to store data for multiple sessions of the application and data I am storing is huge.
Regards,
Android
IT
Each Android app is running in a dependent process. When an app become background and system need memory, the app's process may be killed by system. In this situation, when app relaunch, static objects will lost.
I think use SharedPreference to maintain data is a better way.
You can store data using SharedPreferences
http://developer.android.com/reference/android/content/SharedPreferences.html
Store the data on the local storage would seem appropriate: http://developer.android.com/training/basics/data-storage/index.html
You can store the data local using a XML file. You can read the XML File on startup with a DOM Reader
http://www.ibm.com/developerworks/library/x-android/
If you have lots of data you can use a SQLite Database
http://developer.android.com/training/basics/data-storage/databases.html
If there are not too much data you can keep them in SharedPreferences
You access them with PreferenceManager.getDefaultSharedPreferences()
Once application exit or destroy that will remove all data . if you want to access after relaunch you should save your data as per your requirement . like local or web. its deepen you. right now you can store data in preference so it will useful after relaunch the application.
I think this is better for you : How to use SharedPreferences in Android to store, fetch and edit values

how to cache Google API key string to memory?

I'm developing an Android app that uses Google Places API.
In order to keep the Google API Key out of the application code, I fetch it from my own authenticated API server and its needed in many places around the app.
what is the best way of storing / caching it to memory ?
I understand that just using a singleton will be garbage collected.
is extending the Application class and adding methods to there a good method ?
anything else I might consider ?
Use SharedPreferences
To save to preferences:
PreferenceManager.getDefaultSharedPreferences(context).edit().putString("MYAPIKEY",
"myStringToSave").commit();
To get a stored preference:
PreferenceManager.getDefaultSharedPreferences(context).getString("MYAPIKEY",
"defaultStringIfNothingFound");
Where context is your Context.
Once you get it from server, save it. You can then access it as and when you want. Uninstalling an app will of course remove its contents. Else it is persistant.
A VM cannot garbage collect string references that classes can still reach, so I don't see why you cannot keep the string in a singleton and store it as a static reference. You can also store it in a shared preference like Doomsknight suggests, but this will not be cached in memory. Shared preferences are stored as XML files on a persistence store (against values available on a RAM). Values are retrieved back from them.
Perhaps you need a combination of the two. Store the value in a shared preference and then cache it in memory when your app starts. I'm unaware if the PreferenceManager by itself has a caching mechanism of some sort in memory.

store data without using database in android

i have to make an android application in which i need to download a lot of data from the server which is sent to me via XML. i then need to parse the XML and then display the extracted information.
To avoid making the application slow, i have decided to break my XML down into small parts.. so that i can only call the part that i want, this would limit the information that i am receiving.
My question is once that i have parsed the XML data where do i store it ( except for a db ) until my UI is rendered? On the iPhone there is something called user default where in we can store such information. What would be the equivalent in android?
thank you in advance.
Every thing you need should be right here: http://developer.android.com/guide/topics/data/data-storage.html. Internal storage is the closest to the iPhone equivalent.
You can use application preferences to store data as shown here (if the data is small enough).
Their code sample shows:
SharedPreferences gameSettings = getSharedPreferences("MyGamePreferences", MODE_PRIVATE);
SharedPreferences.Editor prefEditor = gameSettings.edit();
prefEditor.putString("UserName", "Guest123");
prefEditor.commit();
I wouldn't do this for large datasets but it's a handy place to store data.
This gets removed when the application is removed too.
If you've got a lot of data, I'd suggest storing it on disk.
You could store it in a file.
You could store it in SharedPreferences
You could use a ContentProvider --> not recommended for temporary storage.
You could use a SQLiteDB --> I know you said you do not want to use this.

Categories

Resources