I am curious about sharedPreferences and my real question is Variable stored using sharedPreferences in one activity can be fetched in second activity but can i get the value of variable again in third activity ? does it provide this usage? if not How can i achieve this task?
Mr. Z,
You can store a static reference of SharedPreferences in the activity it corresponds to (when the activity's onCreate is called, instantiate the reference). Otherwise, the only thing I can think of for globally retrieving data in a program is just reading files from the scard storage that you create.
If something is unclear or incomplete, please comment and I'll try to help!
You can use it as many times you wish, unless you explicitly clear it by code or your app's local data is removed (like when uinstalling or clearing local storage).
Be aware that SharedPreferences should not be used to store important information as it might get lost occasionally.
Related
In my Android application there are some Boolean variables which I want to access anywhere in the app, by declaring them as public static boolean I can access anywhere but when keeping my app long time in background or if the user clears the memory(by clean master app) then my static variables get reset and again if the user goes to the app from stack then there will be an issue since all my variables got reset to default values.
Instead I am planning to store those boolean vars to Sharedprefs, everytime when I want I will access them from sharedprefs. Everytime reading from sahredprefs is the best way or not?does it effect any performance if I had large number of booleans? Can anyone suggest me what would be the best way of handling this situation.
SharedPreferences is the way to go.
Variables saved to SharedPreferences are saved in an xml file. I never had problems in terms of performance when it comes to large set of preferences. I think you should be fine.
SharedPreferences are in fact an XML, they are fast for few values and you can load/save them on the OnPause/OnResume events on the Application class.
If you need to store large amount of data consider using JSON or even better an ORM like SugarORM (greenDAO is good too).
Should I use a singleton class or preferences to store the data that is shared in the program. For example, say the user can enable or disable location tracking. The state of tracking has to be stored and shared with the SERVICE that handles the location changes. In such case, should I use a singleton class or preferences to persist the choice for a background SERVICE ? Please also relevant supporting points to your choice.
The purpose of SharedPreference is exactly store preference settings and status values.
Singleton restricts the instantiation of a class to one object, that is a different matter.
If you want to avoid the lost of data when the application is closed, if you don't use Preferences you have to create manually a method to store and load your data on a file, so SharedPreference is the best choice, if you want to share an object only during the program execution you can use singleton to restrict its instantiation, however if the value to store is a String or a primitive using Preference also in this case has no side effects but is a bit dirty programming choice.
Singleton - Saves Data in variables and get deleted when Application is Destroyed.
Shared Prefs - Save Data outside of your Application so u can later use even application is destoryed.
SharedPreferences, Service, and Singletons are substantially different. None of them can be used INSTEAD of the other one. If at ANY situation you though you can use one instead of the other, you can be sure you did not understand their application very well.
As one example difference, SharePrefs are saved in the disc so they are persistent, while Singletons are not.
I am working with an android app and am looking for a way to establish some global preferences that will be later accessible to all all activities/classes in my app. This is similar to what i know some people end up using the AppDelegate in objective c programming. The answers i have seen on the net suggest that i either extend from Application or save the data to SharedPreferences. I would like to know if there is a way to combine the two approches so that in my app, i can keep calling properties of a Preferences object (such as Preferences.getName()) and also persist the data to SharedPreferences so that, in case Android decides to restart my application and i loose all the data in my Preferences object, i can still get it back from SharedPreferences and repopulate the Preferences object. Can anyone please let me know how this can be done and if it is viable?
See my answer at https://stackoverflow.com/a/13673178/338479
In short, I create a "singleton" class to hold my preferences, and the data persists as long as the application stays in memory. If the application is killed by the system, the singleton class silently reloads it later.
It's also possible to do this by extending Application class, but conventional wisdom holds that there's no real advantage to doing it this way.
Given that a Service may be running even when the launching activity is destroyed,
and also that data is passed usually using the extra bundle along the intent,
I wonder if the Service is able to directly access the activity's preferences.
My guess is no, it cannot. But it would save me writing a chunk of code if it can do it.
Assuming that You're asking about SharedPreferences. If so, then documentation is clear (by link above):
Note: currently this class does not support use across multiple
processes. This will be added later.
So, currently, if Your service is running in a separate process it's not possible. In that case I would suggest to store Your preferences not in a SharedPreferences, but in some base which could be accessible via specific ContentProvider.
P.S. Actually I tried access SharedPreferences from multiple processes and it has worked (at least in my case), but I've decided to use another way because of the documentation mentioned in the answer.
If you mean SharedPreferences then you can just call getSharedPreferences with the same name.
Ddms tells that, when I recall my class called in the past, it performs an onCreate() instead of onResume() that I expected...
I noticed that values that I stored in variables of my class in this case are lost and are null.
I presume that Android decide to do so to free memory resources (isn't it?).
I know that I could use Sharedpreferences to store data in a persistent way and then retrieve... But this is a really dirty way, in my opinion.
So, my question: how to have variables' values preserved also after an onDestroy() (I think?) that Android decided automatically?
Android will terminate your process at any time when you have no visible activities. For example, the user might go into Settings and terminate your app.
Static data members (my interpretation of your "variables of my class" description) are only meant to be caches, at best. They are no substitute for a persistent data model, whether you use a database, an XML file, a JSON file, or whatever.
So, if you want "variables' values preserved", save them someplace persistent.
You might find this page on data storage helpful. If your data is primitive, SharedPreferences are the recommended route. (Why do you think they are dirty?) If you need to store an object, you can use internal storage, as documented on that page.
http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState(android.os.Bundle)
If you don't like SharedPreferences, then you might want to look into Content Providers Even though Content Providers share data across applications, they also provide functionality for you to store persistent data in SQLlite and files that are available only to your app. In this case data stored in this fashion will be available even after closing and restarting your app.
You can save dynamic state by passing name:value pairs or serializable objects using the Android Architecture and the methods onSaveInstanceState and onRetainNonConfigurationState. You can persist state as explained in the other answers by writing to prefs and doing database writes.
I've been using custom Application class to store data over application life line.
How to declare global variables in Android?