Add a listener to Chrome Custom Tab Close button

Vardaan Sharma picture Vardaan Sharma · Feb 27, 2017 · Viewed 7.8k times · Source

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));

Answer

Rafiqul Rabin picture Rafiqul Rabin · Feb 27, 2017

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.