Checking the URL the Android WebView Back & Forward Buttons Will Navigate To
When developing an app with a WebView on Android, the Back and Forward buttons can be a concern. When you press these buttons, the URL they navigate to might lead to a sensitive(?) screen. So I put together a way to check the URL.
import android.webkit.WebView;
import android.webkit.WebBackForwardList;
// WebView
WebView wv = (WebView) findViewById(R.id.webview);
// history list
WebBackForwardList historyList = wv.copyBackForwardList();
// Back button URL
String backTargetUrl = historyList.getItemAtIndex(historyList.getCurrentIndex() - 1).getUrl();
// Forward button URL
String forwardTargetUrl = historyList.getItemAtIndex(historyList.getCurrentIndex() + 1).getUrl();
Based on the URLs obtained using the API above, you can implement whatever logic you need.
That’s it!
Leave a comment