user2582318
user2582318

Reputation: 1647

SharedPReferences are not restored (restore the default value only)

i have a new Activity called in this way:

 Intent i = new Intent(context, NewActivity.class);
....
                    context.startActivity(i);

At this Activity i have to restore/save some preferences based on the WebView position and a Seekbar(webview textzoom) positions/number.

so i made this:

public class LerLeiActivity extends AppCompatActivity  {
    SharedPreferences preferences;

    WebView wv;
    SeekBar skbar;

    HideOptionsMenu hdMenu;



    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.leituradalei);
        Intent intent = getIntent();
        wv = (WebView)this.findViewById(R.id.webView);
        skbar = (SeekBar) this.findViewById(R.id.seekFont);
        hdMenu = new HideOptionsMenu(this, wv);
        hdMenu.setWebViewURL(intent.getExtras().getString("path"));
        hdMenu.setSeekBarDefault(skbar);

        hdMenu.restoreLastPreference("preferences");

    }

and this HideOptionsMenu have this methods:

WebView wv;
    Context context;
    SharedPreferences preferences;
    SeekBar skb;
public HideOptionsMenu(Context c, WebView webview) {
    this.context = c;
        this.wv = webview;

    }
    public void savePreferences(String leiID) {
        preferences = context.getSharedPreferences(leiID, 0);
        preferences.edit().putInt("fontZoom", skb.getProgress());
        preferences.edit().putInt("scrollY", wv.getScrollY());
        preferences.edit().commit();
        Log.d("shared", "saving");
        Log.d("shared", "fontZoom " + skb.getProgress());
        Log.d("shared", "scrollY " + wv.getScrollY());
    }
    public void restoreLastPreference(String leiID) {
        preferences = context.getSharedPreferences(leiID, 0);
        int lastZoom = preferences.getInt("fontZoom", 100);
        int lastY = preferences.getInt("scrollY", 0);

        Log.d("shared", "restoring");
        Log.d("shared", "fontZoom " + lastZoom);
        Log.d("shared", "scrollY " + lastY);
       // wv.getSettings().setTextZoom(lastZoom);
        //wv.setScrollY(lastY);

    }

i cant understand why it keep loading the default values of stored, since i have this in the Log.d

//loading
D/shared﹕ restoring
D/shared﹕ fontZoom 100
D/shared﹕ scrollY 0

//saving
D/shared﹕ saving
D/shared﹕ fontZoom 167
 D/shared﹕ scrollY 447

//after closing the Activity and opening it again
 D/shared﹕ restoring
D/shared﹕ fontZoom 100
D/shared﹕ scrollY 0

Upvotes: 0

Views: 127

Answers (1)

TmKVU
TmKVU

Reputation: 3000

You should call the edit() function only once, it returns an Editor object. So you should do the following:

Editor editor = preferences.edit();
editor.putInt("fontZoom", skb.getProgress());
editor.putInt("scrollY", wv.getScrollY());
editor.commit();

From the documentation:

returns a new instance of the SharedPreferences.Editor interface, allowing you to modify the values in this SharedPreferences object.

So everytime you call edit() you get a new instance of which you do not commit the changes.

Upvotes: 1

Related Questions