Chrome Custom Tabs for Android

Working with Custom Tabs isn’t exactly straightforward. Even minimal examples involve quite a bit of code:

What makes it worse is that one can’t expect Chrome to be available on any device. That means additional code to fall back to good old WebView. To simplify the experience, there is a new library available that provides a higher-level interface that implements many often-needed convenience features:

To create a new CustomTabsIntent and start it with a keep-alive service as well as a fallback:

CustomTabsIntent customTabsIntent = new CustomTabsIntent.Builder()
.addDefaultShareMenuItem()
.setToolbarColor(this.getResources()
.getColor(R.color.colorPrimary))
.setShowTitle(true)
.setCloseButtonIcon(backArrow)
.build();

// This is optional but recommended
CustomTabsHelper.addKeepAliveExtra(this, customTabsIntent.intent);

// This is where the magic happens...
CustomTabsHelper.openCustomTab(this, customTabsIntent,
Uri.parse("https://github.com/saschpe/android-customtabs"),
new WebViewFallback());

Preload CustomTabs in your Application.java to warm-up early and reduce start-up time:

// Preload custom tabs service for improved performance
// This is optional but recommended
registerActivityLifecycleCallbacks(new
CustomTabsActivityLifecycleCallbacks());

This is how it looks:

Now Custom Tabs are great again!