Home > Net >  How can i use canGoBack() on GeckoView
How can i use canGoBack() on GeckoView

Time:09-18

Below is the code for android Webview

public void onBackPressed() {
    if (webView.canGoBack()) {
        webView.goBack();
    } else {
        super.onBackPressed();
    }
}

How can i use similar code in Geckoview or how can i achieve similar process on Geckoview?

CodePudding user response:

GeckoView provides this piece of information through NavigationDelegate.onCanGoBack but doesn't store it, so you need to create a class to do that that implements NavigationDelegate.

First create your own delegate class:

private class MyNavigationDelegate implements GeckoSession.NavigationDelegate {
    public boolean canGoBack = false;

    @Override
    public void onCanGoBack(GeckoSession session, boolean canGoBack) {
        this.canGoBack = canGoBack;
    }
}

private MyNavigationDelegate navigationDelegate = new MyNavigationDelegate();

Then set the delegate somewhere where you create your GeckoSession:

session.setNavigationDelegate(navigationDelegate);

And then you can implement onBackPressed like so:

public void onBackPressed() {
    if (navigationDelegate.canGoBack) {
        session.goBack();
    } else {
        super.onBackPressed();
    }
}

  • Related