简体   繁体   English

自动关闭Chrome自定义标签

[英]Auto close chrome custom tab

I had an app that used to open the phone browser, where the user would be redirected to my webapp, where after a certain user action, the webapp redirects back to the app using intent filters 我有一个用于打开手机浏览器的应用程序,用户将被重定向到我的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>

Recently I migrated to using Chrome Custom Tabs instead of browser (Webview is not an option due to security concerns) 最近我迁移到使用Chrome自定义标签而不是浏览器(出于安全考虑,Webview不是一个选项)

Unfortunately, when redirecting back to app in chrome custom tabs the chrome custom tab does not auto close. 不幸的是,当在Chrome自定义标签中重定向回应用时,Chrome自定义标签页不会自动关闭。 Even though i can verify via logs that the app has received the response, the custom tab remains open and stays on top, so the user cant see whats happening on the app, until he closes the app. 即使我可以通过日志验证应用程序已收到响应,自定义选项卡仍保持打开并保持在最顶层,因此用户无法看到应用程序上发生的更新,直到他关闭应用程序。

One workaround i found to this was to set the NEW_TASK flag while initiating my custom tab, but that makes my app and the custom tab show up as two separate apps which is the entire reason I moved away from browsers in the first place. 我发现一个解决方法是在启动我的自定义选项卡时设置NEW_TASK标志,但这使我的应用程序和自定义选项卡显示为两个单独的应用程序,这是我首先从浏览器中移开的全部原因。

In summary: I have App A, which opens a custom tab, the webapp opened in the custom tab redirects back to App A using intent filters, but the vustom tab does not close and remains on top until the user manuall closes it. 总结:我有App A,它打开一个自定义选项卡,在自定义选项卡中打开的webapp使用intent过滤器重定向回App A,但vustom选项卡不会关闭并保持在顶部,直到用户手动关闭它。

I have tried setting the NO_HISTORY flag for the custom tab intent but it made no difference. 我已经尝试为自定义选项卡意图设置NO_HISTORY标志但它没有任何区别。

custom tab code: 自定义标签代码:

   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));

UPDATE I have realized that if the intent filter redirect back to a different app, then the custom tab closes successfully (provided its a singletop activity and no history is set to true) But in my case, we need to redirect back to the same activity from which custom tabs was invoked. 更新我已经意识到如果意图过滤器重定向回到另一个应用程序,那么自定义选项卡成功关闭(假设它是一个单一活动并且没有历史记录设置为true)但在我的情况下,我们需要重定向回相同的活动从中调用自定义选项卡。

I also faced the same issue, but in my case its now working. 我也面临同样的问题,但就我而言,它现在正在发挥作用。

I launched the custom tab with FLAG_ACTIVITY_NO_HISTORY and FLAG_ACTIVITY_CLEAR_TOP. 我使用FLAG_ACTIVITY_NO_HISTORY和FLAG_ACTIVITY_CLEAR_TOP启动了自定义选项卡。

customTabsIntent.intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
customTabsIntent.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

And launchMode of my activity was "singleTop". 而我的活动的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>

An important update by @bkant: @bkant的重要更新:

I was able to get this to work by changing the launch mode from 'singleTop' to either 'singleTask' or 'singleInstance'. 我可以通过将启动模式从“singleTop”更改为“singleTask”或“singleInstance”来实现此功能。 In this mode, the redirect will come to the same Activity via 'onNewIntent'. 在此模式下,重定向将通过'onNewIntent'进入同一个Activity。

This problem can exist in Xamarin too and the answer applies to issue when using Xamarin.Forms. 这个问题也可以存在于Xamarin中,并且当使用Xamarin.Forms时,答案适用于问题。 It can be applied by modifying the Activity attribute. 可以通过修改Activity属性来应用它。 Modify the code to match LaunchMode = LaunchMode.SingleTask 修改代码以匹配LaunchMode = LaunchMode.SingleTask

Here is a modified example of Xamarain.Auth from https://developer.xamarin.com/samples/xamarin-forms/WebServices/OAuthNativeFlow/ 以下是来自https://developer.xamarin.com/samples/xamarin-forms/WebServices/OAuthNativeFlow/的Xamarain.Auth的修改示例

namespace OAuthNativeFlow.Droid
{
     [Activity(Label = "CustomUrlSchemeInterceptorActivity", NoHistory = true, LaunchMode = LaunchMode.SingleTask)]
     [IntentFilter(
         new[] { Intent.ActionView },
         Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable },
         DataSchemes = new[] { "<insert custom URL here>" },
         DataPath = "/oauth2redirect")]
     public class CustomUrlSchemeInterceptorActivity : Activity
     {
         protected override void OnCreate(Bundle savedInstanceState)
         {
             base.OnCreate(savedInstanceState);

             // Convert Android.Net.Url to Uri
             var uri = new Uri(Intent.Data.ToString());

             // Load redirectUrl page
             AuthenticationState.Authenticator.OnPageLoading(uri);

             Finish();
         }
     }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM