1

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);
        }
    }
}
Community
  • 1
  • 1
OmnicronCS
  • 31
  • 1
  • 7

2 Answers2

3

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());
Arthur Attout
  • 2,701
  • 2
  • 26
  • 49
  • Yes it is, on developer.android https://developer.android.com/reference/android/content/SharedPreferences.Editor.html#putString(java.lang.String, java.lang.String) – Arthur Attout Apr 14 '17 at 06:51
1

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

Bopmaster
  • 61
  • 6