Maui: [BUG] Net 9 Android - StatusBar Color overriden on modal pages

Is there an existing issue for this?

  • I have searched the existing issues

Did you read the “Reporting a bug” section on Contributing file?

Current Behavior

In MAUI Android, when adding StatusBarBehavior to change the color, and then going to a modal page - the status bar color gets replaced with the colorPrimary color from colors.xml.

This only happens on NET 9. In NET 8 the status bar color remained the same as the previous page. I think its because int NET 9 in the App.cs you have to create a new Window for the app shell, but is there a solution to the status bar changing?

protected override Window CreateWindow(IActivationState? activationState) { var window = new Window(new AppShell()); return window; }

Expected Behavior

In the modal page, the status bar color should be the same as the previous normal page. This was the behaviour in NET 8

Steps To Reproduce

  1. Open repo
  2. Run in NET 9 for Android and click on the button - see the status bar color change from orange to green/red.
  3. Change to NET 8 - see the color stays orange throughout.

Link to public reproduction project repository

https://github.com/Zack-G-I-T/StatusBarIssue-Net9

Environment

- .NET MAUI CommunityToolkit:
- OS:
- .NET MAUI:

Anything else?

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 3
  • Comments: 16 (8 by maintainers)

Most upvoted comments

This is something expected since .net maui changed the way how modal works, now it uses a DialogFragment, and it has it own Window, and that Window doesn’t apply the same configuration as the Activity.Window. I’ll need to think on how to re-design the code for StatusBar color, right now the work around should be this one

#if ANDROID
using Android.App;
using AndroidX.Activity;
using AndroidX.Fragment.App;
using FragmentManager = AndroidX.Fragment.App.FragmentManager;
using Microsoft.Maui.Platform;
using Android.Views;
using AndroidX.Core.View;
#endif
using CommunityToolkit.Maui;
using Microsoft.Extensions.Logging;
using Microsoft.Maui.LifecycleEvents;
using System.Diagnostics;

namespace TestStatusBar;

public static class MauiProgram
{
	public static MauiApp CreateMauiApp()
	{
		var builder = MauiApp.CreateBuilder();
		builder
			.UseMauiApp<App>()
			.UseMauiCommunityToolkit()
			.ConfigureFonts(fonts =>
			{
				fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
				fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
			})
			.ConfigureLifecycleEvents(a =>
			{
#if ANDROID
				a.AddAndroid(builder =>
				{
					builder.OnCreate((activity, bundle) =>
					{
						if (activity is not ComponentActivity componentActivity)
						{
							return;
						}
						componentActivity.GetFragmentManager()?.RegisterFragmentLifecycleCallbacks(new MyFragmentLifecycleCallbacks((fm, f) =>
						{
							if (f is AndroidX.Fragment.App.DialogFragment dialogFragment)
							{
								var activity = Platform.CurrentActivity;

								if (activity is null)
									return;

								var statusBarColor = activity.Window!.StatusBarColor;
								var platformColor = new Android.Graphics.Color(statusBarColor);

								var dialog = dialogFragment.Dialog;

								Debug.Assert(dialog is not null);
								Debug.Assert(dialog.Window is not null);

								var window = dialog.Window;
								dialog.Window.SetStatusBarColor(platformColor);

								bool isColorTransparent = platformColor == Android.Graphics.Color.Transparent;
								if (isColorTransparent)
								{
									window.ClearFlags(WindowManagerFlags.DrawsSystemBarBackgrounds);
									window.SetFlags(WindowManagerFlags.LayoutNoLimits, WindowManagerFlags.LayoutNoLimits);
								}
								else
								{
									window.ClearFlags(WindowManagerFlags.LayoutNoLimits);
									window.SetFlags(WindowManagerFlags.DrawsSystemBarBackgrounds, WindowManagerFlags.DrawsSystemBarBackgrounds);
								}

								window.SetDecorFitsSystemWindows(!isColorTransparent);


								CommunityToolkit.Maui.Core.Platform.StatusBar.SetColor(Color.FromArgb("#FFA500"));
							}

						}), false);
					});
				});
#endif
			});
		Routing.RegisterRoute("SecondPage", typeof(SecondPage));
		builder.Services.AddTransient<SecondPage>();
#if DEBUG
		builder.Logging.AddDebug();
#endif

		return builder.Build();
	}
}


#if ANDROID
public class MyFragmentLifecycleCallbacks(Action<AndroidX.Fragment.App.FragmentManager, AndroidX.Fragment.App.Fragment> onFragmentStarted) : FragmentManager.FragmentLifecycleCallbacks
{
	public override void OnFragmentStarted(FragmentManager fm, AndroidX.Fragment.App.Fragment f)
	{
		onFragmentStarted?.Invoke(fm, f);
		base.OnFragmentStarted(fm, f);
	}
}
#

Sadly you are correct. They released SR1.4 but we need SR2

@brminnick @bijington @gabsamples6 I tested the linked repo using the latest package references and the issue still occurs. Should this actually be marked as completed?

<PackageReference Include="CommunityToolkit.Maui" Version="10.0.0" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0" />
<PackageReference Include="Microsoft.Maui.Controls" Version="9.0.21" />

I don’t think they have release SR2 yet. I think it’s due out tomorrow. Once it is out I expect us to work hard to get it done quickly