RadekVyM / SimpleToolkit

SimpleToolkit is a .NET MAUI library of helpers and simple, fully customizable controls, such as SimpleShell - custom Shell implementation that allows you to create unique navigation experiences.
MIT License
425 stars 41 forks source link

Issue with Resetting MainPage in .NET MAUI #46

Closed AlexDevMobile closed 2 months ago

AlexDevMobile commented 3 months ago

First of all, I would like to thank you for developing and sharing SimpleToolkit. I have found it to be a very useful tool for .NET MAUI development.

I am encountering an issue when trying to reset the navigation in my application. The flow of my application is as follows:

To achieve this, I am using the following code in the logout method:

Application.Current.MainPage = new AppShell();

The problem occurs when I try to execute this line from another screen (for example, from a settings page or a main page). My intention is to completely reset the navigation and clear the history so that the user returns to the login screen without any trace of the previous session.

However, when executing Application.Current.MainPage = new AppShell();, I am encountering the following error:

**Java.Lang.IllegalStateException:** 'The specified child already has a parent. You must call removeView() on the child's parent first.'

**Java.Lang.IllegalStateException:** 'The specified child already has a parent. You must call removeView() on the child's parent first.'

[mono-rt] [ERROR] FATAL UNHANDLED EXCEPTION: Java.Lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
[mono-rt]    at Java.Interop.JniEnvironment.InstanceMethods.CallVoidMethod(JniObjectReference instance, JniMethodInfo method, JniArgumentValue* args) in /Users/runner/work/1/s/xamarin-android/external/Java.Interop/src/Java.Interop/obj/Release/net7.0/JniEnvironment.g.cs:line 20370
[mono-rt]    at Java.Interop.JniPeerMembers.JniInstanceMethods.InvokeVirtualVoidMethod(String encodedMember, IJavaPeerable self, JniArgumentValue* parameters) in /Users/runner/work/1/s/xamarin-android/external/Java.Interop/src/Java.Interop/Java.Interop/JniPeerMembers.JniInstanceMethods_Invoke.cs:line 66
[mono-rt]    at Android.Views.ViewGroup.AddView(View child) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/obj/Release/net8.0/android-34/mcw/Android.Views.ViewGroup.cs:line 2148
[mono-rt]    at SimpleToolkit.SimpleShell.Handlers.SimpleShellHandler.UpdateContent(IView content)
[mono-rt]    at SimpleToolkit.SimpleShell.Handlers.SimpleShellHandler.MapContent(SimpleShellHandler handler, ISimpleShell shell)
[mono-rt]    at Microsoft.Maui.PropertyMapper`2.<>c__DisplayClass5_0[[SimpleToolkit.SimpleShell.ISimpleShell, SimpleToolkit.SimpleShell, Version=4.1.0.0, Culture=neutral, PublicKeyToken=null],[SimpleToolkit.SimpleShell.Handlers.SimpleShellHandler, SimpleToolkit.SimpleShell, Version=4.1.0.0, Culture=neutral, PublicKeyToken=null]].<Add>b__0(IElementHandler h, IElement v) in D:\a\_work\1\s\src\Core\src\PropertyMapper.cs:line 172
[mono-rt]    at Microsoft.Maui.PropertyMapper.UpdatePropertyCore(String key, IElementHandler viewHandler, IElement virtualView) in D:\a\_work\1\s\src\Core\src\PropertyMapper.cs:line 47
[mono-rt]    at Microsoft.Maui.PropertyMapper.UpdateProperties(IElementHandler viewHandler, IElement virtualView) in D:\a\_work\1\s\src\Core\src\PropertyMapper.cs:line 82
[mono-rt]    at Microsoft.Maui.Handlers.ElementHandler.SetVirtualView(IElement view) in D:\a\_work\1\s\src\Core\src\Handlers\Element\ElementHandler.cs:line 79
[mono-rt]    at Microsoft.Maui.Handlers.ViewHandler`2[[SimpleToolkit.SimpleShell.ISimpleShell, SimpleToolkit.SimpleShell, Version=4.1.0.0, Culture=neutral, PublicKeyToken=null],[Android.Views.ViewGroup, Mono.Android, Version=0.0.0.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065]].SetVirtualView(IView view) in D:\a\_work\1\s\src\Core\src\Handlers\View\ViewHandlerOfT.cs:line 53
[mono-rt]    at Microsoft.Maui.Handlers.ViewHandler`2[[SimpleToolkit.SimpleShell.ISimpleShell, SimpleToolkit.SimpleShell, Version=4.1.0.0, Culture=neutral, PublicKeyToken=null],[Android.Views.ViewGroup, Mono.Android, Version=0.0.0.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065]].SetVirtualView(IElement view) in D:\a\_work\1\s\src\Core\src\Handlers\View\ViewHandlerOfT.cs:line 56
[mono-rt]    at Microsoft.Maui.Controls.Element.SetHandler(IElementHandler newHandler) in D:\a\_work\1\s\src\Controls\src\Core\Element\Element.cs:line 922
[mono-rt]    at Microsoft.Maui.Controls.Element.set_Handler(IElementHandler value) in D:\a\_work\1\s\src\Controls\src\Core\Element\Element.cs:line 864
[mono-rt]    at Microsoft.Maui.Controls.VisualElement.Microsoft.Maui.IElement.set_Handler(IElementHandler value) in D:\a\_work\1\s\src\Controls\src\Core\VisualElement\VisualElement.cs:line 2061
[mono-rt]    at Microsoft.Maui.Platform.ElementExtensions.ToHandler(IElement view, IMauiContext context) in D:\a\_work\1\s\src\Core\src\Platform\ElementExtensions.cs:line 96
[mono-rt]    at Microsoft.Maui.Platform.ElementExtensions.ToPlatform(IElement view, IMauiContext context) in D:\a\_work\1\s\src\Core\src\Platform\ElementExtensions.cs:line 127
[mono-rt]    at Microsoft.Maui.Platform.ContainerView.SetView(IElement view, Boolean forceRefresh) in D:\a\_work\1\s\src\Core\src\Platform\Android\ContainerView.cs:line 76
[mono-rt]    at Microsoft.Maui.Platform.ContainerView.set_CurrentView(IElement value) in D:\a\_work\1\s\src\Core\src\Platform\Android\ContainerView.cs:line 55
[mono-rt]    at Microsoft.Maui.Platform.ElementExtensions.ToContainerView(IElement view, IMauiContext context) in D:\a\_work\1\s\src\Core\src\Platform\Android\ElementExtensions.cs:line 11
[mono-rt]    at Microsoft.Maui.Platform.NavigationRootManager.Connect(IView view, IMauiContext mauiContext) in D:\a\_work\1\s\src\Core\src\Platform\Android\Navigation\NavigationRootManager.cs:line 56
[mono-rt]    at Microsoft.Maui.Handlers.WindowHandler.CreateRootViewFromContent(IWindowHandler handler, IWindow window) in D:\a\_work\1\s\src\Core\src\Handlers\Window\WindowHandler.Android.cs:line 91
[mono-rt]    at Microsoft.Maui.Handlers.WindowHandler.MapContent(IWindowHandler handler, IWindow window) in D:\a\_work\1\s\src\Core\src\Handlers\Window\WindowHandler.Android.cs:line 27
[mono-rt]    at Microsoft.Maui.PropertyMapper`2.<>c__DisplayClass5_0[[Microsoft.Maui.IWindow, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Handlers.IWindowHandler, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].<Add>b__0(IElementHandler h, IElement v) in D:\a\_

I would appreciate any insights or guidance on resolving this issue. My intention is to completely reset the navigation and clear the history, ensuring the user returns to the login screen without any traces of the previous session.

Thank you once again for your contribution to the community and for your assistance in this matter.

RadekVyM commented 3 months ago

Hi @AlexDevMobile,

I am glad you find the library useful.

I managed to reproduce your issue and fixed it hopefully. Please try the newest 4.1.3 release of SimpleToolkit.SimpleShell.

AlexDevMobile commented 2 months ago

Hi,

Thank you for your support and sorry for the delay, but the provided solution worked correctly and the error no longer occurs .

Again, thank you for facilitating shell customization with your project.