Skip to content

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

Description

@jim-daf

WebFragment.shouldOverrideUrlLoading (app/src/main/java/com/meteocool/ui/map/WebFragment.kt:286) hands every non-meteocool URL off to a fresh startActivity:

override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
    if (Uri.parse(url).host!!.contains("meteocool.com")) {
        return false
    }
    Intent(Intent.ACTION_VIEW, Uri.parse(url)).apply {
        startActivity(this)
    }
    return true
}

If the URL resolves to no installed Activity (no browser, an oddly-formed link, a custom scheme like mailto: on a device with no mail client, etc.) startActivity throws ActivityNotFoundException, which is uncaught here and brings down the map fragment. This is the U04 pattern from the WebView posture notes.

Suggested fix

Wrap the launch in a try { ... } catch (ActivityNotFoundException) { ... } and fall back to a short Toast so the fragment survives:

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

A PR with this change plus a cant_open_link string is open at #93.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions