![]() |
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();
}
}
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.
Recommended lectures:
Recommended lectures:
- Android Studio 3.3 Development Essentials - Android 9 Edition: Developing Android 9 Apps Using Android Studio 3.3, Java and Android Jetpack.
- Android Studio 3.3 Development Essentials - Kotlin Edition: Developing Android 9 Apps Using Android Studio 3.3, Kotlin and Android Jetpack
- Learn all about how to make Android apps in this very detailed Android app development course.