11

Is there any listener in which we can know that URL has been changed in chrome custom tabs.

String url = ¨https://paul.kinlan.me/¨;
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(this, Uri.parse(url));

I am opening this link using chrome custom tabs. I have to snip each url change and open appropriate activity in android

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
John
  • 8,846
  • 8
  • 50
  • 85
  • With `WebView` you could use custom `WebViewClient` with override methods `shouldOverrideUrlLoading(WebView view, String url)` and `shouldInterceptRequest(WebView view, String url)` but with Chrome Custom Tab I believe you have to resort to deep linking and register to pickup certain URLs in your Application's Manifest rather than intercepting them. – nstosic May 25 '18 at 09:41
  • I dont want to use webview as it is very slow. Is there any docs or links ? – John May 25 '18 at 09:42
  • This is the official Android developer documentation https://developer.android.com/training/app-links/deep-linking . However, I didn't test this with Chrome Custom Tab, I believe you'd have to do additional work with it since your application is already active. – nstosic May 25 '18 at 09:44
  • app linking is enabled in my app already. I just need to listen to url change in custom tabs. Is there any docs for this ? – John May 25 '18 at 09:53
  • AFAIK no, Google introduced Chrome Custom Tab so that app is sandboxed from user browsing and vice-versa. – nstosic May 25 '18 at 10:03
  • Possible duplicate of [Is it possible to get the current URL of a Android Chrome CustomTab in real time?](https://stackoverflow.com/questions/49583579/is-it-possible-to-get-the-current-url-of-a-android-chrome-customtab-in-real-time) – andreban May 25 '18 at 20:46

2 Answers2

10

Unfortunately, it is not possible

In order to safeguard the user's privacy when navigating, the URLs are not automatically sent to the host app through the navigation events.

It is possible to get the URL as a result of the user clicking on the custom action button or on one of the buttons on the secondary toolbar.

This code sample shows how to setup the custom action button and this code shows how to retrieve the URL inside a BroadcastReceiver, invoked by the CustomAction.

andreban
  • 4,621
  • 1
  • 20
  • 49
1

If you know the host you can setup an IntentFilter

<intent-filter>
     <action android:name="android.intent.action.VIEW" />
     <category android:name="android.intent.category.DEFAULT" />
     <category android:name="android.intent.category.BROWSABLE" />

     <data
         android:host="paul.kinlan.me"
         android:scheme="https"/>

</intent-filter>

for any Activity you want to handle this URL in. It will start in onCreate with an argument arguments?.get("_uri") as? Uri or something similar, which you can further parse and see where the user has gone to.

Yurets
  • 3,999
  • 17
  • 54
  • 74