我在解決方案下面放置here,得到一個Can not resolve方法put(java.lang.string,java.lang.string)我是試圖避免在方向變化時避免丟失我的webview中的數據,而無需手動處理方向更改。無法解析方法put(java.lang.string,java.lang.string)
我的代碼下面貼:
@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);
}
}
}
怎麼樣使用putString? https://developer.android.com/reference/android/content/SharedPreferences.Editor.html#putString(java.lang.String,%20java.lang.String) –
有沒有這樣的類型'java.lang.string' 。 –