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

Fix Android FlyoutPage crash when changing orientation of tablet #24394

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

guyvaio
Copy link

@guyvaio guyvaio commented Aug 23, 2024

On an Android tablet, switching from landscape to portrait, and from portrait to landscape repeatedly produces this error: androidx.appcompat.widget.LinearLayoutCompat$LayoutParams cannot be cast to androidx.drawerlayout.widget.DrawerLayout$LayoutParams

This error can be easily reproduced on a physical tablet by quickly changing orientation.

The change of orientation triggers method UpdateFlyoutBehavior of src/Core/src/Handlers/FlyoutView/FlyoutViewHandler.Android.cs, that calls method LayoutViews to determine if the views are to be created in “side by side” mode or “as flyout”. The problem is after the rotation, DrawerLayout.GetPlatformViewBounds() called (for investigation) in this method shows that the Android view still has the height/width before the rotation. The MAUI code is ahead of the android reality. Waiting 100ms is enough to let Android adapt its interface.

A similar issue had been addressed by Xamarin developers: the comment on line 324 of https://github.com/xamarin/Xamarin.Forms/blob/5.0.0/Xamarin.Forms.Platform.Android/AppCompat/FlyoutPageRenderer.cs is //hack : when the orientation changes and we try to close the Flyout on Android //sometimes Android picks the width of the screen previous to the rotation

Description of Change

Issues Fixed

Fixes #18161

On an Android tablet, switching from landscape to portrait, and from portrait to landscape repeatedly produces this error: androidx.appcompat.widget.LinearLayoutCompat$LayoutParams cannot be cast to androidx.drawerlayout.widget.DrawerLayout$LayoutParams

This error can be easily reproduced on a physical tablet by quickly changing orientation.

dotnet#18161

The change of orientation triggers method UpdateFlyoutBehavior of src/Core/src/Handlers/FlyoutView/FlyoutViewHandler.Android.cs, that calls method LayoutViews to determine if the views are to be created in “side by side” mode or “as flyout”. The problem is after the rotation, DrawerLayout.GetPlatformViewBounds() called (for investigation) in this method shows that the Android view still has the height/width before the rotation. The MAUI code is ahead of the android reality. Waiting 100ms is enough to let Android adapt its interface.

A similar issue had been addressed by Xamarin developers: the comment on line 324 of https://github.com/xamarin/Xamarin.Forms/blob/5.0.0/Xamarin.Forms.Platform.Android/AppCompat/FlyoutPageRenderer.cs is 
//hack : when the orientation changes and we try to close the Flyout on Android		
//sometimes Android picks the width of the screen previous to the rotation
@guyvaio guyvaio requested a review from a team as a code owner August 23, 2024 05:22
@guyvaio guyvaio requested review from jfversluis and rmarinho August 23, 2024 05:22
@dotnet-policy-service dotnet-policy-service bot added the community ✨ Community Contribution label Aug 23, 2024
Copy link
Contributor

Hey there @guyvaio! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@guyvaio guyvaio changed the title Update FlyoutViewHandler.Android.cs Fix Android FlyoutPage crash when changing orientation of tablet Aug 30, 2024
After intensive testing via a custom handler in real size app.
Delay has to be increased because it seems Android needs more time when the flyout popup contains a ListView with many items.
@guyvaio
Copy link
Author

guyvaio commented Sep 6, 2024

Temporary fix https://gist.github.com/guyvaio/5ca11a6aa373c1fe486cc9e25137aeec
Hope this helps others.

@jfversluis
Copy link
Member

/azp run

Copy link

Azure Pipelines successfully started running 3 pipeline(s).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-controls-flyout Flyout community ✨ Community Contribution
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Toggling FlyoutLayoutBehavior on Android causes the app to crash
3 participants