[NUI] Override Dispose() in Navigator
authorJaehyun Cho <jae_hyun.cho@samsung.com>
Tue, 22 Dec 2020 12:31:09 +0000 (21:31 +0900)
committerhuiyueun <35286162+huiyueun@users.noreply.github.com>
Mon, 11 Jan 2021 05:49:43 +0000 (14:49 +0900)
Pages pushed into Navigator are disposed if Navigator is disposed.

To override Dispose() in Navigator, two dictionaries are required to
remove disposed navigator from dictionary.

TODO: Needs to consider how to remove disposed window from dictionary.

src/Tizen.NUI.Components/Controls/Navigation/Navigator.cs
test/Tizen.NUI.Samples/Tizen.NUI.Samples/Samples/NavigatorSample.cs

index acbcb1c..10f371c 100755 (executable)
@@ -37,7 +37,10 @@ namespace Tizen.NUI.Components
         //This will be replaced with view transition class instance.
         private Animation _newAnimation = null;
 
+        //TODO: Needs to consider how to remove disposed window from dictionary.
+        //Two dictionaries are required to remove disposed navigator from dictionary.
         private static Dictionary<Window, Navigator> windowNavigator = new Dictionary<Window, Navigator>();
+        private static Dictionary<Navigator, Window> navigatorWindow = new Dictionary<Navigator, Window>();
 
         /// <summary>
         /// Creates a new instance of a Navigator.
@@ -310,6 +313,38 @@ namespace Tizen.NUI.Components
         }
 
         /// <summary>
+        /// Disposes Navigator and all children on it.
+        /// </summary>
+        /// <param name="type">Dispose type.</param>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        protected override void Dispose(DisposeTypes type)
+        {
+            if (disposed)
+            {
+                return;
+            }
+
+            if (type == DisposeTypes.Explicit)
+            {
+                foreach (Page page in NavigationPages)
+                {
+                    Utility.Dispose(page);
+                }
+                NavigationPages.Clear();
+
+                Window window;
+
+                if (navigatorWindow.TryGetValue(this, out window) == true)
+                {
+                    navigatorWindow.Remove(this);
+                    windowNavigator.Remove(window);
+                }
+            }
+
+            base.Dispose(type);
+        }
+
+        /// <summary>
         /// Returns the default navigator of the given window.
         /// </summary>
         /// <returns>The default navigator of the given window.</returns>
@@ -332,6 +367,7 @@ namespace Tizen.NUI.Components
             defaultNavigator.HeightResizePolicy = ResizePolicyType.FillToParent;
             window.Add(defaultNavigator);
             windowNavigator.Add(window, defaultNavigator);
+            navigatorWindow.Add(defaultNavigator, window);
 
             return defaultNavigator;
         }
index 74ece7b..6eea4be 100755 (executable)
@@ -81,20 +81,12 @@ namespace Tizen.NUI.Samples
             {
                 NUIApplication.GetDefaultWindow().Remove(navigator);
 
-                firstButton.Dispose();
+                navigator.Dispose();
+                navigator = null;
                 firstButton = null;
-
-                secondButton.Dispose();
-                secondButton = null;
-
-                firstPage.Dispose();
                 firstPage = null;
-
-                secondPage.Dispose();
+                secondButton = null;
                 secondPage = null;
-
-                navigator.Dispose();
-                navigator = null;
             }
         }
     }