androidapplinksandroid-app-linksandroid-customtabs

Jumping back with App Links closes CustomTab


so what I'm trying to achieve is the following:

In App 1, I'm loading a website in a CustomTab, this website jumps to App 2 via App Links. In App 2, after performing a task, I want to jump back to App 1, where the CustomTab still needs to be open as it needs to do some processing. I have this working where I jump back to App 1 via the package name, but I need to get it working via App Links as well.

However, what currently happens is that the CustomTab seems to get closed when I jump back via App Links. My first thought is that I'm not retaining the app / starting the activity correctly.

AndroidManifest of App 1:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="my.secret.package.name">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:launchMode="singleTask"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter android:autoVerify="true">
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />

                <data
                    android:scheme="https"
                    android:host="some.secret.url" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Opening the CustomTab in App 1:

        CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
        CustomTabsIntent customTabsIntent = builder.build();
        customTabsIntent.launchUrl(MainActivity.this, Uri.parse(url));

Code in App 2 to jump back:

    private void performAppSwitch() {
        Intent intentWithURI = new Intent(Intent.ACTION_VIEW, Uri.parse(APP_LINKS_URL));
        PackageManager packageManager = context.getPackageManager();

        if (intentWithURI.resolveActivity(packageManager) != null) {
            intentWithURI.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intentWithURI);
        } else {
            Intent intentWithPackageID = packageManager.getLaunchIntentForPackage(PACKAGE_NAME);

            if (intentWithPackageID != null) {
                List<ResolveInfo> activities = packageManager.queryIntentActivities(intentWithPackageID, 0);
                boolean isIntentSafe = !activities.isEmpty();

                if (isIntentSafe) {
                    intentWithPackageID.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(intentWithPackageID);
                }
            }
        }
    }

Any ideas as to why it's working when I switch via package name but not when I switch via App Links? I have tried every possible variant of launchMode


Solution

  • The issue was that the CustomTab was starting an activity of its own, and I had to make sure that App 1 did not redraw.

    So in App 1, adding the following to onCreate of my main activity would do the trick:

    if (!isTaskRoot()) {
        finish();
        return;
    }