Search code examples
androidandroid-intent

Why does Android resolveActivity return null when Intent.createChooser works fine


my current android project allows the user to either open a web link in a browser or send and email to a clicked email address.

i construct my intents as follows:-

fun sendEmail(recipient: String, subject: String, message: String) {
    val uriText = "${EMAIL_MAIL_TO}${recipient}?subject=${subject}&body=${message}\n\n"

    val emailIntent = Intent(Intent.ACTION_SENDTO, Uri.parse(uriText)).apply {
        flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_NEW_TASK
        putExtra(Intent.EXTRA_EMAIL, arrayOf(recipient))
        putExtra(Intent.EXTRA_SUBJECT, subject)
        putExtra(Intent.EXTRA_TEXT, message)
    }
    startActivity(Intent.createChooser(emailIntent, "Contact $recipient"))
}

and

fun openBrowser(url: String) {
    val browserIntent = Intent(Intent.ACTION_VIEW).apply {
        flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_NEW_TASK
        addCategory(Intent.CATEGORY_BROWSABLE)
        data = Uri.parse(url)
    }
    startActivity(Intent.createChooser(browserIntent, "Open Web Url"))
}

the above code works for both sending emails and opening the devices web browser on the desired web page.

when i replace createChooser with

if (intent.resolveActivity(packageManager) != null) {
    startActivity(intent)
}

in both cases the resolveActivity function returns null.

what am i doing wrong?


Solution

  • https://developer.android.com/training/package-visibility/use-cases#check-browser-available

    <- Place inside the "<queries>" element. ->

    <intent>
      <action android:name="android.intent.action.VIEW" />
      <category android:name="android.intent.category.BROWSABLE" />
      <data android:scheme="https" />
    </intent>