Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RequestedThemeChanged fires at Home button press event and shows wrong RequestedTheme in iOS #15962

Closed
arahmancsd opened this issue Jul 1, 2023 · 8 comments
Labels
area-theme Themes, theming platform/iOS 🍎 s/triaged Issue has been reviewed s/try-latest-version Please try to reproduce the potential issue on the latest public version s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working

Comments

@arahmancsd
Copy link

Description

RequestedThemeChanged event occurs when clicking on the Home button in iOS and AppThemeChangedEventArgs show a wrong RequestedTheme.

public partial class App : Application
{
    public App()
    {
           InitializeComponent();
          
           // This event fires when you press the Home button on iOS
           Current.RequestedThemeChanged += Current_RequestedThemeChanged;

           MainPage = new AppShell();
    }

    private void Current_RequestedThemeChanged(object sender, AppThemeChangedEventArgs e)
    {
        // Don't handle events fired for old application instances
        // and clean up the old instance's event handler
        if (Current != this && Current is App app)
        {
            Current.RequestedThemeChanged -= Current_RequestedThemeChanged;
            return;
        }

        // Shows a different theme

        Console.WriteLine($"{e.RequestedTheme}");

        UserAppTheme = e.RequestedTheme;
    }
}
WhatsApp.Video.2023-07-01.at.7.30.49.PM.mp4

Steps to Reproduce

  1. Download the repo.
  2. Run the application on iOS
  3. Set a breakpoint in
    Current_RequestedThemeChanged event in the App.cs
  4. Click the Home button in the simulator to minimize the app.
  5. Observe the e.RequestedTheme which shows a different or wrong theme.

Link to public reproduction project repository

https://github.com/arahmancsd/MauiTestApp1

Version with bug

7.0.49

Last version that worked well

Unknown/Other

Affected platforms

iOS

Affected platform versions

iOS

Did you find any workaround?

No

Relevant log output

No response

@arahmancsd arahmancsd added the t/bug Something isn't working label Jul 1, 2023
@jsuarezruiz jsuarezruiz added the area-theme Themes, theming label Jul 3, 2023
@drasticactions
Copy link
Contributor

drasticactions commented Jul 5, 2023

I can validate that this is happening, and the reason is... complicated.

public override void TraitCollectionDidChange(UITraitCollection? previousTraitCollection)
{
if (CurrentView?.Handler is ElementHandler handler)
{
var application = handler.GetRequiredService<IApplication>();
application?.UpdateUserInterfaceStyle();
application?.ThemeChanged();
}
base.TraitCollectionDidChange(previousTraitCollection);
}

public override void TraitCollectionDidChange(UITraitCollection previousTraitCollection)
{
base.TraitCollectionDidChange(previousTraitCollection);
var application = _shellContext?.Shell?.FindMauiContext().Services.GetService<IApplication>();
application?.ThemeChanged();
}

TraitCollectionDidChange is an event that UIKit sends whenever it detects an iOS interface change. When you leave the application and go to another app or back to the home screen, iOS, for some reason, sends a TraitCollectionDidChange event saying that the platform theme is inverted from whatever your current set is. (If your theme is set to Light on iOS, it will send an event saying the theme is now dark). It then sends another event with the correct theme you have.

MAUI handles this event to detect platform theme changes (which it should), so it is going with what iOS is sending, inverting and reverting the theme in the background. If you did not have the code you have in place for setting the UserAppTheme, you would not notice anything wrong. The app will look correct when you return. Because you are setting UserAppTheme inside of this event, that sets the UserAppTheme setting to the opposite theme, so MAUI is now going "The user wants the app to be X theme, ignore the platform settings" so when iOS sends the correct theme setting again, your app will ignore it because you just told MAUI to ignore it. UserAppTheme is a per-app theme setting that is separate from the OS. You don't need to handle it unless you specifically want to allow different theme settings away from the platform theme, and if you want to do that, you shouldn't do that inside of the RequestedThemeChange event. If the user changes their platform theme on an OS level, and you don't have a UserAppTheme set, your app should automatically change to whatever theme they selected, you don't need to set UserAppTheme yourself.

It's hard to tell where the issue is: MAUI is responding to iOS events and iOS is sending bogus information. It could be an underlying iOS issue, a .NET iOS Binding issue (@rolfbjarne, @dalexsoto?), or somewhere else in MAUI that's causing a platform event to fire when it shouldn't (Although IMO that doesn't seem likely).

@PureWeen I'm not sure who would be the right person for this. It feels like an SDK and/or bug in iOS, unless I'm missing some other event that is firing.

Also, seeing that Apple Documentation saying traitCollectionDidChange is depercated, that could be something that would need to be refactored out of MAUI and replaced with whatever the new methods are.

@arahmancsd
Copy link
Author

@drasticactions thanks for the explanation.

If you remove/comment UserAppTheme in the event. iOS would behave as expected, however, in Android, you have to run the app twice to see the effect.

Steps:

  1. Comment the line mentioned.
  2. Run the app on both Android and iOS
  3. Press the Home button or minimize the App.
  4. Change the theme from settings in the emulator/simulator to Dark/Light (both Android/iOS)
  5. Switch to the App
  6. iOS theme will change, however, Android doesn't
  7. For Android, you have to run the App twice to see the right theme.

UserAppTheme = e.RequestedTheme;

@drasticactions
Copy link
Contributor

AFAIK, you should never have to set UserAppTheme yourself if you are changing the theme on an OS level. If you have to do that, file a new bug.

@AnnYang01
Copy link

Verified this on Visual Studio Enterprise 17.7.0 Preview 5.0 in iOS 16.4. This issue repro on .NET 8.0 with below Project:
MauiApp12.zip
Screenshot in iOS 16.4
image

@AnnYang01 AnnYang01 added s/verified Verified / Reproducible Issue ready for Engineering Triage s/triaged Issue has been reviewed labels Aug 1, 2023
@rolfbjarne
Copy link
Member

MAUI is responding to iOS events and iOS is sending bogus information. It could be an underlying iOS issue, a .NET iOS Binding issue (@rolfbjarne, @dalexsoto?)

This is unlikely to be an issue with the bindings, it sounds like something iOS does just because (testing an Xcode project would confirm this).

@hwsmo
Copy link

hwsmo commented Nov 16, 2023

Hi. I have now noticed the bug in my app.

Anyone have an idea for a workaround?

@rkops-bd
Copy link

I'm experience this problem also, so hopefully this will be fixed asap.

Copy link
Contributor

Hi @arahmancsd. 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.

@github-actions github-actions bot locked and limited conversation to collaborators Jul 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-theme Themes, theming platform/iOS 🍎 s/triaged Issue has been reviewed s/try-latest-version Please try to reproduce the potential issue on the latest public version s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

8 participants