others - 如何控制Android WebView的历史记录/后退?

问题与javascript重定向有关,下面是我的相关代码:


private class ArticleWebViewClient extends WebViewClient {


 @Override


 public boolean shouldOverrideUrlLoading(WebView view, String url) {


 view.loadUrl(url);


 return true;


 }


}



@Override


public boolean onKeyDown(int keyCode, KeyEvent event) {


 if (keyCode == KeyEvent.KEYCODE_BACK)


 {


 WebView wv = (WebView)findViewById(R.id.web);


 if (wv.canGoBack())


 {


 wv.goBack();



 return true;


 }


 }



 return super.onKeyDown(keyCode, event);


}



我无法禁用Javascript,因为该网站依赖Javascript ,

Android网络浏览器如何处理此问题?

时间:

你的onKeyDown应该如下所示


@Override


 public boolean onKeyDown(int keyCode, KeyEvent event)


 {


 if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {


 myWebView.goBack();


 return true;


 }


 return super.onKeyDown(keyCode, event);


 }



onCreate中声明

检测用户什么时候触发了url加载,并将它添加到backstack中。


private List<String> previous = new ArrayList<String>();


private String mLastUrl;


webview.setWebViewClient(new WebViewClient() {


 @Override


 public void onPageFinished(WebView view, String url) {


 Log.i("DebugDebug","OnPageFinished" + url);


 mLastUrl = url;


 super.onPageFinished(view, url);


 }


});



webview.setOnTouchListener(new View.OnTouchListener() {


 @Override


 public boolean onTouch(View view, MotionEvent motionEvent) {


 WebView.HitTestResult hr = ((WebView)view).getHitTestResult();


 if (hr != null && mLastUrl != null) {


 if (previous.isEmpty() || !previous.get(previous.size() - 1).equals(mLastUrl)) {


 previous.add(mLastUrl);


 }


 Log.i("DebugDebug","getExtra =" + hr.getExtra() +"tt Type =" + hr.getType());


 }


 return false;


 }


});



@Override


public void onBackPressed() {


 Log.i("DebugDebug","onBackPressed");


 int size = previous.size();


 if (size > 0){


 webview.loadUrl(previous.get(size - 1));


 previous.remove(size - 1);


 } else {


 super.onBackPressed();


 }


}




this snippet work for me, try this. 


@Override


public void onBackPressed() {


 BlankFragment fragment = (BlankFragment)


 getSupportFragmentManager().findFragmentByTag("webView");


 if (fragment.canGoBack()) {


 fragment.goBack();


 } else {


 super.onBackPressed();


 }


 }




private class ArticleWebViewClient extends WebViewClient {


 @Override


 public boolean shouldOverrideUrlLoading(WebView view, String url) {


 return false;


 }


}



@Override


public boolean onKeyDown(int keyCode, KeyEvent event) {


 if (keyCode == KeyEvent.KEYCODE_BACK)


 {


 WebView wv = (WebView)findViewById(R.id.web);


 if (wv.canGoBack())


 {


 wv.goBack();


 return true;


 }


 }


 return super.onKeyDown(keyCode, event);


}



shouldOverrideUrlLoading方法应该返回false,如果你想在web view中处理链接,如果手动加载url,历史记录将不正确。


boolean isGoBack = false; //back key is pressed



private long goBackTime = 0; //



@Override


public boolean onKeyDown(int keyCode, KeyEvent event) {


 if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {


 isGoBack = true;


 goBackTime = System.currentTimeMillis();


 mWebView.goBack();


 return true;


 }


 return super.onKeyDown(keyCode, event);


}



Webview mWebView.



setWebViewClient(new WebViewClient() { //setWebViewClient


 @Override


 public boolean shouldOverrideUrlLoading (WebView view, String url){


 if (!TextUtils.isEmpty(startUrl) && !startUrl.equals(url) && isGoBack && System.currentTimeMillis() - goBackTime < 600) {


 isGoBack = false;


 if (mWebView.canGoBack()) {


 mWebView.goBack();


 } else {


 finish(); //activity finish


 }


 } else {


 view.loadUrl(url);


 isGoBack = false;


 return true;


 }



 return false;


 }


 @Override


 public void onPageStarted (WebView view, String url, Bitmap favicon){


 super.onPageStarted(view, url, favicon);



 startUrl = url;


 }


}



...