【问题标题】:Xamarin Forms: Android CustomTabs close when app is backgroundedXamarin Forms:Android CustomTabs 在应用程序后台关闭时关闭
【发布时间】:2020-10-23 00:45:52
【问题描述】:

TL;DR:当应用程序在后台运行时,CustomTabs 会关闭,但我们需要它保持活动状态。我们怎样才能做到这一点?

我们有一个使用 CustomTabs 登录用户的应用程序。我们添加了双因素身份验证,但这会带来一个问题。当您点击登录按钮时,自定义选项卡 Intent 将按以下方式启动:

        var tcs = new TaskCompletionSource<BrowserResult>();
        try
        {
            var activity = (Activity)Forms.Context;

            var builder = new CustomTabsIntent.Builder().EnableUrlBarHiding();

            var customTabsIntent = builder.Build();

            customTabsIntent.Intent.AddFlags(ActivityFlags.NoHistory);
            
            Action<string> callback = null;
            callback = url =>
            {
                MainActivity.Callbacks -= callback;

                tcs.SetResult(new BrowserResult
                {
                    ResultType = BrowserResultType.Success,
                    Response = url
                });
            };

            MainActivity.Callbacks += callback;

            customTabsIntent.LaunchUrl(activity, Android.Net.Uri.Parse(options.StartUrl));

        }
        catch (Exception ex)
        {
            throw new Exception($"error: {ex.Message}");
        }

        return tcs.Task;

这按预期工作,您可以登录,当然现在询问两因素身份验证代码的代码,这在大多数情况下意味着您必须后台应用程序,打开您的身份验证器(authy、Google 身份验证器等)和然后使用代码返回应用程序。问题是当我们重新打开应用程序时,CustomTabs 和它的会话完全消失了。这意味着您必须再次单击它,再次登录,一切都会发生同样的事情。我已经寻找了几天的解决方案。谁能帮助我们找到一种方法来保持 CustomTabs 和它的会话打开,这样您就可以填写您的身份验证器代码并从此愉快地登录?

【问题讨论】:

    标签: android xamarin xamarin.forms xamarin.android android-customtabs


    【解决方案1】:

    好吧,似乎是某人添加的以下代码导致了这种情况:

    customTabsIntent.Intent.AddFlags(ActivityFlags.NoHistory);
    

    我删除了它,它现在似乎可以工作了。我看到其他一些问题正在发生,但我认为这与这个问题无关。如果有,我会在这里通知您。

    【讨论】:

      猜你喜欢
      • 2015-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-13
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多