Chrome Custom Tabs redirect to Android app will close the app

◇◆丶佛笑我妖孽 提交于 2019-11-28 12:03:44

I've also observed my Android app unexpectedly background after server-side 302 redirection to a custom scheme, and observed expected handling from stand-alone Chrome and manually triggered redirection in the client.

I was able to "fix" the issue by calling the warmup function before loading the url that redirects.

In other words, this works:

void launchTab(Context context, Uri uri){
    final CustomTabsServiceConnection connection = new CustomTabsServiceConnection() {
        @Override
        public void onCustomTabsServiceConnected(ComponentName componentName, CustomTabsClient client) {
            final CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
            final CustomTabsIntent intent = builder.build();
            client.warmup(0L); // This prevents backgrounding after redirection
            intent.launchUrl(context, uri);
        }
        @Override
        public void onServiceDisconnected(ComponentName name) {}
    };
    CustomTabsClient.bindCustomTabsService(context, "com.android.chrome", connection);
}

This doesn't work:

void launchTab(Context context, Uri uri){
    final CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
    final CustomTabsIntent intent = builder.build();
    intent.launchUrl(context, uri);
}

The Chrome Custom Tab docs describe warming up as a best practice, but it also appears to help ensure expected behavior.

In terms of env, I'm testing on a Nexus 5X w Chrome 51. My chrome tab dependency in Gradle looks like this:

dependencies {
    compile 'com.android.support:customtabs:24.0.0'

If I used android:launchMode="singleInstance" there were multiple instances in the task manager so this was no option.

Starting the CustomTabsIntent with FLAG_ACTIVITY_NEW_TASK Flag did the trick.

CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent intent = builder.build();

intent.intent.setFlags(FLAG_ACTIVITY_NEW_TASK);
intent.launchUrl(context, Uri.parse(url));

It helped me to set the Activity that I use to start a CustomTab to singleInstance mode in the manifest file:

    <activity
        android:launchMode="singleInstance"
        android:name="com.example.SingleInstanceActivityToStartCustomTab"
    </activity>

And in the code I do as usual:

    CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
    final CustomTabsIntent customTabsIntent = builder.build();
    customTabsIntent.intent.setPackage(someChromePackage);
    customTabsIntent.launchUrl(singleInstanceModeActivity, someUriThatDoesRedirect);

I tried warming up Chrome and even calling customTabsIntent.launchUrl() with some delay after calling client.warmup(0l); and neither helped.

I'm pretty sure this is the result of a bug in Chrome. I updated all of my devices (GS6, GS7, Nexus 7 and Nexus 9) to the latest version of Chrome and my app is no longer minimized when the redirect occurs.

I just discovered this today (11/3/2016) so I don't yet have any information about the specific bug or its subsequent resolution. This is just what I've noticed.

Hope that helps!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!