Last active
March 9, 2020 16:47
-
-
Save jtmuller5/ab4a40e6c978fe2c23112129b7d2aa3b to your computer and use it in GitHub Desktop.
Back Navigation for WebView in Fragment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | |
super.onViewCreated(view, savedInstanceState) | |
val myWebView: WebView = view.findViewById(R.id.your_webView) | |
myWebView.webViewClient = WebViewClient() | |
myWebView.loadUrl("https://www.google.com/") | |
myWebView.setOnKeyListener { v, keyCode, event -> | |
if (keyCode == KeyEvent.KEYCODE_BACK && myWebView.canGoBack()) { | |
myWebView.goBack() // Navigate back to previous web page if there is one | |
nested_scroll.scrollTo(0, 0) // Scroll webview back to top of previous page | |
} | |
true | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment