36

I have enabled App linking in my application. It works fine. But in my application there are some scenarios where i cannot handle the incoming url. In those cases i want to redirect that url to the default browser in the device.

Currently what i have tried doing is using intents to open browser with the url, but it again redirects to my app itself. The app link is of the format ->

https://<domain>/<prefix>/<params>

so depending on the params, i would like to either handle the app link in the app itself or redirect it to the default browser. Below is the code i tried to open the browser with the above url

val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(appLinkModel.url))
browserIntent.addCategory(Intent.CATEGORY_APP_BROWSER)
browserIntent.resolveActivity(packageManager)?.let {
    startActivity(browserIntent)
}

I tried excluding the addCategory() line but the results are the same. Either the app crashes(hence the resolveActivity()), or app opens itself in a loop.

WHAT I WANT TO DO

So what i want to do is redirect the url to the default browser(or show a chooser WITHOUT my app in it), without triggering the app link again and again. So is this possible?

4 Answers 4

50
String data = "example.com/your_url?param=some_param";
Intent defaultBrowser = Intent.makeMainSelectorActivity(Intent.ACTION_MAIN, Intent.CATEGORY_APP_BROWSER);
defaultBrowser.setData(data);
startActivity(defaultBrowser);

this technique (using makeMainSelectorActivity) will force the link to open in the device's default browser

Note - makeMainSelectorActivity only works for API level 15 and above.

If you need to support API levels lower than 15, you could try this hack

For Android 13 (API level 33) and above a MAIN Action selector may not work when sending a VIEW intent as Android enforces the intent matching the intent filter. The following approach should work for API level 33 (and also 15+):

val emptyBrowserIntent = Intent()
    .setAction(Intent.ACTION_VIEW)
    .addCategory(Intent.CATEGORY_BROWSABLE)
    .setData(Uri.fromParts("http", "", null))
val targetIntent = Intent()
    .setAction(Intent.ACTION_VIEW)
    .addCategory(Intent.CATEGORY_BROWSABLE)
    .setData(targetUri)
    .setSelector(emptyBrowserIntent)
startActivity(targetIntent)
Sign up to request clarification or add additional context in comments.

3 Comments

It working fine on android 12 and below, but not working on android 13. on android 13, It throw ActivityNotFoundException.
In Android 13, the app throws an exception: ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] dat=https://... sel=act=android.intent.action.MAIN cat=[android.intent.category.APP_BROWSER]} }. See issuetracker.google.com/issues/243678703. In my opinion, we can indeed use a try-catch block to catch the exception and then proceed with opening the webview.
9

In Kotlin, try using makeMainSelectorActivity :

val defaultBrowser = Intent.makeMainSelectorActivity(Intent.ACTION_MAIN, Intent.CATEGORY_APP_BROWSER)
defaultBrowser.data = Uri.parse("https://yoururl.com/")
startActivity(defaultBrowser)

Comments

0
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://stackoverflow.com/questions/58800240/android-app-link-open-a-url-from-app-in-browser-without-triggering-app-link"));
        Intent chooseIntent = Intent.createChooser(intent, "Choose from below");
        startActivity(chooseIntent);

This will open all the installed web browsers from which user can choose!

1 Comment

I have already tried this. This just redirects back to my app itself because of the App linking.
0

Unfortunately, we can't exclude any certain URL, since Android doesn't provide that option.

But you can handle using path prefix.

// To handle:
http://myhost.com/v/page1?id=123
// To exclude:
http://myhost.com/v/page2?id=123

then in manifiest

<data android:scheme="http"
      android:host="myhost.com"
      android:pathPrefix="/v/page1" />

2 Comments

Sorry. I cannot use this. Because the in the url the only change is the "param" portion. And based on the params i have to decide whether to handle it in the app or browser.
Try opening in custom chrome tab

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.