How do I save the current URL of android studio web view when the app is closed and load that URL when next the app is open.
Asked
Active
Viewed 571 times
1 Answers
1
You could do it with shared preferences. This is not the best way, but easy to use: First get the url from your webview
String currentUrl = webView.getUrl();
Then save it with shared preferences:
SharedPreferences prefs = getSharedPreferences("YOUR_TAG", Context.MODE_PRIVATE);
prefs.edit().putString("URL_TAG", currentUrl ).apply();
Now the string is stored in shared preferences and you can get the value after an app restart on this way:
SharedPreferences prefs = getSharedPreferences("YOUR_TAG", Context.MODE_PRIVATE);
String savedUrl = prefs.getString("URL_TAG", null);
And last set the url to your webview:
webview.loadUrl(savedUrl);
Be sure, the YOUR_TAG and URL_TAG has still the same string value on saving and receiving. Otherwise the stored String value wont found
More information to shared preferences can you find on this post or on the offical android documentation
I hope I could help!

JonasPTFL
- 189
- 4
- 15
-
.Should I implement this code in on create. When the app is first run there is no saved URL so how will I handle that. – Tolu Olatubosun Mar 30 '19 at 14:29