Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a listener to Chrome Custom Tab Close button

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));
like image 848
Vardaan Sharma Avatar asked Feb 27 '17 15:02

Vardaan Sharma


People also ask

How do I close a custom tab in Chrome?

Currently it seems like clicking on the close button(the button on the top left) from the chrome custom tab page is the only way to close it. Yeah. Also if you set your app to be singleInstance or singleTop, then when you go back to it using an intent, the framework will finish Custom tab activity as well.

What is open Tabs in custom Tabs?

Custom Tabs is a browser feature, introduced by Chrome, that is now supported by most major browsers on Android. It gives apps more control over their web experience, and makes transitions between native and web content more seamless without having to resort to a WebView.


Video Answer


1 Answers

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.

like image 170
mdrafiqulrabin Avatar answered Oct 04 '22 01:10

mdrafiqulrabin