I opened page-1 then open page-2 then open page-3 then how to back to page-1 from page-3?
I write Console.WriteLine("Page1====OnAppearing");
in pages' background code. Here is page1's background code.
public partial class Page1 : ContentPage { public Page1() { InitializeComponent(); } protected override void OnAppearing() { base.OnAppearing(); Console.WriteLine("Page1====OnAppearing"); } private async void Button_Clicked(object sender, EventArgs e) { await Shell.Current.GoToAsync("Page2"); } }
Then here is page2‘s background code.
public partial class Page2 : ContentPage { public static ContentPage page2; public Page2() { page2 = this; InitializeComponent(); } protected override void OnAppearing() { base.OnAppearing(); Console.WriteLine("Page2====OnAppearing"); } private async void Button_Clicked(object sender, EventArgs e) { await Shell.Current.GoToAsync("Page3"); Navigation.RemovePage(this); } protected override void OnDisappearing() { Console.WriteLine("Page2====OnDisappearing"); base.OnDisappearing(); } }
Here page3's background code.
public partial class Page3 : ContentPage { protected override void OnAppearing() { base.OnAppearing(); Console.WriteLine("Page3====OnAppearing"); } public Page3() { InitializeComponent(); } }
If I in the page2, then I navigate to the page3, please see the output information in the GIF.
Xamarin forums are migrating to a new home on Microsoft Q&A!
We invite you to post new questions in the Xamarin forums’ new home on Microsoft Q&A!
For more information, please refer to this sticky post.
Answers
If the page1 is root page, When you execute the navigate in Page3, you can execute
Navigation.PopToRootAsync();
directly.@LeonLu if page1 is not the root page then what to do?
@lescper First of all, You navigate step from
Aboutpage
=>Page1
=>Page2
=>Page3
If page1 is not the root page, we can add
Navigation.RemovePage(this);
inPage2
'sOnDisappearing()
method.Here is running GIF.
If you want to achieve the
SingleTask
in the android, it cannot be achieved. Because xamarin.forms have only one Activity when you diplay the Page.Xamarin forums are migrating to a new home on Microsoft Q&A!
We invite you to post new questions in the Xamarin forums’ new home on Microsoft Q&A!
For more information, please refer to this sticky post.
@LeonLu thankyou. can I use
Shell.Current.GoToAsync("../../");
?there is an OnAppearing in page1 and if you remove page2 then the function will be run.
and on this function it will open another page2.
can you remove the page2 but do not trigger OnAppearing in page1?
When you click an button navigate the page3, you can remove the
Navigation.RemovePage(this);
fromOnDisappearing
method to the Button_Clicked method(blew theShell.Current.GoToAsync("Page3");
), it will not execute theOnAppearing
in page1.Xamarin forums are migrating to a new home on Microsoft Q&A!
We invite you to post new questions in the Xamarin forums’ new home on Microsoft Q&A!
For more information, please refer to this sticky post.
please try .it will execute the OnAppearing.
I write
Console.WriteLine("Page1====OnAppearing");
in pages' background code. Here is page1's background code.Then here is page2‘s background code.
Here page3's background code.
If I in the page2, then I navigate to the page3, please see the output information in the GIF.
Xamarin forums are migrating to a new home on Microsoft Q&A!
We invite you to post new questions in the Xamarin forums’ new home on Microsoft Q&A!
For more information, please refer to this sticky post.