0

I developed a simple webview app. I added a button which should act as the back button, so users can navigate back one page.

The button is initially hidden and should only show if a back history exists. How can I realise this?

The code is simple:

backButton = findViewById(R.id.backButton);
backButton.setEnabled(blizzView.canGoBack());

but where do I have to call this? How is the "some site loaded" event called?

Update:

I tried to apply @Murats answer, but nothing happens:

private WebView blizzView;  // my webview
private Button backButton;

public void onPageFinished(WebView view, String url)
{
    backButton = findViewById(R.id.backButton);
    backButton.setEnabled(blizzView.canGoBack());

    if (blizzView.canGoBack()) {
        backButton.setVisibility(View.VISIBLE);
    } else {
        backButton.setVisibility(View.INVISIBLE);
    }

}
Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30
Black
  • 18,150
  • 39
  • 158
  • 271

2 Answers2

2

You can use WebView#canGoBack to find out if there is a backstack. You can check for that after the page is loaded e.g. in WebViewClient#onPageFinished

Murat Karagöz
  • 35,401
  • 16
  • 78
  • 107
0

It works like this:

blizzView.setWebViewClient(new WebViewClient() {      //
    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);

        backButton = findViewById(R.id.backButton);
        backButton.setEnabled(blizzView.canGoBack());

        if (blizzView.canGoBack()) {
            backButton.setVisibility(View.VISIBLE);
        } else {
            backButton.setVisibility(View.INVISIBLE);
        }

    }
});
Black
  • 18,150
  • 39
  • 158
  • 271