SharedPreferences doesn't work android - android

I have a sharedPreferences object and SharedPreferencesEdit object but doesn't save anything
public void getPreferences(){
SharedPreferences sp = getSharedPreferences("MyData", Context.MODE_PRIVATE);
SharedPreferences.Editor spEditor = sp.edit();
spEditor.putString("us1_Name", us1_Name.getText().toString());
spEditor.commit();
}
public void setPreferences(){
SharedPreferences sp = getSharedPreferences("MyData", Context.MODE_PRIVATE);
us1_Name.setText(sp.getString("us1_Name", "DEFAULT"));
}
I call this methods when onStop and onResume call, but doesn't work for me.

You just need to exchange your preference methods from your getPreferences() to setPreferences() like
public void getPreferences(){
SharedPreferences sp = getSharedPreferences("MyData", Context.MODE_PRIVATE);
us1_Name.setText(sp.getString("us1_Name", "DEFAULT"));
}
public void setPreferences(){
SharedPreferences sp = getSharedPreferences("MyData", Context.MODE_PRIVATE);
SharedPreferences.Editor spEditor = sp.edit();
spEditor.putString("us1_Name", us1_Name.getText().toString());
spEditor.commit();
}
in your getPreferences() you need to get saved preferences value by using
sp.getString("us1_Name", "DEFAULT")
in your setPreferences() you need to saved preferences value by using
SharedPreferences.Editor spEditor = sp.edit();
spEditor.putString("us1_Name", us1_Name.getText().toString());
spEditor.commit();

first thing is that
1.) don't do any operations like sharedPreferance Edit / Database edit in onStop(); method
because it just call just for moment and it is only for do minor operation regarding activity
so please change it and do it in onPause();

Related

Editing shared preference in other activity

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();

Cannot get SharedPreferences value from one activity to another in Android

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);

Using shared preferences in between activities

I am trying to share a shared preference in between two activities of my project, but for some reason I am not able to pass the data.
I have Activity A which reads the shared preference and Activity B that reads as well as edit that shared preference.
Here is the code I am using to write the shared preference in Activity B:
SharedPreferences sharedPref = getSharedPreferences("myPrefs", Context.
MODE_WORLD_WRITEABLE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("theme", "black");
editor.commit();
and for reading in Activity A:
SharedPreferences sharedPref = getSharedPreferences("myPrefs", Context.
MODE_WORLD_WRITEABLE);
String theme=sharedPref.getString("theme","blue");
I have tried using the different modes, and it worked in Activity B in PRIVATE mode but it wasn't shared to activity A. For some reasons I think I have two different shared preferences(same name) for the two different activities. How do I use the same shared preference for both the activities ?
You can do simpler - in any activity:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
You will have the same prefs this way from anywhere.
http://developer.android.com/reference/android/preference/PreferenceManager.html#getDefaultSharedPreferences(android.content.Context)
To read shared datas in the second activity , change the mode :
from MODE_WORLD_WRITEABLE to MODE_WORLD_READABLE
SharedPreferences sharedPref = getSharedPreferences("myPrefs",Context.MODE_WORLD_READABLE);
String theme=sharedPref.getString("theme","blue");
// try this way
1. define SharedPreferences variable in SmartApplication class also define read and write method SharedPreferences
private SharedPreferences sharedPreferences;
#Override
public void onCreate() {
super.onCreate();
sharedPreferences = getSharedPreferences("yourAppName", MODE_PRIVATE);
}
public void writeSharedPreferences(String key, String value) {
SharedPreferences.Editor editor = readSharedPreferences().edit();
editor.putString(key, value);
editor.commit();
}
// write Shared Preferences
public void writeSharedPreferences(String key, boolean value) {
SharedPreferences.Editor editor = readSharedPreferences().edit();
editor.putBoolean(key, value);
editor.commit();
}
// write Shared Preferences
public void writeSharedPreferences(String key, float value) {
SharedPreferences.Editor editor = readSharedPreferences().edit();
editor.putFloat(key, value);
editor.commit();
}
public void writeSharedPreferences(String key, int value) {
SharedPreferences.Editor editor = readSharedPreferences().edit();
editor.putInt(key, value);
editor.commit();
}
// write Shared Preferences
public void writeSharedPreferences(String key, long value) {
SharedPreferences.Editor editor = readSharedPreferences().edit();
editor.putLong(key, value);
editor.commit();
}
// get Shared Preferences
public SharedPreferences readSharedPreferences() {
return sharedPreferences;
}

Remove shared prefrences Android

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();

how to reset all stored data store using shared preferences

I have created an activity where i have used shared preferences for storing data..now in another activity i have an reset button..when i click on the reset button the data store will be lost..so how that can be done..my code is
code in activity1:
public void writeToRegister()
{
// Write history data to register
SharedPreferences preferences1 = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor1 = preferences1.edit();
editor1.putInt("iHistcount", CycleManager.getSingletonObject().iHistCount);
for(int i=0;i< CycleManager.getSingletonObject().iHistCount;i++)
{
editor1.putLong("dtHistoryDate"+Integer.toString(i), CycleManager.getSingletonObject().dtHistory[i].getTime());
}
editor1.commit();
}
public void readFromRegister()
{
// Read history data from register
SharedPreferences preferences1 = getPreferences(MODE_PRIVATE);
CycleManager.getSingletonObject().iHistCount=preferences1.getInt("iHistcount", 0);
for(int i=0;i< CycleManager.getSingletonObject().iHistCount;i++)
{
Long x=preferences1.getLong("dtHistoryDate"+Integer.toString(i), 0L);
CycleManager.getSingletonObject().dtHistory[i]=new Date(x);
}
}
code for Activity 2:
Button pBtnReset = new Button(this);
pBtnNextMonth.setOnClickListener(pBtnReset OnClickListener);
Button.OnClickListener pBtnReset OnClickListenernew Button.OnClickListener()
{
public void onClick(View arg0)
{
}
};
so what i have to write in second activity reset button so that it clear the stored data
Get your Editor and call clear() something like this:
Edit: as the user DDoSAttack mentioned.
There are two ways of getting SharedPreferences
1: getting default SharedPreferences
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(con);
2: getting specific SharedPreferences
SharedPreferences prefs = Context.getSharedPreferences("FileName", Context.MODE_PRIVATE);
and here is how you'll clear it.
public void clear()
{
SharedPreferences prefs; // here you get your prefrences by either of two methods
Editor editor = prefs.edit();
editor.clear();
editor.commit();
}
its very easy..
yourEditor.remove(" thing you want to remove on start");
and then give must
yourEditor.commit();
If you want to wipe all the data in a preference file call clear() from the SharedPreferences.Editor instance
http://developer.android.com/reference/android/content/SharedPreferences.Editor.html#clear()
Use SharedPreferences.Editor clear() method.
See Documentation
SharedPreferences preferences = getPreferences(0);
SharedPreferences.Editor editor = preferences.edit();
editor.clear();
editor.commit();

Categories

Resources