locked
Prism Navigation RRS feed

  • Question

  • User32053 posted

    I would like to remove the Login page after navigation. I am using PRISM. It doesn't matter just login page. It could be any page. I couldn't find ant way to do that.

    Please help me on it. Thanks in advance.

    Friday, February 24, 2017 2:32 AM

Answers

  • User76049 posted

    @nithinshiriya

    This is what I do in my prism app

    ``` protected override void OnInitialized() { InitializeComponent();

            // Get user token
            if (!Equals(token, null))
            {
                NavigationService.NavigateAsync("app:///AppMasterDetailPage/MainNavigationPage/HomePage");
            }
            else
            {
                NavigationService.NavigateAsync("app:///MainNavigationPage/LoginPage");
            }
    
        }
    

    ```

    The app:/// of the URI resets the navigation stack. The only downside to this page is that in the current version (6.3.0 pre1) is that Destroy if not called on pages and vm's that implement IDestructible, that is resolved in forthcoming pre2 nuget package.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Friday, February 24, 2017 9:00 AM

All replies

  • User154141 posted

    I don't know if I'm understanding correctly,

    LoginPage > HomePage > OtherPage

    Once the Login is done, you want to go to HomePage and don't want to be able to go back to the LoginPage

    When your app starts open LoginPage normally, and when the Login process is completed create a NavigationPage with your HomePage, in prism I think is something like this:

    NavigationService.NavigateAsync("NavigationPage/HomePage");

    once in there you can remove the LoginPage from the NavigationStack.

    Friday, February 24, 2017 6:18 AM
  • User76049 posted

    @nithinshiriya

    This is what I do in my prism app

    ``` protected override void OnInitialized() { InitializeComponent();

            // Get user token
            if (!Equals(token, null))
            {
                NavigationService.NavigateAsync("app:///AppMasterDetailPage/MainNavigationPage/HomePage");
            }
            else
            {
                NavigationService.NavigateAsync("app:///MainNavigationPage/LoginPage");
            }
    
        }
    

    ```

    The app:/// of the URI resets the navigation stack. The only downside to this page is that in the current version (6.3.0 pre1) is that Destroy if not called on pages and vm's that implement IDestructible, that is resolved in forthcoming pre2 nuget package.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Friday, February 24, 2017 9:00 AM
  • User251799 posted

    @NMackay said: Destroy if not called on pages and vm's that implement IDestructible, that is resolved in forthcoming pre2 nuget package.

    If you have a navigation page in the mix, you can work around that by subscribing to the pushed / popped events. Here's my snippet (which also covers activation with two states):

    public class NavigationRootPage : NavigationPage
    {
        private Page activePage;
    
        public NavigationRootPage()
        {
            Popped += OnPopped;
            Pushed += OnPushed;
        }
    
        private void OnPushed(object sender, NavigationEventArgs e)
        {
            SetStatus(activePage, false, false);  //not active, not destroyed
            activePage = CurrentPage;
            SetStatus(activePage, true, false); //active, not reentrant
        }
    
        private void OnPopped(object sender, NavigationEventArgs e)
        {
            SetStatus(activePage, false, true);  //not active, destroyed
            activePage = CurrentPage;
            SetStatus(activePage, true, true); //active, reentrant
        }
    
    
        private void SetStatus(Page page, bool status, bool flag)
        {
            var aas = new[] {page as IActivationAware, page?.BindingContext as IActivationAware};
    
            foreach (var aa in aas)
            {
                if (aa == null) continue;
                aa.IsActive = status;
                if (status)
                {
                    aa.OnActivated(flag);
                }
                else
                {
                    aa.OnDeactivated(flag);
                }
            }
        }
    }
    
    Friday, February 24, 2017 10:32 AM
  • User32053 posted

    @NMackay

    My question is after successful login, App will navigate to the home page, But if I press the back button on the home page it will going back to "login" page. I don't want to go back to login page. Login page should be removed from the navigation stack.

    Friday, February 24, 2017 3:58 PM
  • User76049 posted

    After you login (in your navigation viewmodel) just navigate to app:///NavigationPage/HomePage

    That will reset the navigation stack so your login page won't be in the stack.

    Friday, February 24, 2017 4:02 PM
  • User32053 posted

    @NMackay

    I Should be using 6.3.0 pre1 or it doesn't matter. Because I am getting now "Java.Lang.IllegalStateException: Activity has been destroyed" when I press back button.

    Friday, February 24, 2017 7:13 PM
  • User116727 posted

    @nithinshiriya @NMackay is correct. Use an absolute path when navigating to reset the navigation stack. The exception you are getting has nothing to do with Prism. That is a known XF problem.

    Saturday, February 25, 2017 1:19 AM
  • User32053 posted

    @BrianLagunas @NMackay Thanks for your support. The exception has been resolved after updating Xamarin forms to 6.3.0 pre2 .

    Saturday, February 25, 2017 2:34 AM
  • User195916 posted

    @PhilippSumi Can you please explain wider what is you doing in your code and how it works?

    Wednesday, March 15, 2017 1:45 PM
  • User227972 posted

    @BrianLagunas @NMackay I have tried you suggestion. But no luck. I'm using Prims.forms 7.0.0.336-pre version.

    I tried like below 1. await _navigationService.NavigateAsync(new Uri("app:///Main/Navigation/HomePage",UriKind.Absolute)); 2. await _navigationService.NavigateAsync(new Uri("Main/Navigation", UriKind.Relative)); - if we press back button old page is showing. 3. await _navigationService.NavigateAsync("app:///Main/Navigation/HomePage");

    App is relaunching from splash screen. In splash screen i have set nohistory = true and finished the splash activity when done.

    I don't know what I'm missing. can you give me quick suggestion.

    Thursday, December 28, 2017 8:43 AM