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.
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();
If you can help me I will be very grateful,
i want to launch an intent only in first load of my android application and save same settings. To save my settings I use SharedPreferences :
SharedPreferences preferences = getSharedPreferences("prefName", MODE_PRIVATE);
SharedPreferences.Editor edit= preferences.edit();
edit.putBoolean("isFirstRun", false);
edit.commit();
Thanks a lot.
you can do something like this
SharedPreferences preferences = getSharedPreferences("prefName", MODE_PRIVATE);
SharedPreferences.Editor edit= preferences.edit();
if(preferences.getBoolean("isFirstRun", true)){
edit.putBoolean("isFirstRun", false);
edit.commit();
//Do your stuff for first Run
}else {
}
In your activity A, you should check in your shared preference like:
if(!pref.getBoolean("isFirstRun", false)){
//Load activity B
// put your code for updating shared pref as you mention above.
}else{
//Load activity C
}
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();
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);
I've on Activity A the following code:
SharedPreferences sharedPreferences = getSharedPreferences("prefs", 0);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("nome", nome.getText().toString());
editor.commit();
I've on Activity B the following code:
SharedPreferences sharedPreferences = getSharedPreferences("prefs", 0);
String a = sharedPreferences.getString("nome", "");
nomeMediador.setText(a); //TextBox
Can anyone tell why it's not showing (the value saved on Activity A) on Activity B?
Refer the below code
Activity A
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("nome", nome.getText().toString());
editor.commit();
Activity B
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
String a = sharedPreferences.getString("nome", "");
nomeMediador.setText(a);
its better use getter and setter with the help of string.xml file, you will never get this kind of problem. for that matter you may check this blog:
http://sspower3.blogspot.in/2011/11/sharedpreferences-in-eazy-way.html