Reputation: 2399
I'd like to redirect user to the main page after successful login, so I don't want him to have a back button or be able to get to the login page again.
I have redirected user to the login page with something like this:
if (isAuthenticated)
{
MainPage = new General.Pages.MainPage();
}
else
{
MainPage = new Account.Pages.Login();
}
I'm using this code for successful login, which doesn't work and gives an exception:
await Navigation.PopAsync(false);
await Navigation.PushAsync(new General.Pages.MainPage());
The given exception is:
System.InvalidOperationException: PopAsync is not supported globally on Android, please use a NavigationPage.
And no need to mention that the mentioned NavigationPage
doesn't work neither.
Upvotes: 0
Views: 1022
Reputation: 11787
There are 2 ways you could approach this.
Set you App.MainPage
initially in the constructor of App.cs as your home page. Then in your home page's OnAppearing
event or if it has a ViewModel then its Initialising method check if user is authenticated. If yes load his data. If not the show your Login page as a Modal. Once login is successful, you can pop the modal of login page and load the data for the home page. Also if required you can handle the back button event in the login page to not allow the user to go out of the modal page.
Drawback - The home page would be visible for a second before the login modal shows up.
In the constructor of App.cs check if the user is authenticated. If user is authenticated then show home page by setting it as the MainPage
. Else set the MainPage
as login page. Once login is successful again set the MainPage
.
Drawback - Checking isAuthenticated in the constructor might make the initial load of the application seem slower.
And for the error you encounter is because you did not push any pages into the navigation stack. Hence your pop wont work.
Upvotes: 2
Reputation: 3388
var firstPage = isAuthenticated ? new General.Pages.MainPage() : new Account.Pages.Login();
MainPage = new NavigationPage(firstPage);
If you will use
mentioned
NavigationPage
properly, PopAsync
will work.
Upvotes: 1
Reputation: 2399
I just figured it out, you can change MainPage
at any point using this code:
App.Current.MainPage = new General.Pages.MainPage();
I leave this post to be, if it can possibly help other users.
Upvotes: 2