如何在webview中重新加载网址时保存上次状态

时间:2017-09-19 12:53:59

标签: android webview android-webview

1.Webview有goback方法,点击后可以保存网页状态。

2.我的布局是一个Linearlayout,它有两个按钮顶部的webview。

  1. 每个按钮都匹配一个网址。我还在webView.goBack()方法

  2. 中添加onKeyDown
  3. 第一个网址有一个表单,第二个网址没有表单

  4. 在点击第二个按钮后,我在第一个网址的表单中写了一些信息 然后,我点击第一个按钮,但是网址重新加载。但是如果我点击后退 表单信息保存。 我想保存那些表单信息,就像我点击后退按钮,它可以保存最后的表单信息

    我想在上次保存表单信息,怎么做?

    我的代码在这里

     private void checkone(String url) {
    
        if (!Netutils.isNetworkAvalible(MyApplication.mcontext)) {
            fl.removeAllViews();
            webView.loadUrl(url);
            webView.setVisibility(View.GONE);
            webView.removeAllViews();
            fl.setVisibility(View.VISIBLE);
            fl.addView(mErrorView);
    
        } else {
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    fl.setVisibility(View.GONE);
                    webView.setVisibility(View.VISIBLE);
                    fl.removeAllViews();
                }
            },1000);
    
    
    
    
    
    
                Map extraHeaders = new HashMap();
                extraHeaders.put("Referer", "www.emiaoqian.com");
                webView.loadUrl(url, extraHeaders);
    
    
                settings = webView.getSettings();
                settings.setBuiltInZoomControls(true);
                settings.setUseWideViewPort(true);
    
                settings.setJavaScriptEnabled(true);
                settings.setSaveFormData(true);
                settings.setCacheMode(WebSettings.LOAD_DEFAULT);
                settings.setAppCachePath(MyApplication.mcontext.getCacheDir().getPath());
    
                webView.setWebChromeClient(new ChomeClient(this) {
                    @Override
                    public void onProgressChanged(WebView view, int newProgress) {
    
    
                        currentProgress = pg1.getProgress();
                        if (newProgress >= 100 && !isAnimStart) {
    
                            isAnimStart = true;
                            pg1.setProgress(newProgress);
    
                            startDismissAnimation(pg1.getProgress());
                        } else {
    
                            startProgressAnimation(newProgress);
                        }
    
                    }
    
                    @Override
                    public void onReceivedTitle(WebView view, String title) {
                        super.onReceivedTitle(view, title);
    
                        if (title.contains("404")) {
                            showErrorPage();
                        }
                    }
                });
    
    
                settings.setAllowFileAccess(true);
                settings.setAllowContentAccess(true);
    
                settings.setDomStorageEnabled(true);
    
    
                String ua = webView.getSettings().getUserAgentString();
                webView.getSettings().setUserAgentString(ua.replace("Android", "emiaoqian"));
    
    
    
    
                webView.setWebViewClient(new WebViewClient() {
    
                    @Override
                    public void onPageStarted(WebView view, String url, Bitmap favicon) {
                        pg1.setVisibility(View.VISIBLE);
                        pg1.setAlpha(1.0f);
    
    
    
                    }
    
                    @Override
                    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                        handler.proceed();
                    }
    
    
    
                    @Override
                    public boolean shouldOverrideUrlLoading(WebView view, String url) {
    
    
    
                        if (url.startsWith("weixin://wap/pay?")) {
                            Intent intent = new Intent();
                            intent.setAction(Intent.ACTION_VIEW);
                            intent.setData(Uri.parse(url));
                            //startActivityForResult(intent,233);
                            startActivity(intent);
                            return true;
                        }
                        return super.shouldOverrideUrlLoading(view, url);
    
                    }
    
    
    
                    @Override
                    public void onPageFinished(WebView view, String url) {
                        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
                            CookieSyncManager.getInstance().sync();
                        } else {
                            CookieManager.getInstance().flush();
                        }
    
    
                        if (!(url.equals(Constants.LOGIN)) && !(url.equals(Constants.LOGINOUT))) {
                            if (homeTb != null && supportActionBar != null) {
                                supportActionBar.setDisplayHomeAsUpEnabled(false);
                                supportActionBar.setDisplayShowHomeEnabled(false);
                                homeTb.setTitle("");
                            }
                            radiogroup.setVisibility(View.VISIBLE);
                            nouserl.setVisibility(View.VISIBLE);
    
                        } else if (url.equals(Constants.LOGIN) || url.equals(Constants.LOGINOUT)) {
                            inittoolbar2();
                            radiogroup.setVisibility(View.GONE);
                            nouserl.setVisibility(View.GONE);
    
                        }
    
    
                        if (url.startsWith("https://wx.tenpay.com")) {
                            String newurl = url.substring(url.length() - 12, url.length());
                            LogUtil.e("--", newurl);
    
    
    
                        }
    
    
    
                    }
    
                    @Override
                    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                        showErrorPage();
                    }
    
                    @Override
                    public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                        showErrorPage();
                    }
    
    
                });
    
                fixDirPath();
                // webParentView = (LinearLayout) webView.getParent(); 
            }
    
    
    }
    

1 个答案:

答案 0 :(得分:0)

您可以设置:

[assembly: ExportRenderer(typeof(CustomEntryControl), typeof(MyEntryRenderer))]
namespace MyApp.Droid
{
    public class MyEntryRenderer : EntryRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged(e);

            if (Control != null)
            {
                Control.SetBackgroundColor(global::Android.Graphics.Color.LightGreen);
            }
        }
    }
}

还要保存在指定应用缓存路径:

mWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT); // WebSettings.LOAD_CACHE_ELSE_NETWORK

告诉我结果。