如何在按下后重新加载而不是重新加载旧的Webview页面?

时间:2018-07-05 22:24:32

标签: android webview android-emulator localhost restore

悲剧: 我正在尝试在App Webview中设置两个本地主机服务器之间的通信。因此,当单击第二个网页上的按钮时,我会将一些日志和数据发送回第一个网页。按下“后退”按钮时,该日志/数据应显示在第一个网页上。

问题: Webview总是在按下时重新加载第一个网页。这样可以防止将日志从第二个网页url添加到第一个网页。

注意:除App WebView外,它下面的html可以在模拟器/手机/台式机Chrome浏览器上正常运行。因此,Html / Js代码应该没问题。

第一个html页面: //我正在从第二个网页调用第一个html(index.html)的FeatureAPI

function FeatureAPI(url) {
      console.log(url);
      alert("Feature initialized = " + url);
      this.FeatureInitialize = function(value) {
          document.body.innerHTML = document.body.innerHTML + "<br>" + "FeatureInitialize: " + value;
      }
      this.FeatureGetValue = function(element) {
          console.log(element);
          document.body.innerHTML = document.body.innerHTML + "<br>" + "FeatureGetValue: " + element;
          return "FeatureGetValue from Feature";
      }
      this.FeatureSetValue = function(element, value) {
          console.log(value);
          document.body.innerHTML = document.body.innerHTML + "<br>" + "FeatureSetValue: " + element + "=" + value;
      }
  }

//Logic for second url to open on button click
function openSecondUrlOnButtonClick() {
      window.open("http://emulatorlocalhost/abc.html", 'height=200,width=150');
  }

//clip of view
<div class="centered">
    <button id="openSecondUrlOnButtonClick" class="btn info" onclick="openSecondUrlOnButtonClick()">Open new url</button>
  </div>

第二个html页面:

/*******************************************************************************
 **
 ** Function getAPI()
 ** Inputs:  none
 ** Return:  If an API object is found, it's returned, otherwise null is returned
 **
 ** Description:
 ** This function looks for an object named API, first in the current window's
 ** frame hierarchy and then, if necessary, in the current window's opener window
 ** hierarchy (if there is an opener window).
 **
 *******************************************************************************/
function getAPI()
{
    var theAPI = findAPI(window);
    if ((theAPI == null) && (window.opener != null) && (typeof(window.opener) != "undefined"))
    {
        theAPI = findAPI(window.opener);
    }
    if (theAPI == null)
    {
        alert("Unable to find an API adapter");
    }
    return theAPI
}

/*******************************************************************************
 **
 ** Function findAPI(win)
 ** Inputs:  win - a Window Object
 ** Return:  If an API object is found, it's returned, otherwise null is returned
 **
 ** Description:
 ** This function looks for an object named API in the supported window hierarchy
 **
 *******************************************************************************/
function findAPI(win)
{
    while ((win.API == null) && (win.parent != null) && (win.parent != win))
    {
        findAPITries++;

        // Note: 500 is a number that comes from the new IEEE API standard.
        //       See rational
        if (findAPITries > 500)
        {
            alert("Error finding API -- too deeply nested.");
            return null;
        }

        win = win.parent;
    }
    return win.API;
}

Android Fragment已实现了webview,此webview会从按钮单击中拦截第二个url,如下代码所示,以便加载到同一webview中。 (我知道我不应该在同一网页视图中加载,但是我在这里寻找建议吗?)

public static final String INSIDE_TEST_URL = "http://emulatorlocalhost/abc.html";

 @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest webResourceRequest) {
            final String url = webResourceRequest.getUrl().toString();
            if (url.contains(INSIDE_TEST_URL)) {
                return false;
            } else if(mCurrentUrl != null && url != null && url.equals(mCurrentUrl)) {
                //view.stopLoading();
                //view.goBack();
                return false;
            }
            mCurrentUrl = url;
            return super.shouldOverrideUrlLoading(view, webResourceRequest);
        }

0 个答案:

没有答案