5

I'm trying so hard to do something but i wasnt able to solve this problem. I have three pages, One is the MainPage, LoginUpPage and SignUpPage, inside of LoginUpPage has a button who navigates to SignUpPage, what i want to do is when I finish my logic navigates to another page CreatedPage, whitch contains a Label with a Message - Success and then after 2 seconds GoBack to the LoginPage, the problem is if I press the backbutton from device it will return to the last page that has the label with a message and I don't want that. I have a toobar with a BackButton to return to each page that i navigates. So far I have this:

LoginPage to SignUpPage :

Navigation.PushAsync(new SignupPage());

SignUpPage to CreatedPage :

await Navigation.PushModalAsync(new Created());

And inside of CreatedPage in my Contructor, this Method :

    public async void Redirect()
    {
        await Task.Delay(TimeSpan.FromSeconds(2));
        await Navigation.PushAsync(new LoginPage());
    }

I know by this question there's basically three ways to navigate to another page :

Navigation.PushAsync(new OtherPage()); // to show OtherPage and be able to go back

Navigation.PushAsyncModal(new AnotherPage());// to show AnotherPage and not have a Back button

Navigation.PopAsync();// to go back one step on the navigation stack

At the same question has a example how to remove from a page from stack but it doesn't work.

item.Tapped += async (sender, e) => {
await Navigation.PushAsync (new SecondPage ());
Navigation.RemovePage(this);

};

Community
  • 1
  • 1
  • maybe this can help you: http://stackoverflow.com/questions/37398962/remove-modal-page-from-navigationstack – Felix D. Sep 08 '16 at 14:26

7 Answers7

4

The number of time pages are clicked to and fro, pages gets added to navigation stack. Possibly same page can be in navigation stack many times based on number of clicks.

If there are two or pages on navigation stack.

for (int PageIndex = Navigation.NavigationStack.Count; PageIndex >= 0; PageIndex--)
{  
    Navigation.RemovePage(Navigation.NavigationStack[PageIndex]);
}

This will remove all the pages in the navigation stack excluding the Main Page (or the page at top of Navigation stack)

OR

If user is left with two pages in Navigation Stack and want to make 2nd page in navigation stack as main page (Navigationstack[1]) along with removal of first page(Navigationstack[0]) , It works as below

if (Navigation.NavigationStack.Count == 2)
{
    App.Current.MainPage.Navigation.RemovePage(Navigation.NavigationStack.First());
}
3

In that case you need to set a Root page :

//Master Detail Page

  public class RootPage : MasterDetailPage
    {
        MenuPage menuPage;

        public RootPage()
        {
            menuPage = new MenuPage(this);
            Master = menuPage;
            Detail = new NavigationPage(new HomePage());
        }
    }

//Set the Root Page
public class App : Application
{
    public App()
    {
       InitializeComponent ();

       if(NewUser || NotLoggedIn)
          {
             MainPage = new LoginPage();
          }
       else
          {
             MainPage = new RootPage();
          }
    }
}

public class LoginPage : ContentPage
    {            
        private void SignupButtonOnClicked(object sender, EventArgs eventArgs)
        {
            Navigation.PushAsync(new SignupPage());
        }
    }


public class SignupPage : ContentPage
    {            
        private void CreatedButtonOnClicked(object sender, EventArgs eventArgs)
        {
            Navigation.PushModalAsync(new CreatedPage());
        }
    }

// Set the Login Page

public class CreatedPage : ContentPage
    {

        private void CreatedButtonOnClicked(object sender, EventArgs eventArgs)
        {
            Navigation.PushModalAsync(new LoginPage());

            //Special Handel for Android Back button
            if (Device.OS == TargetPlatform.Android)
            Application.Current.MainPage = new LoginPage();
        }

    }

In this way back button will not return to previous page as it will reset the Navigation stack to root page i.e your LoginPage.

Himanshu Dwivedi
  • 7,934
  • 3
  • 31
  • 52
  • Well there's two problems here, the first one is in my App I have this `InitializeComponent();` `MainPage = new NavigationPage(new MenuPage());` I need to change the method `public App()` to `public static page`? I tryied just add the `await Navigation.PopToRootAsync();` and i got a Exception = `System.InvalidOperationException: PopToRootAsync is not supported globally on Android, please use a NavigationPage.` –  Sep 08 '16 at 14:57
  • I have updated the code, try to implement the updated code. Also handle your login credential that is after login you redirect to MenuPage. – Himanshu Dwivedi Sep 08 '16 at 17:06
  • I my rootPage i have this `MainPage = new NavigationPage(new MenuPage());` the MenuPage have a MasterDetailPage whitch contains a `` The MainPage has a List of menus. I can't start the app directly to LoginPage has to start in MenuPage. –  Sep 08 '16 at 18:35
  • And I got the same Exception `PopToRootAsync is not supported globally on Android, please use a NavigationPage` –  Sep 08 '16 at 18:43
  • For android it require a condition I have updated the code, please try to implement. – Himanshu Dwivedi Sep 08 '16 at 19:25
1

I didn't try this before but you can try this solution:

retrieve your navigation stack as a list:

var existingPages = Navigation.NavigationStack.ToList();

then remove the page you want

then set your navigation stack to the modified list

Mohamed Ibrahim Elsayed
  • 2,734
  • 3
  • 23
  • 43
  • `Navigation.NavigationStack.ToList()` returns null ? – Mohamed Ibrahim Elsayed Sep 09 '16 at 23:16
  • I don't know why it actually returns null, but make sure you are accessing the intended `Navigation` property. For example if you created a new class that extends `ContentPage` class and try to access its `Navigation` property this `Navigation` property maybe not the same as the `Navigation` property of your views so make sure that this property is the same as the one used in your views. – Mohamed Ibrahim Elsayed Sep 21 '16 at 16:58
0

App.Navigation.RemovePage(App.Navigation.NavigationStack.ElementAt(App.Navigation.NavigationStack.Count - 2));

will remove page after navigated to next page

where App.Navigation is

public static INavigation Navigation { get; set; }

in app class

harsh patel
  • 511
  • 5
  • 9
0

The easiest way I found to clear a stack and open up a new page on a clear stack

await Navigation.PushAsync(new UserJourneysTabPage()); //Push the page you want to push
            var existingPages = Navigation.NavigationStack.ToList(); 
            //get all the pages in the stack
            foreach (var page in existingPages)
            {
                    //Check they type of the page if its not the 
                    //same type as the newly created one remove it
                    if (page.GetType() == typeof(UserJourneysTabPage))

                    continue;

                Navigation.RemovePage(page);
            }

Note: This doesn't clear a page from the navigation stack if its the same type, but you can always add a property to distinguish the newly created one from the old ones

Adin Sijamija
  • 695
  • 2
  • 7
  • 19
0

Iam posting this solution for removing any desired page from the navigation stack using linq.

Here as an example, I am removing all pages except the current active page.

var currentPage = Navigation.NavigationStack[Navigation.NavigationStack.Count - 1];
var pageList = Navigation.NavigationStack.Where(y => y != currentPage).ToList();

foreach(var page in pageList)
    Navigation.RemovePage(page);
Thameem
  • 700
  • 1
  • 13
  • 38
0

The following routine works perfectly for me:

Using System.Linq;

......


try

{

 foreach (var item in Application.Current.MainPage.Navigation.NavigationStack.ToList()

{

       if (item.GetType().Name == "ClientsPage" || item.GetType().Name == "PagoPage")


       {   

           Application.Current.MainPage.Navigation.RemovePage(item);


       }
  }
         
}
catch() {
        }