ajinasokan / flutter_displaymode

A Flutter plugin to set display mode in Android
MIT License
184 stars 22 forks source link

Refresh rate defaults back to 60hz after exiting the app then opening it again. #20

Closed jsfsarkis closed 1 year ago

jsfsarkis commented 2 years ago

Hi, I am currently setting the refresh rate to high in my main() function.

On app start, refresh rate is set to 120hz and all is well, but if I exit the app without killing it, and come back to it, the refresh rate defaults back to 60hz, if I kill the app and restart it, it goes back to 120hz.

Am I missing something/doing something wrong?

Appreciate any feedback/help, cheers.

Edit: I just cloned and built the example you provided, and it does the same thing. When I set it to 120hz, it's @ 120hz, when I quit the app without killing it and I come back to it, it's back to 60hz.

I confirmed that it is indeed 60hz by enabling "show refresh rate" option in developer settings on my Android device.

ajinasokan commented 1 year ago

It is possible that manufacturers implement different heuristics on this to optimize the battery. If this is the case then you might also have to set the display mode on app resume as well, by hooking to the AppLifecycleState callbacks.

jsfsarkis commented 1 year ago

Hi, yes I thought about trying that, but it still didn't work, until I did a workaround. When I leave the app, the refresh rate drops down to 60hz, and even when I set it to 120hz again on app resume, it stays at 60hz. I printed the current refresh rate using FlutterDisplayMode on app resume before and after I set it to 120hz, and FlutterDisplayMode returned that the current refresh rate is 120hz even before I set it to 120hz when the app was resumed, while in fact the app was running on 60hz.

So, the workaround I did was, on each app resume, I set the refresh rate to low, then immediately after set it to high, and now it works fine.

I understand that, as you said, it is switching to 60hz because of different manufacturers etc and I have confirmed this by testing on different devices and not all exhibit this behavior, but what I fear might be an issue with the package is that, on app resume, even though the app is running at 60hz, FlutterDisplayMode returns that it is running at 120hz, which is why simply setting it to 120hz won't have any effect, I have to set it to low first then to high.

ajinasokan commented 1 year ago

Ah I just remembered setHighRefreshRate does have a check to see if the existing mode is same.

https://github.com/ajinasokan/flutter_displaymode/blob/80d9c6a908f03ee937675322b70dffd5b7de64f0/lib/src/flutter_display_mode.dart#L71-L73

Are you using this method? If setPreferredMode is called directly there is no such checks.

ajinasokan commented 1 year ago

Fixed in flutter_displaymode: 0.5.0

I have removed the above check. So it will always set the mode. Thanks for reporting and for the good explanation!