I want to implement deep links in my .net Maui app. I test on a physical android device. When the application is closed everything works fine, but if the app is still open in the background and I open a link I get the error "Window was allready created". I follow the standard guide from Microsoft (https://learn.microsoft.com/en-us/dotnet/maui/android/app-links?view=net-maui-8.0)
This is my Mauiprogram.cs:
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.UseMauiCommunityToolkit()
.UseViewServices()
.UseMicrocharts()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
})
.ConfigureLifecycleEvents(lifecycle =>
{
#if IOS || MACCATALYST
#elif ANDROID
lifecycle.AddAndroid(android => {
android.OnCreate((activity, bundle) =>
{
var action = activity.Intent?.Action;
var data = activity.Intent?.Data?.ToString();
if (action == Intent.ActionView && data is not null)
{
HandleAppLink(data);
}
});
});
#endif
});
//...
#if DEBUG
builder.Logging.AddDebug();
#endif
return builder.Build();
}
static void HandleAppLink(string url)
{
if (Uri.TryCreate(url, UriKind.RelativeOrAbsolute, out var uri))
App.Current?.SendOnAppLinkRequestReceived(uri);
}
}
And this is my OnAppLinkRequestReceived:
protected override async void OnAppLinkRequestReceived(Uri uri)
{
base.OnAppLinkRequestReceived(uri);
// Überprüft, ob die Anforderung von der Passwort-App stammt
if (uri.Host == "engelberth-developing" && uri.Segments != null)
{
// Behandelt den App-Link für das Teilen von Elementen
if (uri.Segments.Length == 5 && uri.Segments[2] == "share/")
{
Global.Transfer = uri.Segments[3].Replace("/", "|") + uri.Segments[4].Replace("/", "").Replace('*', '/');
// Setzt die nächste Seite auf die Seite zum Freigeben von Elementen
await AssignService.SetNextPage<ISharedItemPage>();
}
// Behandelt den App-Link für die Synchronisierung von Geräten
else if (uri.Segments.Length == 4 && uri.Segments[2] == "sync/")
{
Global.Transfer = uri.Segments[3].Replace("/", "").Replace('*', '/');
// Setzt die nächste Seite auf die Seite für die Gerätesynchronisierung
await AssignService.SetNextPage<IDeviceSyncPage>();
}
}
}
This is my service function. It is working 100% for all other operations:
public static async Task SetNextPage<TService>(string paramValue = null)
{
var implementationType = services
.Where(service => service.ServiceType == typeof(TService))
.Select(service => service.ImplementationType)
.FirstOrDefault();
if (implementationType != null)
{
var route = paramValue != null ? $"/{implementationType.Name}?{paramValue}" : $"/{implementationType.Name}";
await Shell.Current.GoToAsync(route);
}
}
The method gets calles everytime. So this is not the problem. The problem is, when the application should switch the side and ends in an error.