-3

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.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

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