Skip to content

fix(webview): guard external link launch in WebFragment with try/catch#93

Open
jim-daf wants to merge 1 commit into
meteocool:mainfrom
jim-daf:fix-webview-startactivity
Open

fix(webview): guard external link launch in WebFragment with try/catch#93
jim-daf wants to merge 1 commit into
meteocool:mainfrom
jim-daf:fix-webview-startactivity

Conversation

@jim-daf
Copy link
Copy Markdown

@jim-daf jim-daf commented May 15, 2026

Closes #92.

`WebFragment.shouldOverrideUrlLoading` hands non-meteocool URLs to a plain `startActivity(Intent.ACTION_VIEW, ...)`. If the device has no Activity that can handle the URL (some custom scheme, a link a browser cannot open, `mailto:` on a device with no mail client, etc.) the call raises `ActivityNotFoundException` and brings down the map fragment.

Change

Wrap the launch in try/catch and fall back to a Toast so the fragment keeps working:

```kotlin
try {
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url)))
} catch (e: ActivityNotFoundException) {
Toast.makeText(requireContext(), R.string.cant_open_link, Toast.LENGTH_SHORT).show()
}
```

Adds a `cant_open_link` string for the fallback message. The happy path (a URL with a viewer installed) is unchanged.

shouldOverrideUrlLoading sent every non-meteocool URL to
startActivity(Intent.ACTION_VIEW) without protection. If the device
has no app that can handle the URL (no browser/viewer for the scheme,
custom intents like mailto with no mail client, etc.) it raises
ActivityNotFoundException and crashes the map fragment.

Wrap the launch in try/catch and fall back to a short Toast so the
fragment keeps working.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

WebFragment crashes on external link with no viewer (uncaught ActivityNotFoundException)

1 participant