【问题标题】:How to save thw current WebView URL on app close and load it back when next the app is reopen? [closed]如何在应用程序关闭时保存当前的 WebView URL 并在下次重新打开应用程序时将其加载回来? [关闭]
【发布时间】:2019-03-30 12:40:12
【问题描述】:

如何在应用关闭时保存 android studio Web 视图的当前 URL,并在下次打开应用时加载该 URL。

【问题讨论】:

    标签: java android cookies webview


    【解决方案1】:

    您可以通过共享偏好来做到这一点。 这不是最好的方法,但易于使用: 首先从您的 webview 中获取 url

    String currentUrl = webView.getUrl();
    

    然后使用共享首选项保存它:

    SharedPreferences prefs = getSharedPreferences("YOUR_TAG", Context.MODE_PRIVATE);
    prefs.edit().putString("URL_TAG", currentUrl ).apply();
    

    现在字符串存储在共享首选项中,您可以在 应用程序以这种方式重启:

    SharedPreferences prefs = getSharedPreferences("YOUR_TAG", Context.MODE_PRIVATE);
    String savedUrl = prefs.getString("URL_TAG", null);
    

    最后将 url 设置为您的 webview:

    webview.loadUrl(savedUrl);
    

    请确定,YOUR_TAG 和 URL_TAG 仍然具有相同的字符串值 保存和接收。否则找不到存储的字符串值

    您可以找到更多关于共享偏好的信息 on this postoffical android documentation

    希望我能帮上忙!

    【讨论】:

    • 。我应该在创建时实现此代码吗?当应用程序首次运行时,没有保存的 URL,我将如何处理。
    猜你喜欢
    • 2013-11-11
    • 1970-01-01
    • 2012-04-06
    • 1970-01-01
    • 1970-01-01
    • 2011-07-16
    • 1970-01-01
    • 2021-09-14
    • 1970-01-01
    相关资源
    最近更新 更多