Anand
Anand

Reputation: 1959

Xamarin forms navigation backpress prevent on logout

I am creating a xamarin forms app.My app flow is LOGIN-->MAINPAGE. The Mainpage is a Bottombarpage contains three options. 1.DashBoard, 2.Settings, 3.User . I added a logout icon on Dashboard class toolbar. My problem is whenever user click on logout, screen will navigate to login page. But if user press backbutton it will go to previous mainpage. I want to disable that.

Iam navigating from loginpage to Main page is like this.

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

My App.xaml.cs - I using a validation for navigation to main page

if (Settings.Generalsettings != string.Empty)
            {

                MainPage = new NavigationPage(new MainPage());
            }
            else {
                MainPage = new NavigationPage(new Login());

            }

My logout button click on Dashboard.cs

   private  void logout_Clicked(object sender,EventArgs e)
        {

                    Settings.ClearAllData();           
                    Navigation.PushAsync(new Login());               

        }

Upvotes: 0

Views: 1853

Answers (2)

Prasanth
Prasanth

Reputation: 329

Here you pushing a login page again, because the login page is already in navigation stack. So when you hit back button it show the login page.You can do like this MainPage = new NavigationPage(new MainPage()); From App.Xaml.cs page call

public App()
    {
        InitializeComponent(); MainPage = new LoginPage();

}

  // and when login button pressed from login page do like this
private  void loginButton_Clicked(object sender,EventArgs e)
    {
        NavigationPage navigationRootPage = new NavigationPage(new MainPage());
        navigationRootPage.BarTextColor = Color.White;
        MainPage = navigationRootPage;

    }

// when you hit logout button

private  void logout_Clicked(object sender,EventArgs e)
    {

                Settings.ClearAllData();           
                MainPage = new LoginPage();             

    }

Upvotes: 1

K K
K K

Reputation: 996

There are two ways one if you are using a view model then you can simply navigate like this

await _navigationService.NavigateAsync("app:///NavigationPage/Splash");

Second is if you are using forms only then you can set the main page with a login page

MainPage = new LoginPage();

This will clear your back stack and navigate to the particular page.

Upvotes: 1

Related Questions