SharedPreferences aren't resetting using remove(); or clear(); - android

I'm trying to reset my SharedPreferences, but neither clear(), nor remove() work.
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
editor = preferences.edit();
for (int t = 0; t < homeTiles.size(); t++) {
editor.remove(homeTiles.get(t).getmFunction() + "_tile_pos"); //this isnt running
editor.remove(homeTiles.get(t).getmFunction() + "_tile_vis"); //this is getting ecexuted
}
editor.apply();
I also checked the preference file and it did'nt change.

try this
for (int t = 0; t < homeTiles.size(); t++) {
editor.remove(homeTiles.get(t).getmFunction() + "_tile_pos").remove(); //this isnt running
editor.remove(homeTiles.get(t).getmFunction() + "_tile_vis").remove(); //this is getting ecexuted
}

Related

Sharedpreferences not saving data when app restart

I'm trying to make "add favorites" button in my app. I can send and get value with sharedpreferences properly, but when I restart the app, favorites are empty again. I guess I need override OnPause method, but I couldn't apply properly. I need help, thanks in advance.
PLAYING RADIO FRAGMENT
public static int incrementedValue = 0;
add_favorites_button= (Button) view.findViewById(R.id.add_favorites_button);
add_favorites_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
SharedPreferences settings = getActivity().getSharedPreferences("PREFS", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("radio_link"+ incrementedValue, radio_play_link);
editor.putString("radio_name" + incrementedValue, radio_name);
editor.putString("listener_number" + incrementedValue, listener_number);
//editor.clear();
editor.commit();
incrementedValue++;
}
});
FAVORITES FRAGMENT
final List<String> radio_name_list = new ArrayList<>();
final List<String> radio_link_list = new ArrayList<>();
final List<String> listener_numbers = new ArrayList<>();
for (int i=0; i<PlayRadioFragment.incrementedValue; i++) {
SharedPreferences settings = getActivity().getSharedPreferences("PREFS",0);
radio_name_list.add(settings.getString("radio_name" +i, ""));
radio_link_list.add(settings.getString("radio_link" +i, ""));
listener_numbers.add(settings.getString("listener_number" +i, ""));
}
...
then I show them in listview
...
for (int i=0; i<PlayRadioFragment.incrementedValue; i++)
After restarting the app (or Fragment), this incrementedValue will be back to what it was initialized with, probably 0, so you won't load anything from SharedPreferences (not entering the for-loop at all).
Try something like
for (int i=0; i<Integer.MAX_VALUE; i++) {
String s = settings.getString("radio_name" +i, "")
if (TextUtils.isEmpty(s))
break;
} else {
radio_name_list.add(s);
}
You probably also want to avoid overwriting the old favorites after the app restart, so you have to set the incrementedValue after loading the favorites like
incrementedValue = radio_name_list.size();
This is happening because on restarting app the variable named incrementedValue gets value once again as 0 and in for loop conditional statement is not full filling and that's why loop is not executing as expected. To resolve this issue you can do one more thing you need to save the value of incrementedValue in shared preference as well and in for loop testing statement get the updated value from shared preference. Hope that helps you

SharedPreferences resetting after process kill

Right i know there's a lot of these types of questions on here but i'm yet to find one that works.
For the past 30 minutes or so i've playing with SharedPreferences and so far the values save with no problems at all but when you kill the process by either long pressing the back button or using a task manager and you go to restart the app, it'll go back to default values.
Now the default value is 50, as i'm doing my Equalizer and have a flat/reset button
So yeah, basically how do i save the values and keep them saved?!
Here's my various attempts:
Attempt 1:
private int isFirstTime()
{
SharedPreferences sp = getSharedPreferences("sliders", MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
for (int i = 0; i < num_sliders && i < MAX_SLIDERS; i++) {
editor.putInt("sliders", sliders[i].getProgress());
editor.putInt("bass_boost", bass_boost.getProgress());
editor.commit();
}
return num_sliders;
}
Attempt 2:
public int saveProgress(){
SharedPreferences sp = getSharedPreferences("sliders2", MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putInt("sliders", sliders[0].getProgress());
editor.putInt("sliders2", sliders[1].getProgress());
editor.putInt("sliders3", sliders[2].getProgress());
editor.putInt("sliders4", sliders[3].getProgress());
editor.putInt("sliders5", sliders[4].getProgress());
editor.putInt("sliders6", sliders[5].getProgress());
editor.putInt("sliders7", sliders[6].getProgress());
editor.putInt("sliders8", sliders[7].getProgress());
editor.putInt("sliders9", sliders[8].getProgress());
editor.putInt("sliders10", sliders[9].getProgress());
editor.putInt("bass_boost", bass_boost.getProgress());
editor.commit();
return min_level - max_level;
}
Attempt 3:
public int getProgress(){
SharedPreferences sp = getSharedPreferences("sliders", MODE_PRIVATE);
for (int i = 0; i < num_sliders && i < MAX_SLIDERS; i++) {
num_sliders = sp.getInt("sliders", 0);
}
num_sliders = sp.getInt("bass_boost", 0);
return num_sliders;
}
Attempt 4:
public int getProgress2(){
SharedPreferences sp = getSharedPreferences("sliders", Activity.MODE_PRIVATE);
num_sliders = sp.getInt("sliders1", -1);
num_sliders = sp.getInt("sliders2", -1);
num_sliders = sp.getInt("sliders3", -1);
num_sliders = sp.getInt("sliders4", -1);
num_sliders = sp.getInt("sliders5", -1);
num_sliders = sp.getInt("sliders6", -1);
num_sliders = sp.getInt("sliders7", -1);
num_sliders = sp.getInt("sliders8", -1);
num_sliders = sp.getInt("sliders9", -1);
num_sliders = sp.getInt("sliders10", -1);
num_sliders = sp.getInt("bass_boost", -1);
return num_sliders;
}
Any help will be hugely appreciated as i need to get the apps update rolled out!
Thank you.
I know this is an old question, but I just wasted two hours on this, might as well give my answer in case anyone encounters the same problem.
What I had to do to have settings properly survive across app kills / device reboots was:
Use the editor to REMOVE the old value
Commit
Put the new value
Commit
So, for example:
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.remove(Constants.PREF_STARTED_EXPERIMENTS);
editor.commit();
editor.putStringSet(Constants.PREF_STARTED_EXPERIMENTS, experiments);
editor.commit();
Hope it saves someone some time.
The problem could be null values being saved into your preferences file. Please try if this answer solves your problem.

Passing strings to list and deleting them using shared preferences

i wish to, in one activity, put strings in the sharedpreferences, so then, in another activity, i get those strings, put them in a array and display them sequentially. I managed to do this, but i don`t have any idea how can i delete one specified string when asked. These strings will just be scattered on the shared preferences,and i dont know how to keep track of them. I can pass this unique int id to each element. I tried to use LinkedList, but i cannot pass this kind of structure as a shared preferences. I did not managed to make Gson work also. Please help.
Method that gets the string and put on shared preferences:
public void makefavorites(String[] a, String[] b, int id)
{
int idfinal = id%10;
idfinal = idfinal+1;
a[idfinal] = b[idfinal] +"\n" + "\n"+ a[idfinal];
SharedPreferences prefs = getSharedPreferences("Favorites", Activity.MODE_PRIVATE);
Editor edit = prefs.edit();
int temp = prefs.getInt("favorites_size", 0);
edit.putInt("favorites_size", temp+1);
edit.putString("array_" + prefs.getInt("favorites_size", 0), a[idfinal]);
edit.commit();
refreshfavorites();
}
Method that gets those strings, put on array and display it:
public void refreshfavorites()
{
SharedPreferences prefs = getSharedPreferences("Favorites", Activity.MODE_PRIVATE);
//GETS THE ARRAY SIZE
int size = prefs.getInt("favorites_size", 0);
String[] array = new String[size];
for(int i=0; i<size; i++){
array[i] = prefs.getString("array_" + i, null);
}
}
you have to use editor.remove method to delete specific value from arraylist..
public void removeArray(String[] list)()
{
SharedPreferences.Editor editor = mSharedPrefs.edit();
int size = list.length();
for (int i = 0; i < size; i++) {
editor.remove("favorites_size"+i);
}
editor.commit();
}
i hope its useful to you..

How to store one string and arrayvalue in sharedpreference?

I have array values of up[]={0,0,0,0,0} and view="adult" thesetwo value i want to store and retrieve in from sharedpreference how to do that...
Assuming that you have a list of preferences for you app called MY_PREFS, I'd do this:
SharedPreferences settings = getSharedPreferences(MY_PREFS, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("arrayLength",up.size());
for(int i=0; i<up.size(); i++){
editor.putInt("up"+String.valueOf(i), up[i]);
}
editor.putString("view", "adult");
To retrieve them do:
SharedPreferences settings = getSharedPreferences(MY_PREFS, 0);
int arraySize = settings.getInt("arrayLength");
int up[] = new int[arraySize];
for(int i=0; i<arraySize; i++){
up[i] = settings.getInt("up"+String.valueOf(i));
}
String view = settings.getString("view");
you can do by simple way, like this
SharedPreferences settings = getSharedPreferences("pref_name", 0);
SharedPreferences.Editor editor = settings.edit();
String values="";
for(int i=0; i<yourArray.length; i++){
values+=","+Integer.toString(yourArray[i]);
}
editer.putString("array",values);
editor.putString("view", "adult");
To get those values,
SharedPreferences settings = getSharedPreferences("pref_name", 0);
String[] strArray=settings.getString("array").split(",");
int[] yourArray=new int[strArray.length];
for(int i=0;i<strArray.length;i++){
yourArray[i]=Integer.toParseInt(strArray[i]);
}
Well as far as I have seen you can't directly store an array in shared preferences. You can however use a for loop to save an int with an increasing name and have it call it back the same way when you need it and store it into another array.
for(int i=0; i<numInYourArray; i++){
editor.putInt("up"+i, up[i]);
}
If you aren't sure on how to use Shared Preferences in general look here

sharred preference null pointer exception

//Activity-- mTweetApp.SetCaseInfo(type,teet); //storing in another class
public void SetCaseInfo(String PatientType,ArrayList arr){
// All objects are from
SharedPreferences settings =setSharedPreferences(DEALSPOTR_PREFS,0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("arrayLength",arr.size());
for(int i=0; i<=arr.size(); i++){
editor.putInt("Teethsselected"+String.valueOf(i), (Integer) arr.get(i));
}
editor.putString("view", PatientType);
for(int i=0; i<=arr.size(); i++){
System.out.println("Teethsselected-----"+(Integer) arr.get(i)+"type--->"+PatientType);
}
}
public void getCaseInfo() {
SharedPreferences settings =getSharedPreferences(DEALSPOTR_PREFS, 0);
int arraySize = settings.getInt("arrayLength", 0);
int teeth[] =new int[arraySize];
for(int i=0; i<arraySize; i++){
teeth[i] = ettings.getInt("Teethsselected"+String.valueOf(i),0);
}
String type =settings.getString("view"," ");
}
how to fix this?
You don't commit your changes. put editor.commit(); into your set method.
First, when you write your pref values, no call of commit().
Second, the pref file is not written to /data/data/com.package.App/shared_prefs/pref_file.xml, hence when reading the SharedPreferences object is NULL.
SharedPreferences.Editor modifications should be commited.
//Your code
editor.putString("view", PatientType);
for(int i=0; i<=arr.size(); i++){
System.out.println("Teethsselected-----"+(Integer) arr.get(i)+"type--->"+PatientType);
}
//ADD THIS
editor.commit();
SharedPreferences settings =setSharedPreferences(DEALSPOTR_PREFS,0);
instaed of above line, try using
SharedPreferences settings =getSharedPreferences(DEALSPOTR_PREFS,0);
and don't forget editor.commit() line once the values are added..

Categories

Resources