Reputation: 849
I'm using the .net Maui framework for Android development and realized that when you navigate to a page, it creates the page, then hangs on to the page instance. If you navigate to the page again, it re-uses the same page instance.
In the past, with Xamarin.Forms, I have found that with some controls, re-using a page is problematic. I do not know if this is the case using Maui, but want to avoid the situation to be sure.
I haven't found any settings that let me change the default behavior.
Is there a way to configure Maui to create each page every time you navigate to the page?
Example repo: https://github.com/kkohler2/MauiShellReuseTest
Upvotes: 2
Views: 1658
Reputation: 849
I logged the issue as a bug for Maui, since there are valid reasons for NOT keeping pages around (memory/resources, etc.) It is tagged for some possible future release.
My solution was to write my own navigation service. Every time I want to navigate, I create the appropriate page, create the corresponding view model. Assign view model to the page's BindingContext, then set page to App.Current.MainPage for the Maui app. Works great.
I'm only navigating with ContentPages. I do NOT use NavigationPages, no modal pages, no flyout pages, etc.
Upvotes: 1
Reputation: 2216
Maybe you can use this , when navigate with the Button it works .
The Backbutton does not work any more. In AppSHell RegisterRoute
public AppShell()
{
InitializeComponent();
Routing.RegisterRoute(nameof(Page1),
typeof(Page1));
Routing.RegisterRoute(nameof(Page2),
typeof(Page2));
}
Then in Button
await Navigation.PushAsync(new Page2());
and Back
await Navigation.PushAsync(new Page1());
You see that the BackButton is not working any more.
Upvotes: 0