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

Android no longer reacts to system theme change #16241

Open
kerams opened this issue Jul 5, 2024 · 5 comments · May be fixed by #16340
Open

Android no longer reacts to system theme change #16241

kerams opened this issue Jul 5, 2024 · 5 comments · May be fixed by #16340

Comments

@kerams
Copy link
Contributor

kerams commented Jul 5, 2024

Describe the bug

@emmauss

To Reproduce

https://github.com/kerams/theme

Pull down the status bar and change theme, notice the application theme changes accordingly. Repeat with version 11.2.999-cibuild0049782-alpha and observe application theme does not change.

Expected behavior

No response

Avalonia version

11.2.999-cibuild0049474-alpha, 11.2.999-cibuild0049782-alpha

OS

Android

@kerams kerams added the bug label Jul 5, 2024
@maxkatz6 maxkatz6 changed the title Android no longer reacts to system theme change [android] Android no longer reacts to system theme change Jul 5, 2024
@maxkatz6 maxkatz6 changed the title [android] Android no longer reacts to system theme change Android no longer reacts to system theme change Jul 8, 2024
@emmauss
Copy link
Contributor

emmauss commented Jul 9, 2024

Android seems to have a weird behavior concerning theme "ownership".
A simple app(native android in this case) that doesn't change theme dynamically will always follow System theme. In this case, if the app is handling uiMode configuration changes to detect theme, both Activity.OnConfigurationChanged and View.OnConfigurationChanged will be called with the new ui mode.

If the app sets their app theme with AppCompatDelegate.DefaultNightMode to NightModeYes/No, the system no longer manages theming for the app, and only Activity.OnConfigurationChanged will be called for the app to detect system theme change.
In avalonia, we rely on View.OnConfigurationChanged to detect system theme change, but since we enforce ThemeVariant when the app launches, we no longer receive uiMode notifications through View.OnConfigurationChanged. We can listen to it on Activity level, but that wouldn't work when an avalonia view is embedded in an Activity we don't control.

As at now, we have 2 PlatformThemeVariants, Dark and Light. There is no Default or System variant that would reset the app theme to the system one. Android saves the last NightMode for the app and applies it when the Activity is created, unless the app is newly installed, the RequestedThemeVariant usually set in App.xaml will be ignored on Android 12. This is due to requesting the systems/app theme accents here;

OnConfigurationChanged();

which will also forward the saved NightMode to the app.

So the current issues are as follows;

  1. We aren't able to detect system theme changes once we set our own theme.
  2. We force our theme at launch, which we can't reset to system theme. We still need to ensure the theme set in App.xaml, but the Default variant has no PlatformThemeVariant version.
  3. We need to be able to get platform accent colors without overriding the theme requested in App.xaml.

@maxkatz6 any thoughts? We could add a Default/System PlatformThemeVariant to restore system theme.

@maxkatz6
Copy link
Member

maxkatz6 commented Jul 9, 2024

@emmauss we don't have PlatformThemeVariant.Default/System, because framework explicitly expects a specific value from the OS to be reported from the platform settings API -

public PlatformThemeVariant ThemeVariant { get; init; }
.

void SetFrameThemeVariant(PlatformThemeVariant themeVariant) might need to be nullable. But IIRC iOS didn't had an option to reset theme variant, which is why I left it non-nullable. But I might have forgot something.

But also, ideally, if theme wasn't changed, we shouldn't override any system value.

@kerams
Copy link
Contributor Author

kerams commented Jul 10, 2024

There's also one more issue. When you remove ConfigChanges.UiMode from ActivityAttribute, the application just crashes on system theme change.

@emmauss
Copy link
Contributor

emmauss commented Jul 10, 2024

There's also one more issue. When you remove ConfigChanges.UiMode from ActivityAttribute, the application just crashes on system theme change.

That's because of the activity being restarted because of an unhandled config change. We should handle it, but it seems to be broken

@Redns
Copy link

Redns commented Nov 6, 2024

Thanks for your contribution! But since the pull request has not been merged yet, is there any way to respond to android theme changes while still using the official Avalonia package?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants