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:
Check via
CustomTabsClient
.getPackageName()
If the default browser supports Custom Tabs. If yes, bind to the CustomTabsService viaCustomTabsClient.bindCustomTabsService()
.Once connected to the CustomTabsService, in the
CustomTabsServiceConnection.onCustomTabsServiceConnected()
callback, do:a. Warmup the browser process via
CustomTabsClient.warmup()
. b. Create a newCustomTabsSession
viaCustomTabsClient.newSession()
.Optionally, prefetch web pages the user is likely to visit via
CustomTabsSession.mayLaunchUrl()
.When launching a new Custom Tab, pass the
CustomTabsSession
to the CustomTabsIntent.Builder via the constructornew 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"
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("https://developers.android.com", null, null);
}
@Override
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
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(session)
.build();
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:
- When eagerly connecting to the
CustomTabsService
duringonCreate()
, reconnect to the service if your activity gets disconnected and theonServiceDisconnected()
callback gets invoked. - Wait until the user triggers a Custom Tab, then establish the service connection and launch the Custom Tab.
Published on • Improve article