On click of a button i want to remove the data that i have store on Login Activity.I have to remove this from diffrent activity how can we delete this .
This is how i have saved the values.
public void saveInformation(String username, String password) {
SharedPreferences shared = getSharedPreferences("SelfTrip", MODE_PRIVATE);
SharedPreferences.Editor editor = shared.edit();
editor.putString("username", username);
editor.putString("password", password);
editor.commit();
}
You can remove all entries from your shared preferences file with the following:
getSharedPreferences("SelfTrip",Context.MODE_PRIVATE).edit().clear().commit();
Try this
public void onClick(View arg0) {
SharedPreferences myPrefs = getSharedPreferences("SelfTrip",
MODE_PRIVATE);
SharedPreferences.Editor editor = myPrefs.edit();
editor.clear();
editor.commit();
finish();
}
And this is how you can delete values from the SharedPreferences:
SharedPreferences preferences = getSharedPreferences("Mypref", 0);
Editor e = preferences.edit();
e.remove("yourkey");
e.commit();
Simply use the remove() method of the Editor and remove a value with your key.
To remove a value from SharedPreferences use the remove() method with the appropriate key (e.g. username):
SharedPreferences shared = getSharedPreferences("SelfTrip", MODE_PRIVATE);
SharedPreferences.Editor editor = shared.edit();
editor.remove("username").commit();
Related
how can I edit value of sharedPreference from other Activity. I try this codes by I'm getting an error on the context part.
if(stars == 2){
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = scorepref.edit();
editor.putInt("keyhelloworld", stars);
editor.commit();
Intent fromHW = new Intent(HelloWorldGameActivity.this, LessonActivity.class);
startActivity(fromHW);
}
try this.
Pass context from first activity.
To call Shared preferences
SharedPreferences sharedPreferences = context.getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
To call editor
SharedPreferences.Editor editor = context.getSharedPreferences("myPrefs", Context.MODE_PRIVATE).edit();
You can use SharedPreferences as follows. Since sharedpreferences is persistent, you can use the same implementation anywhere in your application to access it.
SharedPreference sharedPreferences = getApplicationContext().getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);
sharedPreferences.edit().putString("key", value).apply();
Why is in the following example the foo false?
mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
mPrefs.edit().putBoolean("myValue",true);
mPrefs.edit().commit();
boolean foo = mPrefs.getBoolean("myValue",false);
I think you have to write in this way
mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
mPrefs.edit().putBoolean("myValue",true).commit();
boolean foo = mPrefs.getBoolean("myValue",false);
When you call edit() the first time, you are not saving the boolean and so it does not exist when you retrieve it.
mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
mPrefs.edit().putBoolean("myValue",true).commit();
boolean foo = mPrefs.getBoolean("myValue",false);
Store your values by--->
SharedPreferences prefs =PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("myValue",true);
editor.apply();
to get your result--->
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
Boolean b = sharedPreferences.getboolean("myValue","");
Save value in shared preferences:
SharedPreferences settings =getSharedPreferences("AppName", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString(key, value);
editor. putBoolean(key, value);
editor.commit();
get value from shared preferences:
SharedPreferences settings = getSharedPreferences("AppName", 0);
String value=settings.getString(key, "");
boolean value=settings.getBoolean(key,false);
You should Edit the SharedPreference object through Editor
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt("Key", value);
editor.commit();
In my current app I made a set of passpoints and want there to be an option to delete them from a different activity. I saw a few questions similar to this on stackoverflow and tried to follow their instructions and tinker a little but nothing worked. What is wrong with my code?
First Activity: ( the activity with the preferences i'm trying to delete )
public void setDefaults() {
SharedPreferences mPrefs = getSharedPreferences(RESETT_PASSPOINTS, 0);
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString(RESETT_PASSPOINTS,Identifier);
editor.commit();
}
Second Activity : ( the activity i'm trying to delete from )
public void deleteDefaults(){
SharedPreferences mPrefs = getSharedPreferences(Activity1.RESETT_PASSPOINTS, 0);
String str = mPrefs.getString(Activity1.RESETT_PASSPOINTS, Activity1.Identifier);
if (str.equals(Activity1.Identifier)){
SharedPreferences.Editor editor = mPrefs.edit();
editor.clear();
editor.commit();
}
}
Also, I know I could use the intents ".put extra" way of doing it but I don't want to start the activity i'm deleting from
Try replacing:
if (str.equals(Activity1.Identifier)){
SharedPreferences.Editor editor = mPrefs.edit();
editor.clear();
editor.commit();
}
For this:
if (str.equals(Activity1.Identifier)){
SharedPreferences.Editor editor = mPrefs.edit();
editor.remove(Activity1.RESETT_PASSPOINTS).commit();
}
Hope it helps!
public void updateDefaults(){
SharedPreferences mPrefs = getSharedPreferences(RESETT_PASSPOINTS, 0);
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString(RESETT_PASSPOINTS,"OTHER_VALUE");
editor.commit();
}
Best option for you is to update it and check its value again.
I am using shared preferences for saving data and accessing it from another activity. I have used suggested methods but they don't seem to work.
Code:
private static String Module_Pref="ModulePreference";
Activity A:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = prefs.edit();
editor.putString(key, value);
editor.commit();
Activity B:
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
tempValue= sharedPreferences.getString(Module_Pref, "empty");
Activity C:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = prefs.edit();
editor.putString(key, value);
editor.commit();
Here if we run first time then pass through A, set nosave, then if we go to activity C then save the data.
What is wrong with this code.I am getting a null. I looked at file explorer also where pref file isn't saved.
use this method to create a sharedPreference and then access it with this same name from any where in any activity within the same app
SharedPreference sp;
sp = getApplicationContext().getSharedPreferences(My_PREFERENCE,
context.MODE_PRIVATE);
Editor e = sp.edit();
e.put(key,value);
e.commit();
and when getting the same sharedPreference in another activity use this method
SharedPreference sp;
sp = getApplicationContext().getSharedPreferences(My_PREFERENCE,
context.MODE_PRIVATE);
sp.get(key,value);
`
Your should use for entering data like this:
editor.putString(Module_Pref, "value that you want to store");
editor.commit();
Now For getting that String you can use this after storing the value:
sharedPreferences.getString(Module_Pref, "empty");
sharedPreferences.getString(Module_Pref, "empty");
means you want to retrieve the "value" for the key- Module_Pref, if there is none, by default it will return -> "empty" .
So either,
editor.putString(Module_Pref, value);
Or
sharedPreferences.getString(key, "empty");
where "key" is same as the key in:
editor.putString(key, value);
Activity A:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = prefs.edit();
editor.putString(key,value );
editor.commit();
Activity B:
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
tempValue= sharedPreferences.getString(key, "emptyValue");
SharedPreferences sp = getSharedPreferences("main",0);
SharedPreferences.Editor ed = sp.edit();
ed.putString("personEmail",personEmail);
ed.putString("personName",personName);
ed.commit();
this is used for saving values in shared pref & for accessing it use ->
SharedPreferences sp = getSharedPreferences("main",0);
sp.getString("personEmail",null);
sp.getString("personName",null);
I'm unable to delete SharedPreferences from the app on click event.
Here is how I'm storing the value into UserInfoActivity SharedPreferences:
SharedPreferences notificationCountSP = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor notificationEditor = perkBalance.edit();
notificationEditor.putString("notificationCount",notificationCountValue);
notificationEditor.commit();
And here is how I'm trying to clear all data in SharedPreferences from MainActivity:
SharedPreferences clearNotificationSP = getSharedPreferences(
"notificationCountSP", 0);
SharedPreferences.Editor editor = clearNotificationSP.edit();
editor.remove("notificationCount");
editor.clear();
editor.commit();
Please tell what am I doing wrong with this.
Any kind of help will be appreciated.
SharedPreferences notificationCountSP = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor notificationEditor = notificationCountSP.edit();
notificationEditor.putString("notificationCount", notificationCountValue);
notificationEditor.commit();
notificationEditor.remove("notificationCount");
notificationEditor.commit();
or
SharedPreferences clearNotificationSP = getSharedPreferences("notification_prefs", 0);
SharedPreferences.Editor editor = clearNotificationSP.edit();
editor.putString("notificationCount", notificationCountValue);
editor.commit();
editor.remove("notificationCount");
editor.commit();
First solution uses the default application preferences file, and the second a custom notification_prefs file.
You are using PreferenceManager.getDefaultSharedPreferences to store but retrieving from getSharedPreferences("notificationCountSP"). They are different files unless you set the default one to "notificationCountSP".
You can do it like below
SharedPreferences userPref = getSharedPreferences(
MyActivity.SHARED_PREFERENCES_FILENAME,MODE_PRIVATE);