Android WebView canGoBack is always true

I have a webview that loads social networking pages. I redefined the back button to call the goBack method in the WebView until canGoBack() returns false . Then it calls super.onBackPressed() . The problem is that Twitter and YouTube use Javascript in such a way that canGoBack() returns true unless you press the back button 4 times very quickly.

I tried to check the source url with the current url, but the page has a different url than the one that passes.

Does anyone know how to get the back button to invoke the goBack goBack method until you start and then call super.onBackPressed ?

+7
source share
1 answer

Well, this is not ideal, but you can track the history of navigation manually using an array (or just an integer that needs to be increased and decreased), and click on the current url every time it changes, and pull the last URL out of it when you press the back button, and if the array is empty (or an integer is 0), you call super.onBackPressed() .
To execute the code when the URL changes, you can override the boolean shouldOverrideUrlLoading (WebView view, WebResourceRequest request) WebViewClient ( link to the documentation ).
Thus, javascript of web pages cannot interfere with how you handle the back button.

0
source

All Articles