Skip to content Skip to sidebar Skip to footer

Shared Preferences Returning Last Value After Restarting?

I'm using shared preference to store dynamically created buttons and also using it to store the label of dynamically generated buttons after renaming them. Application works fine t

Solution 1:

You are using same key for all the buttons:

btn1.setText(input.getText());
Editoredit= prefs.edit();
edit.putString("key", btn1.getText().toString());
edit.commit();

You should create different keys like key1, key2 and key3 for each of these.

Solution 2:

Editoredit= prefs.edit();
edit.putString("key1", myButton.getText().toString());
edit.commit();

Editoredit= prefs.edit();
edit.putString("key2", btn1.getText().toString());
edit.commit();

Solution 3:

as its inside the for loop- (?)

edit.putString("key"+i, myButton.getText().toString());  

the "i" will be from the for loop then

Post a Comment for "Shared Preferences Returning Last Value After Restarting?"