Login Design Pattern

Xamarin start off by recommending that you show any login page via a PushModalAsync on the navigation stack.

I never liked that because it would keep login and registration sequences in your main app navigation, even though I know you can’t go back to it. But it could also so a flicker when it goes to your main page, then jumps to your login screen if you aren’t logged in.

I did discover a way to avoid this and then also found others had come up with this same method.

Create multiple navigation pages. One with your login page(s) and the other with your main app pages.

In your App.cs (or App.xaml.cs)


public static void SwitchNavigationStack(StackEnum stack)
{

    switch (stack)
    {
         case StackEnum.Login:
              MainPage = GetLoginStack();
              break;
         case StackEnum.Main:
              MainPage = GetMainStack();
              break;
         default:
              MainPage = GetMainStack();
    }

}

// Just as an example
private static Page GetMainStack()
{
    return new NavigationPage(new MainPage());
}

private static Page GetLoginStack()
{
    return new NavigationPage(new LoginStack());
}

Change the main navigation page as you see fit. Then no back buttons ever cross over onto a navigation flow they shouldn’t be on.

Personally I use this pattern to also break out navigation flows within an app if they get too complex.


XAMARIN.FORMS MONTHLY NEWSLETTER

JOIN 1,000+ SUBSCRIBERS

  1. Don't miss out on updates
  2. The latest info from this site, Xamarin and the community
  3. Unsubscribe at any time*

* We use MailChimp, with double opt-in, and instant unsubscribe

Microsoft MVP | Xamarin MVP | Xamarin Certified Developer | Exrin MVVM Framework | Xamarin Forms Developer | Melbourne, Australia

Related Posts

Leave A Comment?