diff --git a/Mopups/Mopups.Maui/Platforms/Android/Handler/PopupPageHandler.cs b/Mopups/Mopups.Maui/Platforms/Android/Handler/PopupPageHandler.cs index 9510ab3..025a23a 100644 --- a/Mopups/Mopups.Maui/Platforms/Android/Handler/PopupPageHandler.cs +++ b/Mopups/Mopups.Maui/Platforms/Android/Handler/PopupPageHandler.cs @@ -20,7 +20,8 @@ public PopupPageHandler(IMauiContext context) protected override void ConnectHandler(ContentViewGroup platformView) { - (platformView as PopupPageRenderer).PopupHandler = this; + if (platformView is PopupPageRenderer popupPageRenderer) + popupPageRenderer.PopupHandler = this; base.ConnectHandler(platformView); } @@ -32,6 +33,8 @@ protected override ContentViewGroup CreatePlatformView() protected override void DisconnectHandler(ContentViewGroup platformView) { + if (platformView is PopupPageRenderer popupPageRenderer) + popupPageRenderer.PopupHandler = null; base.DisconnectHandler(platformView); } } diff --git a/Mopups/Mopups.Maui/Platforms/Android/Handler/PopupPageRenderer.cs b/Mopups/Mopups.Maui/Platforms/Android/Handler/PopupPageRenderer.cs index 9ddca9b..6fbed59 100644 --- a/Mopups/Mopups.Maui/Platforms/Android/Handler/PopupPageRenderer.cs +++ b/Mopups/Mopups.Maui/Platforms/Android/Handler/PopupPageRenderer.cs @@ -115,13 +115,13 @@ protected override void OnLayout(bool changed, int left, int top, int right, int systemPadding = new Thickness(); } - (PopupHandler.VirtualView as PopupPage)?.SetValue(PopupPage.SystemPaddingProperty, systemPadding); - (PopupHandler.VirtualView as PopupPage)?.SetValue(PopupPage.KeyboardOffsetProperty, keyboardOffset); + (PopupHandler?.VirtualView as PopupPage)?.SetValue(PopupPage.SystemPaddingProperty, systemPadding); + (PopupHandler?.VirtualView as PopupPage)?.SetValue(PopupPage.KeyboardOffsetProperty, keyboardOffset); if (changed) - (PopupHandler.VirtualView as PopupPage)?.Layout(new Rect(Context.FromPixels(left), Context.FromPixels(top), Context.FromPixels(right), Context.FromPixels(bottom))); + (PopupHandler?.VirtualView as PopupPage)?.Layout(new Rect(Context.FromPixels(left), Context.FromPixels(top), Context.FromPixels(right), Context.FromPixels(bottom))); else - (PopupHandler.VirtualView as PopupPage)?.ForceLayout(); + (PopupHandler?.VirtualView as PopupPage)?.ForceLayout(); base.OnLayout(changed, left, top, right, bottom); //base.OnLayout(changed, 20, 500, 1080, 2000); //base.OnLayout(changed, visibleRect.Left, visibleRect.Top, visibleRect.Right, visibleRect.Bottom); @@ -137,17 +137,19 @@ protected override void OnAttachedToWindow() var activity = Platform.CurrentActivity; var decoreView = activity?.Window?.DecorView; //activity?.Window?.SetSoftInputMode(SoftInput.AdjustResize); - Context.HideKeyboard(decoreView); + if (decoreView != null) + Context?.HideKeyboard(decoreView); base.OnAttachedToWindow(); } protected override void OnDetachedFromWindow() { - Device.StartTimer(TimeSpan.FromMilliseconds(0), () => + Application.Current?.Dispatcher.StartTimer(TimeSpan.FromMilliseconds(0), () => { var activity = Platform.CurrentActivity; var decoreView = activity?.Window?.DecorView; - Context.HideKeyboard(decoreView); + if (decoreView != null) + Context?.HideKeyboard(decoreView); return false; }); @@ -170,7 +172,7 @@ public override bool DispatchTouchEvent(MotionEvent e) { return false; } - if ((PopupHandler.VirtualView as PopupPage).BackgroundInputTransparent) + if ((PopupHandler?.VirtualView is PopupPage popupPage) && popupPage.BackgroundInputTransparent) { return base.DispatchTouchEvent(e); } @@ -192,11 +194,11 @@ public override bool OnTouchEvent(MotionEvent e) _gestureDetector.OnTouchEvent(e); - if ((PopupHandler?.VirtualView as PopupPage).BackgroundInputTransparent) + if ((PopupHandler?.VirtualView is PopupPage popupPage) && popupPage.BackgroundInputTransparent) { if ((ChildCount > 0 && !IsInRegion(e.RawX, e.RawY, PopupHandler?.PlatformView.GetChildAt(0)!)) || ChildCount == 0) { - (PopupHandler?.VirtualView as PopupPage).SendBackgroundClick(); + popupPage.SendBackgroundClick(); return false; } @@ -227,11 +229,20 @@ private async void OnBackgroundClick(object sender, MotionEvent e) if (ChildCount == 0) return; - var isInRegion = IsInRegion(e.RawX, e.RawY, PopupHandler.PlatformView.GetChildAt(0)); - - if (!isInRegion) + if (PopupHandler != null) { - (PopupHandler.VirtualView as PopupPage).SendBackgroundClick(); + var child = PopupHandler.PlatformView.GetChildAt(0); + + if (child != null) + { + var isInRegion = IsInRegion(e.RawX, e.RawY, child); + + if (!isInRegion) + { + if (PopupHandler.VirtualView is PopupPage popupPage) + popupPage.SendBackgroundClick(); + } + } } } }