OmnicronCS
OmnicronCS

Reputation: 41

Cannot resolve method put(java.lang.string, java.lang.string)

I am following a solution placed here, and getting a Cannot resolve method put(java.lang.string, java.lang.string) I am attempting to avert to avert losing the data in my webview on orientation change without handling the orientation change manually.

my code is posted below:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    myWebView = (WebView) findViewById(R.id.webcontent);
    myWebView.getSettings().setJavaScriptEnabled(true); // enable javascript
    myWebView.loadUrl("file:///android_asset/Welcome.html");
    BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}

@Override
protected void onPause() {
    super.onPause();
    SharedPreferences prefs = context.getApplicationContext().
            getSharedPreferences(context.getPackageName(), Activity.MODE_PRIVATE);
    Editor edit = prefs.edit();
    edit.put("lastUrl",myWebView.getUrl());
    edit.commit();   // can use edit.apply() but in this case commit is better
}

@Override
protected void onResume() {
    super.onResume();
    if(myWebView != null) {
        SharedPreferences prefs = context.getApplicationContext().
                getSharedPreferences(context.getPackageName(), Activity.MODE_PRIVATE);
        String s = prefs.getString("lastUrl","");
        if(!s.equals("")) {
            myWebView.loadUrl(s);
        }
    }
}

Upvotes: 1

Views: 5571

Answers (2)

Bopmaster
Bopmaster

Reputation: 61

There's no put methods for SharedPreferences.Editor. The correct one should be

edit.putString("lastUrl",myWebView.getUrl());

You can find out more here SharedPreferences.Editor

Upvotes: 1

Arthur Attout
Arthur Attout

Reputation: 2916

Editor doesn't contain a method "put".

Because you want to put an url, you can use Editor.putString instead

So there you go

edit.putString("lastUrl",myWebView.getUrl());

Upvotes: 3

Related Questions