简体   繁体   中英

React native change color scheme in real time problem android

What I'm doing is giving the user the possibility to change the color of the device scheme ( light or dark ) in real time.

On ios it works, when on the device I press the key combination: Command + Uppercase + A .

Event is intercepted, and the theme is changed in real time.

When on Android I try to change the theme color from the button menu, as you can see from the image.

在此处输入图像描述

The event is not intercepted I have to exit the app for a moment and then re-enter to make the changes happen.

I'm using import {useColorScheme} from 'react-native' .

I also tried import {useColorScheme} from 'react-native-appearance' , but on ios the realtime thing doesn't work.

How can I solve?

I had a similar issue, on iOS the theme change worked fine however on Android it did not trigger a re-render.

I fixed this by doing the following thing: Create an appstate listener which detects when an app is coming back from the background to the foreground .

Then in the listener function I checked if the current theme is equal to the theme selected in the device. If the theme changed I updated my redux store which contains my colorScheme.

And by doing that all my components subscribed to my reduxstate just re-renderd.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM