dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.21k stars 1.74k forks source link

Issue with Essentials.WebAuthenticator and multiple activities #22017

Closed koviant closed 5 months ago

koviant commented 6 months ago

Description

Hello,

I'd like to report an issue that I've encountered while using WebAuthenticator from the MAUI Essentials.

In order to open the link in the CustomTabs on Android, WebAuthenticator uses the CustomTabsActivityManager from the Xamarin.AndroidX.Browser nuget. Method CustomTabsActivityManager.From contains the following code:

static CustomTabsActivityManager instance;

public static CustomTabsActivityManager From(Activity parentActivity, string servicePackageName = null)
{
    if (instance == null) {
        instance = new CustomTabsActivityManager(parentActivity);
    }

    return instance;
}

Due to the static instance field of the manager, it forever holds the reference to the activity that was passed with the first call to that method. The problem happens when this activity was destroyed, and then the app tries to call WebAuthenticator again. CustomTabsActivityManager still holds the reference to the Activity that is no longer in use, and then WebAuthenticator fails to use CustomTabs and fallbacks to the default browser.

I've created a repo where you can reproduce the issue.

Also this issue was discussed in this issue in the Xamarin/AndroidX repository, and as the result of that, the From method was marked as obsolete.

Steps to Reproduce

  1. Download the repo
  2. Run the android app on the device that have browser, which supports CustomTabs (Chrome)
  3. Tap "Navigate to second activity" button
  4. Tap "Open Web Auth" button
  5. Verify that the CustomTab is open, user is not redirected to a browser app
  6. Go back to the repro app
  7. Tap "Go back", which would finish the second activity
  8. Tap "Navigate to second activity" button
  9. Tap "Open Web Auth" button

Expected result: link is still opened in the CustomTabs Actual result: user is redirected to the browser app, CustomTabs are not open

Link to public reproduction project repository

https://github.com/koviant/DroidWebAuthRepro

Version with bug

8.0.20 SR4

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

No response

Did you find any workaround?

No response

Relevant log output

No response

PureWeen commented 6 months ago

/similarissues

github-actions[bot] commented 6 months ago

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!

Open similar issues:

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.