How to control the Android WebView history/back stack?

后端 未结 7 1973
半阙折子戏
半阙折子戏 2021-02-04 10:49

I am trying to figure out a way to handle the WebView back stack similar to how the Android web browser handles it when the back button is pressed from within my own app\'s WebV

相关标签:
7条回答
  • 2021-02-04 10:58

    this snippet work for me, try it:

    @Override
    public void onBackPressed() {
        BlankFragment fragment = (BlankFragment)
                getSupportFragmentManager().findFragmentByTag("webView");
        if (fragment.canGoBack()) {
            fragment.goBack();
        } else {
            super.onBackPressed();
        }
      }
    
    0 讨论(0)
  • 2021-02-04 11:00
    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;
        }
    }
    
    0 讨论(0)
  • 2021-02-04 11:03

    I had a similar problem with yours and found how to figure it out. Here is my answer.

    When I click the first link(www.new.a) it automatically redirects other link(mobile.new.a). Usually the links redirect two or three, and my solution have been worked on almost every redirect links. I hope this answer help you out with annyoing redirecting links.

    I finally figured out that. You need a WebViewClient with four APIs. There are shouldOverrideUrlLoading(), onPageStarted(), onPageFinished(), and doUpdateVisitedHistory() in the WebViewClient. All the APIs you need is API 1 so don't worry about.

    Here is my answer. Check out that! :)

    0 讨论(0)
  • 2021-02-04 11:04
    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);
    }
    

    This shouldOverrideUrlLoading method should return false if you want to handle the link in your webview rather than system browser, and do not call view.loadUrl(url), your webview will load url automatically if you return false. The history will be not correct if you load url manually.

    0 讨论(0)
  • 2021-02-04 11:05

    Detect when url loading is triggered by the user, and add those to the backstack instead.

    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() + "\t\t 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();
        }
    }
    
    0 讨论(0)
  • 2021-02-04 11:05

    The correct answer is to return false on shouldOverrideUrlLoading() as described in https://stackoverflow.com/a/8644978/5022858 and https://developer.android.com/reference/android/webkit/WebViewClient.html. This prevents redirects from being added to the history list.

    0 讨论(0)
提交回复
热议问题