diff --git a/Xamarin.Essentials/WebAuthenticator/WebAuthenticator.android.cs b/Xamarin.Essentials/WebAuthenticator/WebAuthenticator.android.cs index c117f1187..f6e204224 100644 --- a/Xamarin.Essentials/WebAuthenticator/WebAuthenticator.android.cs +++ b/Xamarin.Essentials/WebAuthenticator/WebAuthenticator.android.cs @@ -14,6 +14,8 @@ public partial class WebAuthenticator static TaskCompletionSource tcsResponse = null; static Uri currentRedirectUri = null; + internal static bool AuthenticatingWithCustomTabs { get; private set; } = false; + internal static bool OnResume(Intent intent) { // If we aren't waiting on a task, don't handle the url @@ -71,7 +73,11 @@ static async Task PlatformAuthenticateAsync(WebAuthentic tcsResponse = new TaskCompletionSource(); currentRedirectUri = callbackUrl; - if (!(await StartCustomTabsActivity(url))) + // Try to start with custom tabs if the system supports it and we resolve it + AuthenticatingWithCustomTabs = await StartCustomTabsActivity(url); + + // Fall back to using the system browser if necessary + if (!AuthenticatingWithCustomTabs) { // Fall back to opening the system-registered browser if necessary var urlOriginalString = url.OriginalString; diff --git a/Xamarin.Essentials/WebAuthenticator/WebAuthenticatorCallbackActivity.android.cs b/Xamarin.Essentials/WebAuthenticator/WebAuthenticatorCallbackActivity.android.cs index 17056e01d..95aed8e01 100644 --- a/Xamarin.Essentials/WebAuthenticator/WebAuthenticatorCallbackActivity.android.cs +++ b/Xamarin.Essentials/WebAuthenticator/WebAuthenticatorCallbackActivity.android.cs @@ -10,11 +10,21 @@ protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); - // start the intermediate activity again with flags to close the custom tabs - var intent = new Intent(this, typeof(WebAuthenticatorIntermediateActivity)); - intent.SetData(Intent.Data); - intent.AddFlags(ActivityFlags.ClearTop | ActivityFlags.SingleTop); - StartActivity(intent); + // Check how we launched the flow initially + if (WebAuthenticator.AuthenticatingWithCustomTabs) + { + // start the intermediate activity again with flags to close the custom tabs + var intent = new Intent(this, typeof(WebAuthenticatorIntermediateActivity)); + intent.SetData(Intent.Data); + intent.AddFlags(ActivityFlags.ClearTop | ActivityFlags.SingleTop); + StartActivity(intent); + } + else + { + // No intermediate activity if we returned from a system browser + // intent since there's no custom tab instance to clean up + WebAuthenticator.OnResume(Intent); + } // finish this activity Finish();