From 820c37ab78d47757fbc5df1498e8f63d1bccf168 Mon Sep 17 00:00:00 2001 From: Shane Neuville Date: Wed, 8 Feb 2023 23:25:37 -0600 Subject: [PATCH] Add NotNullWhen to IsAlive() (#13214) --- .../ModalNavigationManager/ModalNavigationManager.Android.cs | 4 ++-- src/Core/src/Platform/Android/JavaObjectExtensions.cs | 5 +++-- .../Platform/Android/Navigation/StackNavigationManager.cs | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Controls/src/Core/Platform/ModalNavigationManager/ModalNavigationManager.Android.cs b/src/Controls/src/Core/Platform/ModalNavigationManager/ModalNavigationManager.Android.cs index a94344e99137..3b0c5771d928 100644 --- a/src/Controls/src/Core/Platform/ModalNavigationManager/ModalNavigationManager.Android.cs +++ b/src/Controls/src/Core/Platform/ModalNavigationManager/ModalNavigationManager.Android.cs @@ -257,13 +257,13 @@ protected override void OnDetachedFromWindow() void UpdateRootView(AView? rootView) { - if (_rootView.IsAlive() && _rootView != null) + if (_rootView.IsAlive()) { _rootView.LayoutChange -= OnRootViewLayoutChanged; _rootView = null; } - if (rootView.IsAlive() && rootView != null) + if (rootView.IsAlive()) { rootView.LayoutChange += OnRootViewLayoutChanged; _rootView = rootView; diff --git a/src/Core/src/Platform/Android/JavaObjectExtensions.cs b/src/Core/src/Platform/Android/JavaObjectExtensions.cs index 7b0a81c8f1c6..014c0fed5eaa 100644 --- a/src/Core/src/Platform/Android/JavaObjectExtensions.cs +++ b/src/Core/src/Platform/Android/JavaObjectExtensions.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics.CodeAnalysis; namespace Microsoft.Maui { @@ -14,7 +15,7 @@ public static bool IsDisposed(this global::Android.Runtime.IJavaObject obj) return obj.Handle == IntPtr.Zero; } - public static bool IsAlive(this Java.Lang.Object? obj) + public static bool IsAlive([NotNullWhen(true)] this Java.Lang.Object? obj) { if (obj == null) return false; @@ -22,7 +23,7 @@ public static bool IsAlive(this Java.Lang.Object? obj) return !obj.IsDisposed(); } - public static bool IsAlive(this global::Android.Runtime.IJavaObject? obj) + public static bool IsAlive([NotNullWhen(true)] this global::Android.Runtime.IJavaObject? obj) { if (obj == null) return false; diff --git a/src/Core/src/Platform/Android/Navigation/StackNavigationManager.cs b/src/Core/src/Platform/Android/Navigation/StackNavigationManager.cs index 8f2c0270e270..b7efcce2a60a 100644 --- a/src/Core/src/Platform/Android/Navigation/StackNavigationManager.cs +++ b/src/Core/src/Platform/Android/Navigation/StackNavigationManager.cs @@ -414,7 +414,7 @@ protected virtual void OnDestinationChanged(NavController navController, NavDest // If you try to access `ChildFragmentManager` and the `NavHost` // isn't attached to a context then android will throw an IllegalStateException if (_navHost.IsAlive() && - _navHost?.Context != null && + _navHost.Context is not null && _navHost.ChildFragmentManager.IsAlive()) { return _navHost.ChildFragmentManager;