Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

How to navigate to a page and reset the 'stack'

AndrewArnottAndrewArnott USMember ✭✭

My app has a few introductory screens for first launch. Once the user gets through these, I want to transition the user to the "main screen" in a way where the back button will not take the user back to the opening screens. How can this be achieved in Xamarin.Forms?

Posts

  • RichardCampbellRichardCampbell GBMember ✭✭

    The suggestions I have seen elsewhere in this forum and in the samples is that you use the .PopModalAsync() and .PushModalAsync() methods to display your intro screen(s), but I can't get these to work the way they should.

    When I call await Navigation.PushModalAsync(new LoginPage()); it displays the Login page as expected, but then I simply press back and I get back to the calling page. I wanted it to stay 'Modal' until I call Navigation.PopModalAsync()

    Hopefully someone can provide guidance.

  • AndrewArnottAndrewArnott USMember ✭✭

    I haven't been able to figure out the difference between a modal and non-modal page.

  • AndrewArnottAndrewArnott USMember ✭✭

    Thanks. I don't care about WinPhone because my WinPhone app is now 8.1 XAML based (wpa81) and evidently Xamarin.forms doesn't support that (yet?).
    Your info about modal pushes sounds like my solution might be to initialize my NavigationPage to my ordinary Main page, and then immediately do a PushModal for my first intro screen when that's appropriate. When the intro screen flow is complete, I can PopModal and the user is then in the right state for the main experience. Am I piecing this together right?

  • TheRealJasonSmithTheRealJasonSmith USXamarin Team Xamurai

    thats 100% correct and the normal way to do things. As for 8.1 support, the produced app does run on 8.1, but correct you cant write custom renderers targeting the new API's yet. We unfortunately had no time to forward port after MS made their release.

  • AndrewArnottAndrewArnott USMember ✭✭

    Thanks, Jason.
    BTW, by 8.1 support I meant wpa81, or a Windows Phone 8.1 app targeting WinRT instead of Silverlight. And it appears there is no support for that yet in Xamarin.Forms.

  • Is it possible to manipulate the navigation stack, either by Forms or by native implementation.
    I need to remove a some pages from my stack at a certain point - issue is that Android has a hardware back button so I need to be able to remove these. Any suggestions?

  • MihaMarkicMihaMarkic SI ✭✭✭✭

    I am trying to solve this same issue right now (back button). My current solution is, since Forms won't give me the top modal page, to track the viewmodel (using Stack) when doing PushModalAsync, so I know what viewmodel is on top. Then I simply catch Activity's OnBackPressed and delegate it to top viewmodel.

  • Chris.4142Chris.4142 USMember

    This is a pretty massive problem, and I haven't found a proper solution using xamarin forms. I was using the method Andrew outlined, but this does not work properly on android, where a modal (the log in page) can be removed with the hardware back button. Is there currently any way to present a page (modally or not), which the user cannot navigate back from?

    Or is there at least something in the works? Its a bit of an issue when displaying a log in screen (which most apps need) is such a problem.

  • MaxPoolMaxPool USMember ✭✭

    Craig - GREAT demo reference. +10 bonus points for you.

    The problem I was running into with the modal solution was that it flashes the main page as the modal pops up. This solution is smooth as ice.

  • MihaMarkicMihaMarkic SI ✭✭✭✭

    The demo is nice but it is just a workaround the fact that page navigation is seriously lacking features. Like reset. The same would be easily done on platform independent level if there was a method Navigation.Reset.

  • MaxPoolMaxPool USMember ✭✭

    Navigation.PopToRootAsync() ?

  • MihaMarkicMihaMarkic SI ✭✭✭✭
    edited September 2014

    I don't think that'd work in case where you first show login screen and only after successful login you navigate to main page.
    (as in the demo)

  • VincentPoirierVincentPoirier CAMember ✭✭

    Was there any progress on this matter? In the same way, I have a login screen that I only want to be accessed to through a top-right 3-dot menu. (android) Pushing back shows it if it's modal and pushing the top-left back button shows it too if not modal.

    We're still searching for another thing too, we're trying to change the MasterDetail top-left icon for a .png (a three bar standard menu) that opens the Master menu.

  • VincentPoirierVincentPoirier CAMember ✭✭
    edited December 2014

    Maybe I found a solution (couldn't find it elsewhere)!

    `void btnLoginClicked(Object sender, EventArgs EventArgs){
    var activityIndicator = this.FindByName("actIndicator");
    actIndicator.IsRunning = true;

        actIndicator.IsRunning = false;
        App.Current.MainPage = new HomePage ();
    

    }`

    Please give us the reference links if anything about this topic is elsewhere!

    PS: I don't know why the markdown is letting me down.

  • SamuelCadimaSamuelCadima USMember
    edited March 2015

    Solved the problem of removing the login page with this:

    async void onLoginClick(object sender, EventArgs e)
    {
    await Navigation.PushAsync(new MainView());
    Navigation.RemovePage(this);
    }

    It seems to work on WP and Android

  • Daniel.9388Daniel.9388 USMember

    @ashalva Your solution doesn't seem to work on Android with Modal views. This is the problem I'm facing right now.

    I'm trying to have a flow in the pages like this :

    LoginPage -> LoginAuthPage (Xamarin.Auth) + Renderer -> (User logs in) -> TabbedPage

    I'm facing two issues. The first is, if I want to keep this flow, the user will be able to get back to the LoginPage by pressing the back button 2 times, and only the third will exit the app. The second issue is that I can't remove the Pages in the stack, because they are Modal and RemovePage throws an exception when doing it.

    Solution : Override the Back button behavior ? Reset the stack in another way ?

    I've tried another solution like this :

    TabbedPage => Redirected to (*) => LoginPage -> LoginAuthPage (Xamarin.Auth) + Renderer -> (User logs in) -> Back to TabbedPage (PopToRoot)

    • : By specifying my Pages in TabbedPage as BaseContentPage, it checks if the user is logged or not in the "OnAppearing" event, and push the LoginPage in the stack if needed.

    The issues I'm facing here is that I can't PopToRoot the ModalStack (it throws an exception saying it is not globally supported). The other one is the same as the first try, I can't reset the stack and I "can't" hardcode the number of Pops I have to do until reaching the TabbedPage.

    So I'm asking you, what do you think about this case ? And do you have any ideas ? Comments are also welcome.

  • Daniel.9388Daniel.9388 USMember
    edited April 2015

    By using the second way I mentionned, I used this :

    while (Navigation.ModalStack.Count > 1) Navigation.PopModalAsync();

    Where "1" corresponds to my TabbedPage. It works, but I don't like the way we have to handle these cases.

  • FrancMoralesFrancMorales JPMember ✭✭

    Ankur, works like a charm.

    Any issues regarding leaks or whatever?

  • AnkurAnkur USMember ✭✭

    @Franc ,

    I haven't seen any issues, nor have I specifically checked if it leaks to memory leaks. Any specific reason you were concerned for Memory leaks with this code snippet?

  • 15mgm1515mgm15 USMember ✭✭✭✭

    @Ankur I owe you a beer! Thanks for your solution.

  • AneeshOdataAneeshOdata USMember ✭✭

    Hi,

    Xamarin.IOS Application Close Issue Using Navigation.PopAsync();

    Application closing issue whenI try to move back to previous page. When I clicking button to move to the previous page (Current Page is ContentPage and Previous Page is Tabbed Page) , app getting closed. But its working properly on
    Android & Windows Phone.

    await Navigation.PopAsync(true);
    Navigation.RemovePage(this);

    NavigationStack containing 4 Page.

  • AneeshOdataAneeshOdata USMember ✭✭

    @CraigDunn
    Xamarin.IOS Application Close Issue Using Navigation.PopAsync();

    Application closing issue whenI try to move back to previous page. When I clicking button to move to the previous page (Current Page is ContentPage and Previous Page is Tabbed Page) , app getting closed. But its working properly on
    Android & Windows Phone.

    await Navigation.PopAsync(true);
    Navigation.RemovePage(this);

    NavigationStack containing 4 Page.

  • DquaglioDquaglio ITMember

    Is there any way to access the previous Page when I hit the back button? I'd like to refresh the previous view when I hit the back button on the newest page.

  • SuarezDiegoSuarezDiego UYMember ✭✭

    @Dquaglio said:
    Is there any way to access the previous Page when I hit the back button? I'd like to refresh the previous view when I hit the back button on the newest page.

    You can get the previous pages from Navigation.NavigationStack. Btw, refreshing the previous page should be done on the previous page, like overriding OnAppearing(), or attaching to the NavigationPage events.

  • GiggasGiggas PTMember

    I want to navigate from a MasterDetailPage to the Login Page, when i click the logout button the Login page appears for a second and returns to the MasterDetailPage. How can I make the Login page to stay open??? I'm working on a UWP project

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    @Giggas post a repo

  • GiggasGiggas PTMember

    @AlessandroCaliaro said:
    @Giggas post a repo

    private async void Logout_OnClicked(object sender, EventArgs e) { await UserManager.Instance.LogoutAsync(); Application.Current.MainPage = new NavigationPage(new Login()); }

    solved :smiley:

  • masoudcherageemasoudcheragee USMember

    @SuarezDiego said:
    Another approach that worked for me:

    Navigation.InsertPageBefore (new MainPage(), this);
    Navigation.PopAsync ();
    

    Brilliant Idea worked for me , thanks Buddy

  • N_BauaN_Baua INMember ✭✭✭✭✭

    @Giggas ,

    The above idea of InsertPageBefore is also great.
    Another approach would be (MVVM way) - Just posting it here is someone want to use it with MVVM-Prism

    Helpers.Utility.Logout();\\ this method is to kill all your settings OR variables
    await _navigationService.NavigateAsync("app:///Navigation/Login?appModuleRefresh=OnInitialized");

    where Navigation is alias to your NavigationPage and Login is your view after LogOut

    Note the appModuleRefresh is the key to success :)

    Hope this helps.

    Regards,
    N Baua

  • HarshithaLSHarshithaLS USMember ✭✭

    async void LoadLoginPage()
    {
    App.Current.MainPage.Navigation.InsertPageBefore(new UserLogin(),this);
    await Navigation.PushAsync();
    }

    Loads Navigation stack with current page and pops the recent pages navigated.
    Popping of recent pages in navigation stack loads the pages faster.

Sign In or Register to comment.