8

I have a chrome custom tab, but I want to add a listener to the close (X) button on the upper left corner of the title bar.

I want to trigger a callback every time the user clicks on the close button.

I was able to do that in a web view, but unable to figure out if that is possible for a chrome custom tab.

Here is the code snippet i use to call a custom tab:

     CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
                    builder.enableUrlBarHiding();
                    builder.setToolbarColor(getTitleBarBackgroundColor());
                    builder.setStartAnimations(this, android.R.anim.slide_in_left, android.R.anim.slide_out_right);
                    builder.setExitAnimations(this, android.R.anim.slide_in_left, android.R.anim.slide_out_right);
                    customTabsIntent = builder.build();
                    customTabsIntent.intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY |  Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                    customTabsIntent.launchUrl(this, Uri.parse(url));
Vardaan Sharma
  • 1,125
  • 1
  • 10
  • 21

4 Answers4

16

There already have some queries ( here and here ) regarding chrome custom tab close button customization for different purpose. From current implementation of chrome custom tab, that's not possible to add a listener directly to chrome custom tab close button. You only can customize the close button for its icon.

Update: Although you can't add a listener directly to chrome custom tab close button, you can fire callback on dismiss of chrome custom tab by using onResume() or onActivityResult() of caller activity from where chrome custom tab has opened. But remember that, in this case, callback will be invoked whether chrome custom tab has closed by close button or device back key.

VIN
  • 6,385
  • 7
  • 38
  • 77
mdrafiqulrabin
  • 1,657
  • 5
  • 28
  • 38
  • 3
    Depending on how your activity was started, onResume() will also be called if the user navigates to another app (if he taps 'open in chrome' for example) and comes back. – natronite Feb 09 '18 at 07:15
  • 3
    how to know if the onresume is being called due to custom tab closed or something else (if suppose there are other places that can also resume the activity) – RamPrasadBismil Apr 18 '20 at 01:47
0

It's not possible to override the behaviour of the close button. But if you just want to fire a callback to, for instance, track clicks on the close button, you can use the onResume() callback on the Activity that started the custom tab, as the caller Activity will be resumed as a result of the custom tab being closed.

andreban
  • 4,621
  • 1
  • 20
  • 49
  • 3
    how to know if the onresume is being called due to custom tab closed or something else (if suppose there are other places that can also resume the activity) – RamPrasadBismil Apr 18 '20 at 01:47
  • 2
    Because you know onPause() was called when the CustomTabs Activity was started, you can save that information and track matching onResume() calls – andreban Apr 20 '20 at 06:52
0

there is a setActionButton method that allows you to provide a PendingIntent so you could effectively use that as a callback

hmac
  • 267
  • 3
  • 9
0

Not possible. I do not understand why "custom tabs" technology does not support it. For me now best is to use onResume. Before starting custom tab I remember in var/prop, that custom tabs was opened and next call onResume() will check stored var/prop and with high probability it means "custom tabs was closed".

mikep
  • 5,880
  • 2
  • 30
  • 37