maui icon indicating copy to clipboard operation
maui copied to clipboard

.NET MAUI Stuck on splash screen when reopen the app after start a foreground service

Open alzubitariq opened this issue 5 months ago • 5 comments

Description

I am running a foreground service on .Net Maui Blazor. Once I got the foreground service to start and exit the app, when I try to reopen the application. It is stuck on the SplashScreen

Steps to Reproduce

I already provided a repository

Link to public reproduction project repository

https://github.com/alzubitariq/MauiAppServiceIssue

Version with bug

8.0.7 SR2

Is this a regression from previous behavior?

Yes, this used to work in .NET MAUI

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

Android 33

Did you find any workaround?

No

Relevant log output

No response

alzubitariq avatar Feb 18 '24 18:02 alzubitariq

Yes, this used to work in .NET MAUI

You mention this worked before and therefore is a regression. Do you know in what version it worked? Does it work with the exact same code?

@jonathanpeppers does anything stand out to you immediately with this?

jfversluis avatar Feb 18 '24 18:02 jfversluis

@jfversluis Yes it was working with .NET MAUI 7.0.49 if I am not mistaken

alzubitariq avatar Feb 18 '24 19:02 alzubitariq

Does it work with the exact same code?

It was different code but with foreground service and when I try to reopen the application it opens without loading the assemblies like it is live inside the memory "Very fast"

alzubitariq avatar Feb 19 '24 06:02 alzubitariq

We've added this issue to our backlog, and we will work to address it as time and resources allow. If you have any additional information or questions about this issue, please leave a comment. For additional info about issue management, please read our Triage Process.

ghost avatar Feb 19 '24 15:02 ghost

Between .NET 7 and 8, Android API 34 was released. What is the error message you are getting? adb logcat output would show it:

https://learn.microsoft.com/xamarin/android/deploy-test/debugging/android-debug-log#accessing-from-the-command-line

You could try targeting API 33 with this line in your AndroidManifest.xml (it will trigger a warning):

<uses-sdk android:targetSdkVersion="33" />

If the problem goes away when doing this, it's an Android OS behavior change that is the cause.

jonathanpeppers avatar Feb 20 '24 15:02 jonathanpeppers

Hi @alzubitariq. We have added the "s/needs-info" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

ghost avatar Feb 22 '24 08:02 ghost

I'm having the same issue, in MAUI Android 8.0.100 targeting SDK 33 or 34. This is only occuring with a Blazor Hybrid app not a normal MAUI app.

Log attached, I can't see an obvious error, possibly this one 02-23 15:57:30.940 549 2815 E TaskPersister: File error accessing recents directory (directory doesn't exist?). logcat.txt

My reproduction repo https://github.com/greg73/MauiHybridForegroundService

Duplicate issue I logged (closed) https://github.com/dotnet/maui/issues/20812

greg73 avatar Feb 23 '24 16:02 greg73

I encountered the same issue upgrading my maui blazor hybrid app from .net 7 to .net 8.

Tried a few scenarios - all blazor hybrid: .NET 7 Targetting API33 on 33 Device - works .NET 8 Targetting API33 on 33 Device - does not work - freezes at startup screen Targetting API33 on 34 Device - does not work - freezes at startup screen Targetting API34 on 34 Device - does not work - freezes at startup screen

wtvamp avatar Feb 26 '24 21:02 wtvamp

I encountered the same issue upgrading my maui blazor hybrid app from dotnet 7 to dotnet 8.

Everything was is fine in dotnet 7 but when upgrade my project to dotnet 8 (with exact same code), In first start everything seems fine but when i switch my app or close it (when my foreground service is running) and reopen the app, The app will freeze in splash screen.

Anyone have any solution for this?

Alipoustdouzan avatar Mar 01 '24 10:03 Alipoustdouzan

Verified this issue with Visual Studio 17.10.0 Preview 1. Can repro on Android platform.

image

ninachen03 avatar Mar 12 '24 07:03 ninachen03

Confirm this issue still exists with these NuGet Packages :

    <ItemGroup>
        <PackageReference Include="Microsoft.Maui.Controls" Version="8.0.14" />
        <PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="8.0.14" />
        <PackageReference Include="Microsoft.AspNetCore.Components.WebView.Maui" Version="8.0.14" />
        <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.0" />
    </ItemGroup>

Alipoustdouzan avatar Mar 28 '24 14:03 Alipoustdouzan

~~I ran into a similar if not identical issue in my Xamarin Android project. Whenever I navigate between two fragments or re-open the app from background, there is a chance that the app gets frozen.~~

(Edit: Not related to this issue)

PopSlime avatar Apr 04 '24 04:04 PopSlime

I still have this issue with any version of .NET 8 but only on a Blazor App. On a normal .NET Maui App with .NET 8 it works well. It happens every time I open the app with a foreground service already running.

On MainActivity, the place where the app gets stuck is:

protected override void OnStart()
{
    base.OnStart();    <------ Here
}

The work around I found for now is to stop the service and restart the app:

protected override void OnStart()
{
    var serviceIntent = new Intent(this, typeof(MyBackgroundService));
    var stopped = StopService(serviceIntent);

    if (stopped)
    {
        FinishAndRemoveTask();

        Intent i = new Intent(this.ApplicationContext, typeof(MainActivity));
        i.SetFlags(ActivityFlags.ClearTop | ActivityFlags.NewTask);
        MainApplication.Current.StartActivity(i);

        // Throw exception here to avoid reaching the base.OnStart() that actually causes the problem.
        throw new Exception("Forcing a crash to work around the Foreground Service running at startup issue!");
    }
    
    base.OnStart();
}

Initially I was having the same problem in a normal .Net Maui App but the reason was because there was an issue on my OnDestroy() method.

nunosantosgithub avatar Apr 30 '24 15:04 nunosantosgithub

Same, but in iOS simulator 🥺 in Windows and Android Emulator its working (the app is new from visual studio template MAUI Blazor hybrid)

Naisroan avatar May 01 '24 22:05 Naisroan

I still have this issue with any version of .NET 8 but only on a Blazor App. On a normal .NET Maui App with .NET 8 it works well. It happens every time I open the app with a foreground service already running.

On MainActivity, the place where the app gets stuck is:

protected override void OnStart()
{
    base.OnStart();    <------ Here
}

The work around I found for now is to stop the service and restart the app:

protected override void OnStart()
{
    var serviceIntent = new Intent(this, typeof(MyBackgroundService));
    var stopped = StopService(serviceIntent);

    if (stopped)
    {
        FinishAndRemoveTask();

        Intent i = new Intent(this.ApplicationContext, typeof(MainActivity));
        i.SetFlags(ActivityFlags.ClearTop | ActivityFlags.NewTask);
        MainApplication.Current.StartActivity(i);

        // Throw exception here to avoid reaching the base.OnStart() that actually causes the problem.
        throw new Exception("Forcing a crash to work around the Foreground Service running at startup issue!");
    }
    
    base.OnStart();
}

Initially I was having the same problem in a normal .Net Maui App but the reason was because there was an issue on my OnDestroy() method.

I tested your solution but after stop the service application still freeze on base.OnStart();

Alipoustdouzan avatar May 09 '24 10:05 Alipoustdouzan