Skip to content

Commit be06844

Browse files
authored
made sure to dispose properly for both platforms (#325)
1 parent e4f0c6e commit be06844

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

src/DIPS.Xamarin.UI.Android/ContextMenu/ContextMenuButtonRenderer.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,26 @@ protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
3737
{
3838
base.OnElementChanged(e);
3939

40-
if (e.NewElement is ContextMenuButton contextMenuButton)
40+
if (e.NewElement is ContextMenuButton m_contextMenuButton)
4141
{
42-
m_contextMenuButton = contextMenuButton;
42+
m_contextMenuButton = m_contextMenuButton;
4343
if (Control != null)
4444
{
4545
{
46-
contextMenuButton.Clicked += OpenContextMenu;
46+
m_contextMenuButton.Clicked += OpenContextMenu;
4747
(((Activity)Context!)).RegisterActivityLifecycleCallbacks(this);
4848
}
4949
}
50-
else
51-
{
52-
contextMenuButton.Clicked -= OpenContextMenu;
53-
(((Activity)Context!)).UnregisterActivityLifecycleCallbacks(this);
54-
}
5550
}
5651
}
5752

53+
protected override void Dispose(bool disposing)
54+
{
55+
m_contextMenuButton.Clicked -= OpenContextMenu;
56+
(((Activity)Context!)).UnregisterActivityLifecycleCallbacks(this);
57+
base.Dispose(disposing);
58+
}
59+
5860
private void OpenContextMenu(object sender, EventArgs e)
5961
{
6062
var gravity = (m_contextMenuButton.ContextMenuHorizontalOptions == ContextMenuHorizontalOptions.Right)

src/DIPS.Xamarin.UI.iOS/ContextMenu/ContextMenuButtonRenderer.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ namespace DIPS.Xamarin.UI.iOS.ContextMenu
1919
internal class ContextMenuButtonRenderer : ButtonRenderer
2020
{
2121
private ContextMenuButton m_contextMenuButton;
22+
private NSObject m_didEnterBackgroundNotificationObserver;
2223
internal static void Initialize() { }
2324

2425
protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
@@ -36,22 +37,21 @@ protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
3637
CreateMenu(); //Create the menu the first time so it shows up the first time the user taps the button
3738
Control.TouchDown += OnTouchDown;
3839
Control.ShowsMenuAsPrimaryAction = true;
39-
NSNotificationCenter.DefaultCenter.AddObserver(UIApplication.DidEnterBackgroundNotification, notification =>
40+
m_didEnterBackgroundNotificationObserver = NSNotificationCenter.DefaultCenter.AddObserver(UIApplication.DidEnterBackgroundNotification, notification =>
4041
{
4142
Control.Menu = null;
4243
Control.Menu = CreateMenu(); //Recreate the menu to close it, and to make it possible to re-open it in one tap after it went to the background
4344
});
4445
}
4546
}
4647
}
47-
else
48-
{
49-
if (Control != null)
50-
{
51-
Control.TouchDown -= OnTouchDown;
52-
NSNotificationCenter.DefaultCenter.RemoveObserver(UIApplication.DidEnterBackgroundNotification);
53-
}
54-
}
48+
}
49+
50+
protected override void Dispose(bool disposing)
51+
{
52+
Control.TouchDown -= OnTouchDown;
53+
NSNotificationCenter.DefaultCenter.RemoveObserver(m_didEnterBackgroundNotificationObserver);
54+
base.Dispose(disposing);
5555
}
5656

5757
private void OnTouchDown(object sender, EventArgs e)

0 commit comments

Comments
 (0)