xamarin / AndroidX

AndroidX bindings for .NET for Android
MIT License
173 stars 42 forks source link

Issue with CustomTabsActivityManager and multiple activities #873

Closed koviant closed 2 months ago

koviant commented 2 months ago

Android application type

Android for .NET (net6.0-android, etc.)

Affected platform version

.NET 7.0.404, .NET 8.0.200

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.

Thank you

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

Did you find any workaround?

Haven't found a workaround, except copying all the code from MAUI's WebAuthenticator and from AndroidX.Browser, and changing the code in the CustomTabsActivityManager.From, so that it would check that passed activity is not the same as the one it already holds.

Relevant log output

No response

jpobst commented 2 months ago

I suspect anyone who knows why this was written this way is long gone.

However, CustomTabsActivityManager has a public constructor:

public CustomTabsActivityManager (Activity parentActivity)
{
  ParentActivity = parentActivity;
}

You should be able to create whatever CustomTabsActivityManager instances you want and managed their lifetimes however you see fit without using the From method.

koviant commented 2 months ago

Thanks, that should work. I guess now I should head out to the MAUI repo and ask them to drop calling the From method in favor of that constructor.

Btw, if you don't mind, I would like make a PR adding an [Obsolete] attribute on the From method, so that in future there would be less chance that someone would use this method, with the message that it may cause a memory leak by holding the reference to the activity in the static instance field.

jpobst commented 2 months ago

Agreed, I was thinking the same thing. 😉