[Android] Add guard and add missing unsubscribe on MasterDetailPageRenderer (#8704)
authorKevin Petit <kevin.petit@outlook.com>
Tue, 3 Dec 2019 10:36:55 +0000 (11:36 +0100)
committerRui Marinho <me@ruimarinho.net>
Tue, 3 Dec 2019 10:36:55 +0000 (10:36 +0000)
Xamarin.Forms.Platform.Android/AppCompat/MasterDetailPageRenderer.cs

index 4431abb..131ec78 100644 (file)
@@ -117,10 +117,25 @@ namespace Xamarin.Forms.Platform.Android.AppCompat
 
                        if (oldElement != null)
                        {
+                               Device.Info.PropertyChanged -= DeviceInfoPropertyChanged;
+
                                ((IMasterDetailPageController)oldElement).BackButtonPressed -= OnBackButtonPressed;
+
                                oldElement.PropertyChanged -= HandlePropertyChanged;
                                oldElement.Appearing -= MasterDetailPageAppearing;
                                oldElement.Disappearing -= MasterDetailPageDisappearing;
+
+                               RemoveDrawerListener(this);
+                       
+                               if (_detailLayout != null)
+                               {
+                                       RemoveView(_detailLayout);
+                               }
+
+                               if (_masterLayout != null)
+                               {
+                                       RemoveView(_masterLayout);
+                               }
                        }
 
                        if (newElement != null)
@@ -168,9 +183,6 @@ namespace Xamarin.Forms.Platform.Android.AppCompat
 
                                Presented = newElement.IsPresented;
 
-                               if (element != null && !string.IsNullOrEmpty(element.AutomationId))
-                                       SetAutomationId(element.AutomationId);
-
                                newElement.SendViewInitialized(this);
                        }
 
@@ -294,10 +306,18 @@ namespace Xamarin.Forms.Platform.Android.AppCompat
                                        MasterDetailPageController.CanChangeIsPresented = true;
                                        //hack : when the orientation changes and we try to close the Master on Android         
                                        //sometimes Android picks the width of the screen previous to the rotation              
-                                       //this leaves a little of the master visible, the hack is to delay for 50ms closing the drawer
+                                       //this leaves a little of the master visible, the hack is to delay for 100ms closing the drawer
                                        await Task.Delay(100);
+
+                                       //Renderer may have been disposed during the delay
+                                       if (_disposed)
+                                       {
+                                               return;
+                                       }
+
                                        CloseDrawer(_masterLayout);
                                }
+
                                UpdateSplitViewLayout();
                        }
                }