I have a strange problem. I have never had it before. When I try to save int value to my SharedPreference and then restore in other Activity. Value is always 0 even if I save there other value (for example: 1);
private String Number;
private String Profile;
and then saving values (in this case "1") to SharedPreferences in first Activity:
SharedPreferences a = FirstActivity.this.getSharedPreferences("a", MODE_PRIVATE);
SharedPreferences.Editor prefsEditorProfiles = a.edit();
prefsEditorProfiles.putInt(Profile, 1);
prefsEditorProfiles.putInt(Number, 1);
prefsEditorProfiles.commit();
then restore SharedPreferences in other Activity:
SharedPreferences a = SecondActivity.this.getSharedPreferences("a", MODE_PRIVATE);
int ab = a.getInt(Number, 0);
And application shows me 0 instead of 1. My other SharedPreferences works great. I don't know where is the problem.
I'd check what's the value of the Number and Profile variables you declared... you are using their values as keys, so if they have conflicting names, you might be overwriting one setting with the other even though the code looks right.
I'd recommend replacing this:
private String Number;
private String Profile;
With this:
private final String NUMBER = "Number";
private final String PROFILE = "Profile";
And then using those constants when setting/getting your preference value.
Do you ever set a value for
"Number" and "Profile"?
If not then that is your problem -those strings are null.
Please try to use
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
rather than using
SharedPreferences prefs = getActivity().getSharedPreferences ("PREFS_KEY", 0);
when Storing int in shared preference
I've been trying for a while to use putInt like you but it always give an error.
prefsEditorProfiles.putInt(Number, 1);
by just changing a.putInt to a.putString and retrieving it with a.getString I was able to have the correct value.
so, I guess there should be something wrong with putInt and getInt.
Anyway, try that also to have the correct value you need to continue for application.
Related
Is it possible to get a sharedprefence data from MainActivity_A and fetch it only to MainActivity_B and the other activities like MainActivity_C and MainActivity_D cannot access the data that has been fetched to MainActivity_B?
Well It totally depends on you wheather you wanna get that data or not..once you store data in shared preference with key then just get that data in the specific activity you want Thats It !!
For Example in Activity X you save the Shared Preference value like this
public static final String SHARED_PREFS = "sharedPrefs";
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS,MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putString("Key",Value);
Then in Activity A you want to use this value then you can do it like this
SharedPreferences pref;
pref = getSharedPreferences(SHARED_PREFS, Context.MODE_PRIVATE);
String Value = pref.getString("Key");
In This way the value will be stored in variable String
And if you don't want the value in other activity then just don't call it !!
I need to generate notification using BroadcastReceiver and need to save some data in the BroadcastReceiver class.
So, I used SharedPreference. But not able to save data inSharedPreference`.
SharedPreferences pref_date;
public static final String MyPREFERENCES = "MyPrefs";
pref_date = context.getSharedPreferences(MyPREFERENCES,context.MODE_PRIVATE);
_ed = pref_date.edit();
String t="hello";
_ed.putString(_date,t);
_ed.putString(flag,"0");
_ed.commit();
Yes, SharepPreference is just a kind of file that has key-value pairs to store and retrieve. You can access it from anywhere within the application. Even from the Services created by your application.
Hard to tell w/o looking into the source code, but most possibly you forgot to invoke the Editor's apply() or commit() method, like so:
mSharedPreferences.edit().putInt("some_int", 0).apply();
As per the documentation, this is how you should write to your SharedPreferences:
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("yourKey", "stringToSave");
editor.commit();
And this is how you read:
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
String defaultValue = "defaultValue";
String yourSavedString = sharedPref.getString("yourKey", defaultValue);
This is a working copy/paste example. Keep in mind that "yourKey" is used to identify your value, since SharedPreferences is key/value pair, so they have to match both when you read and write.
Hey guys I am unable to save a boolean value using SharedPreferences. The value is ALWAYS true for some reasons. Here is how I save the value:
public static void setSharedPreference(Context ctx, String keyValue, boolean value){
SharedPreferences sp = ctx.getSharedPreferences(Constants._preferencesName, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putBoolean(keyValue,value);
editor.commit();
}
And this is how I get it back:
public static boolean getBooleanPreference(Context ctx, String keyValue){
boolean prefValue;
SharedPreferences sp = ctx.getSharedPreferences(Constants._preferencesName, ctx.MODE_PRIVATE);
prefValue = sp.getBoolean(keyValue, false);
return prefValue;
}
What is wrong?!
Your code is syntactically correct, but I suspect you are passing different Context while saving than you are passing while reading from prefs. This will result in accessing different shared preferences storage. This is especially easy to step on if you are doing your writes and reads in different activities and decide to pass this as context. Unless there's a reason for doing so then you most likely want to reach your preferences from anywhere in your app then use always application context instead (getApplicationContext()).
Everything is correct in your code.
The ONLY possibility of a mistake is when you are calling these methods. Please use getApplicationContext() while putting and retrieving data.
And please do a "Clear data" for the app and start with a clean SharedPreference.
I need to extract the application data using SharedPreferences in Xamarin.Android.
Here is what I have tried in android.
public static void SetAuthentication(bool authenticationValue)
{
var localSettings = Application.Context.GetSharedPreferences ("Hello", FileCreationMode.Private);
localSettings.Edit ().PutBoolean ("ValidUser", authenticationValue).Commit ();
}
public static bool GetAuthentication()
{
var retValue = false;
object value;
var localSettings = Application.Context.GetSharedPreferences ("Hello", FileCreationMode.Private);
localSettings.GetBoolean ("ValidUser", out value);
}
But somehow I feel this is not the right approach.
Any guidance is appreciated.
Thanks
On the Android platform, preferences are stored in a text file in the private folders of the Android application - there are 2 ways to access these SharedPreferences data files.
You can either use the "default" file for the application, or you can use a named file, using any name you want.
This second approach is what you have chosen to do by calling GetSharedPreferences with a filename.
I would suggest to just use the "default" preferences. This way has some advantages:
the code is simpler
the preferences can be accessed more easily in a PreferenceActivity later on
To do this, create your ISharedPreferences instance using:
ISharedPreferences localSettings =
PreferenceManager.GetDefaultSharedPreferences (mContext);
Once you have your "default" shared preferences in this manner, the rest of your code stays the same.
For more info, check out related questions:
How do I use SharedPreferences in Xamarin.Android?
save android app string value after close xamarin
I agree with Richard so just to be clear and complete I would do like this:
public static void SetAuthentication(Context ctx, bool authenticationValue)
{
ISharedPreferences pref = ctx.GetSharedPreferences("your_app_preferences", FileCreationMode.Private);
ISharedPreferencesEditor edit = pref.Edit();
edit.PutBoolean("ValidUser", authenticationValue);
return edit.Commit();
}
public static bool GetAuthentication(Context ctx)
{
ISharedPreferences pref = ctx.GetSharedPreferences("your_app_preferences", FileCreationMode.Private);
return pref.GetBoolean("ValidUser"), null);
}
I hope this will make you feel happier.
Let's say i created a shared preference.
sp = this.getSharedPreferences("name",MODE_PRIVATE);
If i'm in another activity, is there a way i can find the name of sp and set it to a string?
you can access the sharedpreferences with the same method you have used. use a public static varibale to hold the name in a class.
Use a constant. a public final static String NAME = "the_name";. Then you can alway reference it as NameOfTheClassWhereYouPutIt.NAME
If you are looking to refer to a given shared preference in another activity, you can pass that String key of that preference to the new Activity via its Intent when creating it.
If you simply need to find a certain preference in any activity, you can iterate over all of the preferences, and get access to all the keys (and values) that way.
Assuming your shared preferences is in the variable mySharedPreferences, an example of getting all of the keys and vales would be:
Map<String,?> preferenceMap = mySharedPreferences.getAll();
for ( Map.Entry<String, ?> keyValPair : preferenceMap.entrySet() )
{
String key = keyValPair.getKey(); // this is your preference name
String val = keyValPair.getValue(); // this is your preference value
}