我如何在 GeckoView 上使用 canGoBack()

How can i use canGoBack() on GeckoView

下面是 android Webview

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

如何在 Geckoview 中使用类似的代码或如何在 Geckoview 上实现类似的过程?

GeckoView 通过 NavigationDelegate.onCanGoBack but doesn't store it, so you need to create a class to do that that implements NavigationDelegate.

提供这条信息

首先创建你自己的委托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();

然后将委托设置在您创建 GeckoSession:

的地方
session.setNavigationDelegate(navigationDelegate);

然后你可以像这样实现onBackPressed

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