Search code examples
androidandroid-customtabs

Is there a way to trigger an activity on a chrome custom tabs resume?


I don't think there's a way to do this but I wanted to double check.

I have some web content being loaded in a chrome custom tab, and the content could be sensitive in nature.

The android app has a PIN screen, which we would like to show when the custom tab resumes, but I don't see any way to do this, or believe it's possible.

Can this be done with custom tabs, or will the web app need to implement their own PIN screen?


Solution

  • There's a callback when you bind a custom tab service that will alert you when a tab is shown (5) / hidden (6)

    CustomTabsServiceConnection customTabsServiceConnection = new CustomTabsServiceConnection() {
                @Override
                public void onCustomTabsServiceConnected(ComponentName componentName, CustomTabsClient customTabsClient) {
                    client = customTabsClient;
                    client.warmup(0L);
                    customTabsSession = client.newSession(new CustomTabsCallback() {
                        @Override
                        public void onNavigationEvent(int navigationEvent, @Nullable Bundle extras) {
                            super.onNavigationEvent(navigationEvent, extras);
                        }
                }
    }
    

    You can then bind this and the callbacks will start triggering when you use it when launching your tabs (SDK <= 29, it will be different on 30)

        String packageName = CustomTabsClient.getPackageName(this, null);
        boolean didSucceed = CustomTabsClient.bindCustomTabsService(this, packageName, mCustomTabsServiceConnection);
    
        CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder(customTabsSession);