Solved: Android Studio webView.canGoBack() not working

by Violeta León

   If you are starting developing Android Apps its possible to find problem such as: "webView.canGoBack() not working" and you'll read many solutions but nothing may seems to work. Let me explain, how I solved it and where it worked.

   I made a tabbed activity with eleven fragments. My back button kept quiting the app instead of going back to the previous page into the webview. I thought if I added the funtionality of the back button inside the fragment it will solved it. But it did not.

   All you have to do its open the mainactivity.java, search the line: "public class MainActivity extends AppCompatActivity" and add this code: 

@Override
    public void onBackPressed(){
        WebView wv = (WebView)findViewById(R.id.mWebView);
        if(wv.canGoBack()){
            wv.goBack();
        } else {
            super.onBackPressed();
        }
    }

Just as simple as that! all your fragments with webview will go back with the phone or table back button. 




Comentarios