Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

[Bug] DisplayInfoChanged invoked with wrong orientation #1355

Closed
BaseCrusher opened this issue Aug 20, 2020 · 10 comments
Closed

[Bug] DisplayInfoChanged invoked with wrong orientation #1355

BaseCrusher opened this issue Aug 20, 2020 · 10 comments
Labels
bug Something isn't working
Milestone

Comments

@BaseCrusher
Copy link

Description

When observing the MainDisplayInfoChanged of DeviceInfo you get Orientation Landscape when entering Protrait and vice versa.
In Addition to this, the OnDeviceOrientationChanged Event is only fired after you roatated the screen a second time.

On iOS it works as it should.

Steps to Reproduce

  1. Create a new Forms Project.
  2. Observe the OnDeviceOrientationChanged and write out the args.DisplayInfo.Orientation.
  3. Start the application and rotate the phone.

Expected Behavior

MainDisplayInfoChanged should be invoke on the first roatation not only after the second.
When the phone is roatated to Landscape the Orientation in the DisplayInfoChangedEventArgs should be Landscape as well.
Same goes for Portrait.

Actual Behavior

MainDisplayInfoChanged invokes only after the second roatation.
When the phone is roatated to Landscape the Orientation in the DisplayInfoChangedEventArgs is Portrait. The same issue applies to Portait rotation.

Basic Information

  • Version with issue: 1.5.3.2
  • Last known good version: --
  • IDE: Visual Studio 2019 (version 16.7.2)
  • Platform Target Frameworks:
    • iOS: --
    • Android: 9.0
    • UWP: --
  • Nuget Packages: --
  • Affected Devices: All Andorid Phones

Reproduction Link

Link

@BaseCrusher BaseCrusher added the bug Something isn't working label Aug 20, 2020
@StargazerNC
Copy link

I can confirm this one too. Same steps to reproduce.

@EvanMulawski
Copy link

Can also confirm this issue. Any update on when this will be fixed?

@mattleibow
Copy link
Contributor

I can't seem to reproduce this. Just tested the sample app on my Android 9 device.

Could you submit a sample app or a snippet that I can copy paste and see what the difference is?

@IndianaGary
Copy link

IndianaGary commented Dec 3, 2020

Mattleibow: The issue is not with a physical device, just the Android emulator. It works correctly with my Samsung S10+ and fails in the Pixel3 Q 10.0 emulator.

@jamesmontemagno
Copy link
Collaborator

I think it is a known issue maybe on emulators something with timing.

@mattleibow
Copy link
Contributor

Ah.... Maybe in the event handler, you can add a delay before actually reading the values? Probably just needs like a 10ms delay or even less.

@BaseCrusher
Copy link
Author

BaseCrusher commented Dec 16, 2020

@mattleibow
Sorry for the late response, I was pretty busy the last cuple weeks.

So I've updated the sample project and tested it out.
It is true, when running the code on a physical device this works just fine, but even after adding a delay to the event call I still get the same wrong behaviour on the simulator. Orientation and first event call are messed up as explained in the initial thread. I have added it as followed:

        public App()
        {
            InitializeComponent();

            MainPage = new MainPage();

            DeviceDisplay.MainDisplayInfoChanged += async (s, a) => { await OnDeviceOrientationChanged(s, a); };
        }

        async Task OnDeviceOrientationChanged(object Sender, DisplayInfoChangedEventArgs args)
        {
            await Task.Delay(10);

            if (args.DisplayInfo.Orientation == DisplayOrientation.Landscape)
            {
                Debug.WriteLine("Landscape");
            }
            else if (args.DisplayInfo.Orientation == DisplayOrientation.Portrait)
            {
                Debug.WriteLine("Portrait");
            }
        }

Interestingly enough I also realised that the event is called twice when using a physical device. Once with the orientation that you had before turning your phone and the second one with the new orientation. This could be troublesome if someone has a big chunk of code depending on the orientation.

As for the event not being raised on the first orientation switch:
On the phisical device this works without any proplems and only one event is raised. The follow orientation changes are raising the event twice as explained in the upper section.

@jamesmontemagno
Copy link
Collaborator

jamesmontemagno commented Apr 13, 2021

I believe this is just a bug with emulators in general and no real way to get around it :(

You could put in a bigger delay and then check DeviceDisplay.MainDisplay and go from there.

A work around that I can put in the code to do this is:

public override async void OnOrientationChanged(int orientation)
        {
            await Task.Delay(500);
            onChanged();
        }

This is probably fine and gives the device time to process the change.

@PhilippElhaus
Copy link

PhilippElhaus commented May 1, 2021

I can confirm this bug exists on Android as described. Just wanted to report it myself.
It does not work reliable also on physical Devices (Xiamo Red Mi 5).
Always has this problem of firing at the first turn.

mattleibow added a commit that referenced this issue Jul 12, 2021
mattleibow added a commit that referenced this issue Jul 13, 2021
@jamesmontemagno jamesmontemagno added this to the 1.7.0 milestone Jul 15, 2021
@yshteinm
Copy link

yshteinm commented Aug 4, 2021

I have a problem on physical device HXY A9 Android 11.0 API 30. When I read DeviceDisplay.MainDisplayInfo.Orientation always returns DisplayOrientation.Portrait.
I am using ZXing QR scanner and it also displays wrong scan window because it "thinks" it is in Portrait mode while it is not.
It happens only if system settings are set to not auto-rotate screen. Despite my app supports only Landscape mode and displayed correctly DeviceDisplay.MainDisplayInfo.Orientation stays always Portrait .

@jamesmontemagno jamesmontemagno modified the milestones: 1.7.0, 1.7.1 Oct 7, 2021
jfversluis added a commit that referenced this issue Jan 18, 2022
jfversluis added a commit that referenced this issue Jan 18, 2022
Fixed #1355 delay processing change so OS updates
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

8 participants