自动关闭Chrome自定义标签

Var*_*rma 8 android chrome-custom-tabs

我有一个用于打开手机浏览器的应用程序,用户将被重定向到我的webapp,在某个用户操作后,webapp会使用intent过滤器重定向回应用程序

 <application>     
<activity
                android:name="com.xyz.sdk.SomeActivity"
                android:allowTaskReparenting="true"
                android:configChanges="keyboardHidden|orientation|keyboard|screenSize"
                android:launchMode="singleTask"
                android:noHistory="true"
                android:theme="@android:style/Theme.NoTitleBar">
                <intent-filter>
                    <data android:scheme="someString-${applicationId}" />
                    <action android:name="android.intent.action.VIEW" />
                    <category android:name="android.intent.category.DEFAULT" />
                    <category android:name="android.intent.category.BROWSABLE" />
                </intent-filter>
            </activity>
 </application>
Run Code Online (Sandbox Code Playgroud)

最近我迁移到使用Chrome自定义标签而不是浏览器(出于安全考虑,Webview不是一个选项)

不幸的是,当在Chrome自定义标签中重定向回应用时,Chrome自定义标签页不会自动关闭.即使我可以通过日志验证应用程序已收到响应,自定义选项卡仍保持打开并保持在最顶层,因此用户无法看到应用程序上发生的更新,直到他关闭应用程序.

我发现一个解决方法是在启动我的自定义选项卡时设置NEW_TASK标志,但这使我的应用程序和自定义选项卡显示为两个单独的应用程序,这是我首先从浏览器中移开的全部原因.

总结:我有App A,它打开一个自定义选项卡,在自定义选项卡中打开的webapp使用intent过滤器重定向回App A,但vustom选项卡不会关闭并保持在顶部,直到用户手动关闭它.

我已经尝试为自定义选项卡意图设置NO_HISTORY标志但它没有任何区别.

自定义标签代码:

   CustomTabsIntent customTabsIntent = new CustomTabsIntent.Builder()
                         .setShowTitle(true)
                         .setToolbarColor(getToolBarColor())
                         .setStartAnimations(this, android.R.anim.slide_in_left, android.R.anim.slide_out_right)
                         .setExitAnimations(this, android.R.anim.slide_in_left, android.R.anim.slide_out_right)
                         .build();
                 customTabsIntent.intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                 customTabsIntent.launchUrl(this, Uri.parse(url));
                 customTabsIntent.launchUrl(this, Uri.parse(url));
Run Code Online (Sandbox Code Playgroud)

更新 我已经意识到如果意图过滤器重定向回到另一个应用程序,那么自定义选项卡成功关闭(假设它是一个单一活动并且没有历史记录设置为true)但在我的情况下,我们需要重定向回相同的活动从中调用自定义选项卡.

Fan*_*ock 6

我也遇到了同样的问题,但就我而言,它现在可以正常工作了。

我使用FLAG_ACTIVITY_NO_HISTORY和FLAG_ACTIVITY_CLEAR_TOP启动了自定义标签。

customTabsIntent.intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
customTabsIntent.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Run Code Online (Sandbox Code Playgroud)

我的活动的launchMode是“ singleTop”。

<activity
    android:name=".SomeActivity"
    android:theme="@style/SomeTheme"
    android:configChanges="keyboardHidden|orientation|screenSize"
    android:noHistory="true"
    android:launchMode="singleTop" >
    <intent-filter>
        <data android:scheme="intent" />
        <data android:scheme="myFile" />
        <data android:scheme="myScheme" />
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
    </intent-filter>
</activity>
Run Code Online (Sandbox Code Playgroud)

@bkant的重要更新:

通过将启动模式从“ singleTop”更改为“ singleTask”或“ singleInstance”,我能够使它工作。在这种模式下,重定向将通过“ onNewIntent”到达同一活动。