Safe Browsing in Android WebView: choosing back to safety and testing your policy
When Safe Browsing detects a threat, the app must explicitly choose WebView’s response. If the product’s policy requires returning to safety, an automatic proceed contradicts that policy. We will explain how to keep protection enabled, distinguish framework APIs from AndroidX, and test the app’s decision using your own synthetic data. These checks show which response your code chooses, but do not verify the live threat classifier.
What the app must decide
Safe Browsing is enabled by default; Android recommends keeping it enabled at all times. Starting with Android 8.0, API 26, setSafeBrowsingEnabled() lets you toggle protection for an individual WebView. Under the policy discussed here, disabling protection must not become a way to bypass loading restrictions.
The default warning lets users continue loading the URL or return to the previous safe page. With custom handling, responsibility is divided between WebView’s threat notification and the app’s response. The app chooses how to respond to the event; it does not override the validity of the classification. Returning to safety should also not be described as a guaranteed return to the app’s home page.
Initialization: completion does not mean success
The guide recommends waiting for Safe Browsing initialization before calling loadUrl(). The AndroidX example first checks START_SAFE_BROWSING, then calls WebViewCompat.startSafeBrowsing(). The feature check determines which path is available; it does not promise support on every device.
In the documentation example, safeBrowsingIsInitialized is set to true when the callback arrives, including when success=false. It is therefore useful for the app to track completion of the attempt separately from its successful outcome. Under the policy chosen here, loading is allowed only after success=true. Before the callback, when the result is false, or when START_SAFE_BROWSING is unavailable, the web session does not start and the user sees a custom unavailable screen. This is a product decision, not a universal fallback guaranteed by WebView.
Integrate framework APIs and AndroidX separately
The framework class SafeBrowsingResponse was introduced in API 27. WebView creates its instances and passes them to WebViewClient.onSafeBrowsingHit(). The framework adapter should only be connected on API 27 and later. Do not create SafeBrowsingResponse yourself: the documentation explicitly prohibits apps from doing so, and its constructor is deprecated.
The AndroidX example handles the event through SafeBrowsingResponseCompat and checks SAFE_BROWSING_RESPONSE_BACK_TO_SAFETY before calling backToSafety(true). Do not treat the framework class’s API level as a promise of AndroidX compatibility. Keep feature checks for AndroidX and define behavior separately for cases where those features are unavailable.
An explicit response: backToSafety and independent reporting
When a threat is detected, the app must provide a response through showInterstitial(boolean), proceed(boolean), or backToSafety(boolean). A policy requiring a return to safety selects backToSafety(report): this corresponds to pressing the back-to-safety button. By contrast, proceed(report) corresponds to choosing to visit the unsafe site and must not be called automatically in this branch.
The report parameter controls reporting: true enables Safe Browsing reports. It does not choose between proceeding and returning. Pass the reporting setting separately from the backToSafety decision. Calling the method with true does not, by itself, prove that a report was actually sent.
A fictional example and tests for the response choice
Consider a completely fictional app called Leaflet that displays its own educational articles. Separate its threat-response policy from a custom response-adapter interface. In production, the adapter calls the appropriate API; in tests, a fake adapter only records calls. The synthetic event contains fictional data owned by the team; there is no need to open malicious pages or create a SafeBrowsingResponse.
The following checks are original suggested acceptance tests, not an official Android procedure. With the feature supported, pass an event to the policy and verify exactly one backToSafety call and no proceed calls. Repeat with reporting=true and reporting=false: the selected method must remain the same, and the recorded report argument must match the setting. These observable criteria test the app’s decision and the independence of reporting, but not WebView’s actual navigation.
Testing readiness and unavailable features
To test initialization, use your own fake feature provider and a controllable callback result. Loading must not start before the callback. With success=false, the app remains in the unavailable state. The positive control is success=true with START_SAFE_BROWSING supported: loading your own ordinary test page is then allowed. Check the app’s state and the loading call; a single successful HTTP response does not prove that every state transition is correct.
Separately simulate the absence of START_SAFE_BROWSING and the absence of SAFE_BROWSING_RESPONSE_BACK_TO_SAFETY. Leaflet’s predefined policy prevents the web session from starting, displays a custom unavailable screen, and makes neither unsupported calls nor automatic proceed calls. Verify this before loading. This scenario does not define how to complete a callback already received when the response is unsupported: the obligation to respond to a threat detection remains, and silently ignoring it cannot be considered a complete solution.
Implementation priorities and limits of the results
First, check that the manifest configuration and WebView settings do not disable Safe Browsing. Then distinguish successful readiness from initialization completion, connect the appropriate adapter, and enforce an explicit backToSafety response through tests. Check for unavailable features before starting the web session. This sequence makes the product’s policy observable without replacing it with a general promise of security.
Synthetic checks do not verify the real Safe Browsing service, actual navigation, report delivery, or compatibility across all devices. Their result is a verified choice made by your app under specified conditions. CheckMyApp offers a free audit for your website or the web part of your product; the mobile app checks described here are not an offer of a mobile scanning service.