返回片段时Android webview为空

返回片段时Android webview为空,android,android-fragments,android-webview,Android,Android Fragments,Android Webview,我使用webview显示web服务调用的数据。 如果数据显示在webview中,我将转到下一个片段,然后返回到上一个片段,webview为空。一旦数据加载到应用程序中,有没有办法保存它 webView.loadDataWithBaseURL(null, html,"text/html", "utf-8", null); “html”字符串包含html代码。试试这种方法 1) 在活动中创建此界面 public class JavaScriptInterface { private

我使用webview显示web服务调用的数据。 如果数据显示在webview中,我将转到下一个片段,然后返回到上一个片段,webview为空。一旦数据加载到应用程序中,有没有办法保存它

webView.loadDataWithBaseURL(null, html,"text/html", "utf-8", null);
“html”字符串包含html代码。

试试这种方法

1) 在活动中创建此界面

public class JavaScriptInterface {
        private Context context;

        public JavaScriptInterface(Context activiy) {
            this.context = activiy;
        }

        public void onVideoClick(String sourceAddress) {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.parse(sourceAddress), "video/3gpp");
            context.startActivity(intent);
        }
    }
2) 在您的代码中,在webview中加载数据之前先编写这些行

JavaScriptInterface jsInterface = new JavaScriptInterface(this);
webView.addJavascriptInterface(jsInterface, "JSInterface");

String newHtml = html.replaceAll("<video width=\"[0-9]*", "<video width=\"100\\%\" onclick=\"window.JSInterface.onVideoClick(src);\"");

webView.loadDataWithBaseURL(null, newHtml,"text/html", "utf-8", null);
JavaScriptInterface jsInterface=新的JavaScriptInterface(此);
addJavascriptInterface(jsInterface,jsInterface);

字符串newHtml=html.replaceAll(“发布您的相对代码webview.loadDataWithBaseURL(null,html,“text/html”,“utf-8”,null));html字符串包含html代码。我已将html sting保存在常量sting变量中。当我返回上一个片段时,将该字符串分配给webview。这对我来说很有用。感谢重播。