如何保存的WebView状态,并在Android的棒棒糖恢复? [英] How to save WebView state and restore it in Android Lollipop?

查看:513
本文介绍了如何保存的WebView状态,并在Android的棒棒糖恢复?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题是问多次,它有用来工作的答案。近日在文档中表示,他们已经取消了这一功能出于安全原因。只有一些有限的事物可被恢复为web视图状态。我曾尝试多种方法可以做到这一点,但每次都刷新web视图状态,并没有显示视图内容。

我要做些什么?

我有我想要检索后超时用户内容的web应用程序(如果他们输入他们的凭据是正确的)。

 

我迄今所做的?

1 - 我已经尝试了所有解决方案,包括以下方法:

  @覆盖
保护无效的onSaveInstanceState(捆绑outState){
    webView.saveState(outState);
}
@覆盖
公共无效的onCreate(最终捆绑savedInstanceState){
   super.onCreate(savedInstanceState);
   的setContentView(R.layout.blah);
   的WebView的WebView =(的WebView)findViewById(R.id.webview);
   如果(savedInstanceState!= NULL)
      webview.restoreState(savedInstanceState);
   其他
      webview.loadUrl(URLData)
}

此外清单文件

<$p$p><$c$c>android:configChanges=\"keyboardHidden|orientation|screenSize|screenLayout|uiMode|screenSize|smallestScreenSize\"

2 - 我试图表明我超时活动作为一个对话框,它不评估工作的凭据,如果我只是关闭它,但如果我评价并关闭对话框它将重新创建web视图。

任何帮助将是AP preciated。


解决方案

最后我想出的解决方案。有没有办法挽救的WebView的状态,并且拥有所有的内容,所以我们需要prevent重装或重新创建web视图。为了让我们的活动的一个实​​例,其中包含web视图我们应该加上以下code到我们的清单文件:

 &LT;活动...
        机器人:launchMode =singleInstance
        机器人:alwaysRetainTaskState =真正的&GT;

这时如果通过你的应用程序,你需要完全重新创建实例刷新web视图,可以使用结束();如果它是活动的内部或者如果你是在另一个活动,并要重新创建的网页视图活动,阅读的此链接做到这一点。

This question is asked multiple times and it has answers that used to work. Recently in documentation said, they have removed this feature for security reasons. Only some limited things can be restored for the webView state. I have tried multiple ways to do it but every time it refreshes the webView state and it doesn't show the webView content.

What I want to do?

I have a web app that I want to retrieve the user content after time-out (If they input their credentials correct).

What I have done so far?

1- I have tried all solutions including the following approaches:

@Override
protected void onSaveInstanceState(Bundle outState) {
    webView.saveState(outState);
}
@Override
public void onCreate(final Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.blah);
   WebView webview = (WebView)findViewById(R.id.webview);
   if (savedInstanceState != null)
      webview.restoreState(savedInstanceState);
   else
      webview.loadUrl(URLData)
}

Also manifest file

android:configChanges="keyboardHidden|orientation|screenSize|screenLayout|uiMode|screenSize|smallestScreenSize"

2- I tried to show my Time-Out activity as a dialog and it worked if I just dismiss it without evaluating the credentials but if I evaluate and dismiss the dialog it recreates the webView.

Any help would be appreciated.

解决方案

Finally I figured out the solution. There is no way to save the state of WebView and have all of the content, so we need to prevent reloading or recreating the webView. In order to have one instance of our activity that contains webView we should add the following code to our Manifest file:

<activity ...
        android:launchMode="singleInstance"
        android:alwaysRetainTaskState="true">

Then if through your application you needed to recreate the instance entirely to refresh the webView, you can use finish(); if it's inside of the activity or if you are in another activity and you want to recreate your webView activity, read this link to do it.

这篇关于如何保存的WebView状态,并在Android的棒棒糖恢复?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆