Warm-up and pre-fetch: using the Custom Tabs Service

Published on

The third part of this guide focuses on speeding up the browser startup via warmup() and prefetching web pages via mayLaunchUrl(). Warming up the browser process can save up to 700ms when opening a link. Pre-rendering content via mayLaunchUrl will make external content open instantly. Together both APIs can greatly improve the user experience of a Custom Tabs integration and are highly recommended.

The required steps are:

  1. Check via CustomTabsClient.getPackageName() If the default browser supports Custom Tabs. If yes, bind to the CustomTabsService via CustomTabsClient.bindCustomTabsService().

  2. Once connected to the CustomTabsService, in the CustomTabsServiceConnection.onCustomTabsServiceConnected() callback, do:

    a. Warmup the browser process via CustomTabsClient.warmup(). b. Create a new CustomTabsSession via CustomTabsClient.newSession().

  3. Optionally, prefetch web pages the user is likely to visit via CustomTabsSession.mayLaunchUrl().

  4. When launching a new Custom Tab, pass the CustomTabsSession to the CustomTabsIntent.Builder via the constructor new CustomTabsIntent.Builder(session).


When targeting API level 30, CustomTabsClient.getPackageName() requires you to add a queries section to your Android Manifest, declaring an intent-filter that matches browsers with Custom Tabs support.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

<action android:name="android.support.customtabs.action.CustomTabsService" />

Here is a full example for how to connect to a Custom Tabs service:

private CustomTabsClient mClient;
private CustomTabsSession mSession;

private CustomTabsServiceConnection mConnection = new CustomTabsServiceConnection() {
public void onCustomTabsServiceConnected(
@NonNull ComponentName name,
@NonNull CustomTabsClient client
) {
mClient = client;
// Warm up the browser process
mClient.warmup(0 /* placeholder for future use */);
// Create a new browser session
mSession = mClient.newSession(new CustomTabsCallback());
// Pre-render pages the user is likely to visit
// you can do this any time while the service is connected
mSession.mayLaunchUrl("https://developers.android.com", null, null);

public void onServiceDisconnected(ComponentName name) {
mClient = null;
mSesssion = null;

private void bindCustomTabService(Context context) {
// Check for an existing connection
if (mClient != null) {
// Do nothing if there is an existing service connection

// Get the default browser package name, this will be null if
// the default browser does not provide a CustomTabsService
String packageName = CustomTabsClient.getPackageName(context, null);
if (packageName == null) {
// Do nothing as service connection is not supported
CustomTabsClient.bindCustomTabsService(context, packageName, mConnection);

protected void onCreate(Bundle savedInstanceState) {

findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String url = "https://developers.android.com";
CustomTabsIntent intent = new CustomTabsIntent.Builder(session)
intent.launchUrl(MainActivity.this, Uri.parse(url));

A Custom Tabs service connection might fail while your activity is running. If your app requires an active service connection, there are two strategies to ensure a working service connection:

  1. When eagerly connecting to the CustomTabsService during onCreate(), reconnect to the service if your activity gets disconnected and the onServiceDisconnected() callback gets invoked.
  2. Wait until the user triggers a Custom Tab, then establish the service connection and launch the Custom Tab.

Next up: Learn how to resize the Custom Tabs experience.

Published on Improve article

This site uses cookies to deliver and enhance the quality of its services and to analyze traffic. If you agree, cookies are also used to serve advertising and to personalize the content and advertisements that you see. Learn more about our use of cookies.