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.04k stars 1.73k forks source link

White or Black screen navigation on Android #13698

Closed JORGEGO closed 1 month ago

JORGEGO commented 1 year ago

Description

We are migrating our apps from Xamarin to MAUI. Our apps use Shell and use routes all works as expected in windows and in Android... but behavior's in android when you navigate during some time start to show white or black windows (depend of your theme android). I debug my app trying to understand what happen and I believe it is related to the memory:

In my case, for example: AnswerMailboxPage works perfectly any page works perfectly, but if you navigate may times for different page any page not render, navigate perfect: await Shell.Current.GoToAsync($"Todo.MailBox.AnswerMailboxPage?pIdMailbox={lMailboxItem.IdMessage}"); but not render the page and the debug output show this information in the case that show white or black screen

Steps to Reproduce

If you create any project with shell and navigate passing parameters to the page for about 30 times for different pages start this behaviour...

Link to public reproduction project repository

I can not share the code

Version with bug

6.0.312

Last version that worked well

6.0.312

Affected platforms

Android

Affected platform versions

Android 13

Did you find any workaround?

No for the moment, I have also added to the android manifest android:enableOnBackInvokedCallback="true" but nothing

Relevant log output

...
[ss.back.Android] Explicit concurrent copying GC freed 9664(520KB) AllocSpace objects, 7(188KB) LOS objects, 49% free, 8972KB/17MB, paused 26us,19us total 21.891ms
Thread started: <Thread Pool> #20
Resolved pending breakpoint at 'C:\greenYng\greenyngMOBILE\greenyng.mobile\greenyng.mobile.business.back\Views\Todo\MailBox\AnswerMailboxPage.xaml.cs:30,1' to void greenyng.mobile.business.back.Views.Todo.MailBox.AnswerMailboxPage..ctor () [0x00008].
[ss.back.Android] Explicit concurrent copying GC freed 2815(263KB) AllocSpace objects, 3(204KB) LOS objects, 49% free, 8848KB/17MB, paused 23us,19us total 24.077ms
[0:] Microsoft.Maui.Controls.Xaml.Diagnostics.BindingDiagnostics: Warning: '#FFFFFF' cannot be converted to type 'Microsoft.Maui.Graphics.Color'
[OnBackInvokedCallback] OnBackInvokedCallback is not enabled for the application.
[OnBackInvokedCallback] Set 'android:enableOnBackInvokedCallback="true"' in the application manifest.
[Choreographer] Skipped 1125 frames!  The application may be doing too much work on its main thread.
[OpenGLRenderer] Davey! duration=18725ms; Flags=0, FrameTimelineVsyncId=107687437, IntendedVsync=737426048460041, Vsync=737444758683791, InputEventId=0, HandleInputStart=737444762882262, AnimationStart=737444762883531, PerformTraversalsStart=737444763025377, DrawStart=737444763247339, FrameDeadline=737426081757240, FrameInterval=737444762848031, FrameStartTime=16630533, SyncQueued=737444763685069, SyncStart=737444763982839, IssueDrawCommandsStart=737444764761839, SwapBuffers=737444772667608, FrameCompleted=737444774241339, DequeueBufferDuration=43308, QueueBufferDuration=879115, GpuCompleted=737444774212146, SwapBuffersCompleted=737444774241339, DisplayPresentTime=737393480777606, CommandSubmissionCompleted=737444772667608, 
Thread started: <Thread Pool> #21
[0:] Microsoft.Maui.StreamImageSourceService: Warning: Unable to load image stream.

System.Threading.Tasks.TaskCanceledException: A task was canceled.
   at Microsoft.Maui.Controls.StreamImageSource.Microsoft.Maui.IStreamImageSource.GetStreamAsync(CancellationToken userToken) in D:\a\_work\1\s\src\Controls\src\Core\StreamImageSource.cs:line 48
   at Microsoft.Maui.StreamImageSourceService.GetDrawableAsync(IImageSource imageSource, Context context, CancellationToken cancellationToken) in D:\a\_work\1\s\src\Core\src\ImageSources\StreamImageSourceService\StreamImageSourceService.Android.cs:line 61
[0:] Microsoft.Maui.StreamImageSourceService: Warning: Unable to load image stream.

System.Threading.Tasks.TaskCanceledException: A task was canceled.
   at Microsoft.Maui.Controls.StreamImageSource.Microsoft.Maui.IStreamImageSource.GetStreamAsync(CancellationToken userToken) in D:\a\_work\1\s\src\Controls\src\Core\StreamImageSource.cs:line 48
   at Microsoft.Maui.StreamImageSourceService.GetDrawableAsync(IImageSource imageSource, Context context, CancellationToken cancellationToken) in D:\a\_work\1\s\src\Core\src\ImageSources\StreamImageSourceService\StreamImageSourceService.Android.cs:line 61
[0:] Microsoft.Maui.IViewHandler: Error: Unexpected exception in MapBackground.

System.Threading.Tasks.TaskCanceledException: A task was canceled.
   at Microsoft.Maui.Controls.StreamImageSource.Microsoft.Maui.IStreamImageSource.GetStreamAsync(CancellationToken userToken) in D:\a\_work\1\s\src\Controls\src\Core\StreamImageSource.cs:line 48
   at Microsoft.Maui.StreamImageSourceService.GetDrawableAsync(IImageSource imageSource, Context context, CancellationToken cancellationToken) in D:\a\_work\1\s\src\Core\src\ImageSources\StreamImageSourceService\StreamImageSourceService.Android.cs:line 76
   at Microsoft.Maui.Platform.ViewExtensions.UpdateBackgroundImageSourceAsync(View platformView, IImageSource imageSource, IImageSourceServiceProvider provider) in D:\a\_work\1\s\src\Core\src\Platform\Android\ViewExtensions.cs:line 353
   at Microsoft.Maui.TaskExtensions.FireAndForget(Task task, Action`1 errorCallback) in D:\a\_work\1\s\src\Core\src\TaskExtensions.cs:line 36
[0:] Microsoft.Maui.IViewHandler: Error: Unexpected exception in MapBackground.

System.Threading.Tasks.TaskCanceledException: A task was canceled.
   at Microsoft.Maui.Controls.StreamImageSource.Microsoft.Maui.IStreamImageSource.GetStreamAsync(CancellationToken userToken) in D:\a\_work\1\s\src\Controls\src\Core\StreamImageSource.cs:line 48
   at Microsoft.Maui.StreamImageSourceService.GetDrawableAsync(IImageSource imageSource, Context context, CancellationToken cancellationToken) in D:\a\_work\1\s\src\Core\src\ImageSources\StreamImageSourceService\StreamImageSourceService.Android.cs:line 76
   at Microsoft.Maui.Platform.ViewExtensions.UpdateBackgroundImageSourceAsync(View platformView, IImageSource imageSource, IImageSourceServiceProvider provider) in D:\a\_work\1\s\src\Core\src\Platform\Android\ViewExtensions.cs:line 353
   at Microsoft.Maui.TaskExtensions.FireAndForget(Task task, Action`1 errorCallback) in D:\a\_work\1\s\src\Core\src\TaskExtensions.cs:line 36
JORGEGO commented 1 year ago

I am replacing all the embedded resources images for MauiImages and seems works must better

ghost commented 1 year ago

Hi @JORGEGO. We have added the "s/needs-repro" label to this issue, which indicates that we require steps and sample code to reproduce the issue before we can take further action. Please try to create a minimal sample project/solution or code samples which reproduce the issue, ideally as a GitHub repo that we can clone. See more details about creating repros here: https://github.com/dotnet/maui/blob/main/.github/repro.md

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 commented 1 year ago

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

JORGEGO commented 1 year ago

My latest experience is that if I don't use the embedded resources, the black/white screens don't appear... It seems that if a page uses a list of embedded resources, instead of using MAUI images, it ends up on Android a memory freeing problem, so after changing the resources to MAUIImages, the screens don't seem to appear anymore... I'll keep you posted

kfrancis commented 1 year ago

We might be seeing the same thing here. I'll try and get some more info shortly.

kfrancis commented 1 year ago

blankScreen

Once we put the app in the background and then back in the foreground, it's fine for a time but it does happen somewhat randomly. It happens 100% of the time when the app is first started, as seen in the above recording.

android_log.txt

UkeHa commented 1 year ago

@kfrancis i think this is fixed in .net8 preview according to #14109 - could you give it a shot?

jinxinjuan commented 1 year ago

@kfrancis Not repro on android platform with Visual Studio Enterprise 17.8.0 Preview 1.0(.NET8) using this sample project (https://github.com/dotnet/maui-samples/tree/main/7.0/Navigation/ShellFlyoutSample), could you have a try on .NET8 to see if it works fine? Thanks.

kfrancis commented 1 year ago

@jinxinjuan Reproduced and filed it here (as it looks possibly related to toolkit, but based on the stack trace it might not be): https://github.com/CommunityToolkit/Maui/issues/1368

Stack trace:

Android.Util.AndroidRuntimeException
  Message=Only the original thread that created a view hierarchy can touch its views.

    0xFFFFFFFFFFFFFFFF in Android.Runtime.JNIEnv.monodroid_debugger_unhandled_exception C#
    0x1A in Android.Runtime.JNINativeWrapper._unhandled_exception at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:12,5 C#
    0x1D in Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PP_V at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:23,26    C#
    0x17 in System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw    C#
    0x89 in Java.Interop.JniEnvironment.InstanceMethods.CallNonvirtualVoidMethod at /Users/runner/work/1/s/xamarin-android/external/Java.Interop/src/Java.Interop/obj/Release/net7.0/JniEnvironment.g.cs:12324,5    C#
    0x78 in Java.Interop.JniPeerMembers.JniInstanceMethods.InvokeVirtualVoidMethod at /Users/runner/work/1/s/xamarin-android/external/Java.Interop/src/Java.Interop/Java.Interop/JniPeerMembers.JniInstanceMethods_Invoke.cs:75,7   C#
    0x12 in Android.Views.View.RequestLayout at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/obj/Release/net7.0/android-33/mcw/Android.Views.View.cs:20879,5 C#
    0x8 in Microsoft.Maui.Platform.WrapperView.RequestLayout at D:\a\_work\1\s\src\Core\src\Platform\Android\WrapperView.cs:89,4    C#
    0x8 in Android.Views.View.n_RequestLayout at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/obj/Release/net7.0/android-33/mcw/Android.Views.View.cs:20869,4    C#
    0x8 in Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PP_V at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:22,5  C#
    0x37 in Java.Interop.NativeMethods.java_interop_jnienv_call_static_void_method_a    C#
    0x54 in Java.Interop.JniEnvironment.StaticMethods.CallStaticVoidMethod at /Users/runner/work/1/s/xamarin-android/external/Java.Interop/src/Java.Interop/obj/Release/net7.0/JniEnvironment.g.cs:13246,4  C#
    0x1A in Java.Interop.JniPeerMembers.JniStaticMethods.InvokeVoidMethod at /Users/runner/work/1/s/xamarin-android/external/Java.Interop/src/Java.Interop/Java.Interop/JniPeerMembers.JniStaticMethods.cs:97,4 C#
    0x7B in Microsoft.Maui.PlatformInterop.LoadImageFromStream at D:\a\_work\1\s\src\Core\src\obj\Release\net7.0-android\generated\src\Microsoft.Maui.PlatformInterop.cs:443,5  C#
    0xC8 in Microsoft.Maui.StreamImageSourceService.LoadDrawableAsync at D:\a\_work\1\s\src\Core\src\ImageSources\StreamImageSourceService\StreamImageSourceService.Android.cs:28,6 C#
    0x11 in System.Runtime.CompilerServices.AsyncTaskMethodBuilder<Microsoft.Maui.IImageSourceServiceResult>.AsyncStateMachineBox<Microsoft.Maui.StreamImageSourceService.<LoadDrawableAsync>d__0>.ExecutionContextCallback C#
    0x17 in System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop C#
    0x55 in System.Runtime.CompilerServices.AsyncTaskMethodBuilder<Microsoft.Maui.IImageSourceServiceResult>.AsyncStateMachineBox<Microsoft.Maui.StreamImageSourceService.<LoadDrawableAsync>d__0>.MoveNext C#
    0x2 in System.Runtime.CompilerServices.AsyncTaskMethodBuilder<Microsoft.Maui.IImageSourceServiceResult>.AsyncStateMachineBox<Microsoft.Maui.StreamImageSourceService.<LoadDrawableAsync>d__0>.ExecuteFromThreadPool C#
    0xC in System.Threading.ThreadPoolWorkQueue.DispatchWorkItem    C#
    0x142 in System.Threading.ThreadPoolWorkQueue.Dispatch  C#
    0x67 in System.Threading.PortableThreadPool.WorkerThread.WorkerThreadStart  C#
    0x1F in System.Threading.Thread.StartHelper.RunWorker   C#
    0x28 in System.Threading.Thread.StartHelper.Run C#
    0xF in System.Threading.Thread.StartCallback    C#

What the absolute minimal repro looks like: repro

kfrancis commented 1 year ago

@jinxinjuan While it doesn't exactly repo the same in net 8.0.0-preview.7.8842 - the control is still not rendering (though the exception and the white screen issue are now gone):

reproNet8P7

Zhanglirong-Winnie commented 1 month ago

This issue has been verified using Visual Studio 17.12 Preview 1 (8.0.80 & 8.0.72). Not repro on android platform. Using this sample project: https://github.com/dotnet/maui-samples

dotnet-policy-service[bot] commented 1 month ago

Hi @JORGEGO. We have added the "s/try-latest-version" label to this issue, which indicates that we'd like you to try and reproduce this issue on the latest available public version. This can happen because we think that this issue was fixed in a version that has just been released, or the information provided by you indicates that you might be working with an older version.

You can install the latest version by installing the latest Visual Studio (Preview) with the .NET MAUI workload installed. If the issue still persists, please let us know with any additional details and ideally a reproduction project provided through a GitHub repository.

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.