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 makes 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).

If your app targets Android 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"
xmlns:tools="http://schemas.android.com/tools">


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

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() {
@Override
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(Uri.parse("https://developers.android.com"), null, null);
}

@Override
public void onServiceDisconnected(ComponentName name) {
mClient = null;
mSession = null;
}
};

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

// 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
return;
}
CustomTabsClient.bindCustomTabsService(context, packageName, mConnection);
}

@Override
protected void onCreate(Bundle savedInstanceState) {

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

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.

Open webpages in native apps

On Android, URLs can be handled by native applications. For example, if the user has the Facebook app installed and clicks on a link to a Facebook post, they usually prefer the link opening in the Facebook app instead of in the browser.

By default, Custom Tabs opens links in the respective native application if installed. However, once a CustomTabsServiceConnection has been established, this behavior stops working and all URLs open in Custom Tabs instead. For an improved user experience, we recommend re-enabling this behavior using the following code:

CustomTabsIntent customTabsIntent = new CustomTabsIntent.Builder()
.setSendToExternalDefaultHandlerEnabled(true)
.build();

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.