In my NET9 MAUI application, I try to implement the deeplink following the Microsoft documentation. When I send a request, it gets to the OnCreate in the MainActivity.
[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true,
LaunchMode = LaunchMode.SingleTop,
ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation |
ConfigChanges.UiMode |
ConfigChanges.ScreenLayout |
ConfigChanges.SmallestScreenSize |
ConfigChanges.Density)]
[IntentFilter(new[] { Platform.Intent.ActionAppAction },
Categories = new[] { global::Android.Content.Intent.CategoryDefault })]
[IntentFilter(new[] { Intent.ActionView },
Categories = new[]
{
Intent.ActionView,
Intent.CategoryDefault,
Intent.CategoryBrowsable,
},
DataScheme = "liu", DataHost = "", DataPathPrefix = "/")]
public class MainActivity : MauiAppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Window.SetFlags(WindowManagerFlags.Secure,
WindowManagerFlags.LayoutNoLimits);
Window.SetStatusBarColor(Android.Graphics.Color.Blue);
var action = Intent?.Action;
var data = Intent?.Data?.ToString();
if (action == Intent.ActionView && data is not null)
{
HandleAppLink(data);
}
}
}
Immediately after that, I get this error:
System.InvalidOperationException: 'This window is already associated with an active Activity (LanguageInUse.MainActivity). Please override CreateWindow on LanguageInUse.App to add support for multiple activities https://aka.ms/maui-docs-create-window or set the LaunchMode to SingleTop on LanguageInUse.MainActivity.'
As the documentation says, I added in the MauiProgram.cs this
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
})
.ConfigureLifecycleEvents(lifecycle =>
{
#if ANDROID
lifecycle.AddAndroid(android =>
{
android.OnCreate((activity, bundle) =>
{
var action = activity.Intent?.Action;
var data = activity.Intent?.Data?.ToString();
if (action == Android.Content.Intent.ActionView && data is not null)
{
Task.Run(() => HandleAppLink(data));
}
});
android.OnNewIntent((activity, intent) =>
{
var action = intent?.Action;
var data = intent?.Data?.ToString();
if (action == Android.Content.Intent.ActionView && data is not null)
{
Task.Run(() => HandleAppLink(data));
}
});
});
#endif
});
