Justin Pfenning
Justin Pfenning

Reputation: 543

Android canGoBack always false

I have an Android app where I am trying to display some custom back/forward buttons in the title to let the user know they can go back or forward. My idea was to toggle the opacity and set whether or not they are enabled if the user can go back or forward in the WebView.

Inside my fragment's OnCreateView method is:

public View onCreateView(LayoutInflater inflater,
                         @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.web_view_fragment, container, false);
    Toolbar toolbar = (Toolbar) v.findViewById(R.id.webview_toolbar);
    TextView titleText = (TextView) toolbar.findViewById(R.id.webTitle);
    titleText.setText("SBN Viewer");
    webView = (WebView) v.findViewById(R.id.webview);
    setOnClickListeners(toolbar);
    this.toolbar = toolbar;
    WebViewClient webclient = new MyWebViewClient(toolbar, getActivity());
    webView.setWebViewClient(webclient);
    return v;
}

And the important part of MyWebViewClient() is:

@Override
public void onPageFinished(WebView view, String url) {
    LogHelper.d(this.getClass().getSimpleName(), "Before super. Can go back? " + view.canGoBack());
    super.onPageFinished(view, url);
    LogHelper.d(this.getClass().getSimpleName(), "After super. Go Back? " + view.canGoBack());
    enableBecauseFinished(view);
    if(view.canGoBack())
        LogHelper.d(this.getClass().getSimpleName(), "We Can Go Back Here...");
}

I have checked numerous times to make sure I am not overwriting the MyWebViewClient with a new instance, and I am not. I have also moved this OnPageFinished into just the fragment's onCreate as like this:

webView.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
            LogHelper.d(this.getClass().getSimpleName(), "Before super. Can go back? " + view.canGoBack());
            super.onPageFinished(view, url);
            LogHelper.d(this.getClass().getSimpleName(), "After super. Go Back? " + view.canGoBack());
    }
    });

No matter what I do, this always prints out that it can never go back. false is always printed out even though I click on link after link on a website and navigate the app (well, forward navigate anyway). Thanks for the help.

Upvotes: 1

Views: 1145

Answers (1)

Justin Pfenning
Justin Pfenning

Reputation: 543

Take it from me folks - always clean you project if you are experiencing wonky things like this. Always. Always. Always. DOH!

Upvotes: 1

Related Questions