Are shared preferences in Android always read at startup and stored in memory during runtime? If not, are there more efficient ways to read preferences than this?
settings = getSharedPreferences("myPrefsFile", 0);
int answer = settings.getInt("ULTIMATE_QUESTION", 42);
Are shared preferences in Android always read at startup and stored in memory during runtime
Simply Yes till user doesn't clear it manually from setting.
are there more efficient ways to read preferences than this
As JonasCz said in comments.. this is the most common and developer friendly way till now.
Related
In android, is it possible to delete data in shared preferences by clearing data in cache memory? if yes then what is happens & if not then why?
Clearing an app's cache does not clear the settings. The settings data (including shared preferences) is stored in a different place than the app's cache. You need to "clear data" to clear out the shared preferences. See this thread, for instance, for more info.
Using SharedPreferences.Editor clear () and commit() will remove all values from the preferences. Do you mean this?
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.
I've been searching for days to solve this, but with no success.
I want to get the shared preference settings from my old app and put it on to my new app.
but I've encounter some security issue (suspect).
my code:
Context c = createPackageContext("my.app.pkg", Context.CONTEXT_IGNORE_SECURITY);
SharedPreferences sp = c.getSharedPreferences("my.app.pkg", Context.CONTEXT_IGNORE_SECURITY);
Running the code above giving me this:
Attempt to read preferences file /data/data/my.app.pkg/shared_prefs/my.app.pkg_preferences.xml without permission
even thought the object sp is not null, but it does not retrieve anything from my old app.
I tried googling around and seems like most people can run the code above with no errors.
Is there anything I've missed out?
We've done exactly this for our Android book Android in Practice. The key is to use the same process and user ID for both apps. The sample code and sample apps are on Google Code (SharedProcessApp1 and SharedProcessApp2). You can go from there.
You can indeed share preferences across applications. That's why it's called SharedPreferences.
What you need to do is make sure both application are signed with the same certificate and they both share the same SharedUserId in the AndroidManifest.xml file: read here.
This is because the SharedPreferences you get from
PreferenceManager.getDefaultSharedPreferences(context)
has always MODE_PRIVATE.
However in an application you can also get a SharedPreferences object within a Context with the following:
SharedPreferences prefs = getSharedPreferences("my_public_shared_prefs", MODE_WORLD_READABLE);
which you can freely retreive from another app with the following:
Context context = createPackageContext("my_target_app_package", Context.CONTEXT_IGNORE_SECURITY);
SharedPreferences prefs = context.getSharedPreferences("my_public_shared_prefs", MODE_WORLD_READABLE);
Make sure you don't store any private information there because it's WORLD readable, which means you and anyone else can read that data.
To finish, if you want to retrieve the SharedPreferences of your old app you will need to update the old application with a SharedUserId in the Manifest file.
Android APplications runs in their own sandbox, so one application can not access data of other activity. BUt If an application want to share some of its data, this can be achieved by ContentProvider.
See Content Provider: http://developer.android.com/guide/topics/providers/content-providers.html
I have an appliaction which will show a welcome window only the first time the user starts the app. What would be the best way to store this boolean variable (i.e. "isFirstTime") to phone storage? Should I use Shared Preferences or Internal Storage?
The docs say that if I use Internal Storage my "preference" file will automatically get removed upon uninstallation which is quite handy.
I want a clean, simple and fast solution.
Yes, I'd recommend using Shared Preferences. Basically you could put a shared preference with a key of "isFirstTime" and a type of boolean set to false. Then in your main activity do something like:
getBoolean (isFirstTime, true);
This, if it can't find isFirstTime will give you true, allowing you to do an if-statement based on the result.
I agree that SharedPreferences would probably be the most "clean, simple, and fast solution" that you are looking for. SharedPreferences are also deleted when the application is uninstalled.
Are the shared preferences associated with the App deleted when the app is removed?
SharedPreferences are your best option for this.
I basically want to be able to have multiple SharedPreferences files for my app. The names of these will be based off of a string entered by the user. Then when the user wants to restore, I want to create a popup that will allow them to pick from all available SharedPreferences files. Is there a way to see what SharedPreferences files are in the directory? Or is there a better way to store this?
TIA
You could save a csv list of SharedPreferences as a SharedPreference...
But I think if you are going to have multiple configurations, better approaches would be either a small SqliteDatabase or ".ini" files in the internal storage.
If you insisted on using SharedPreferences as the storage mechanism, what you could do is use the default shared preferences (get it with SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);) to store a list of the preference files created (using something like the putStringSet method at http://developer.android.com/reference/android/content/SharedPreferences.Editor.html#putStringSet%28java.lang.String,%20java.util.Set%3Cjava.lang.String%3E%29). You can then retrieve them later and generate the popup list.