Reputation: 1540
I am developing an app that starts with a login page, called LoginPage
.
Once logged in a different page is viewed, called UserPage
.
Once LoginPage
has been popped and UserPage
has been pushed, I press the back button on the Android device, which takes me to the home screen.
Once at the home screen I reopen the app from the app manager (the little square button at the lower right side of the device) and the LoginPage
reappears.
Why is that?
My code to pop the LoginScreen
is as following:
Application.Current.MainPage = new UserPage();
await Navigation.PopToRootAsync(true);
EDIT 1 Main app method
public App()
{
InitializeComponent();
var navPage = new NavigationPage(new LoginPage());
NavigationPage.SetHasNavigationBar(navPage.CurrentPage, false);
MainPage = navPage;
}
Upvotes: 2
Views: 1441
Reputation: 2027
Once at the home screen I reopen the app from the app manager (the little square button at the lower right side of the device) and the LoginPage reappears.
According your code :
Application.Current.MainPage = new UserPage();
await Navigation.PopToRootAsync(true);
Pop up the login page and set the main page is UserPage
. At this time your page stack only have the UserPage
. When you click the back button. The app has been killed by the system.
But android will keep app the shot cut at the app manager. When you click the shot cut in fact you are reopening your app. And your MainPage is LoginPage
so you will see your LoginPage
again.
I have added the output message in the app init function:
App Init:
public App()
{
InitializeComponent();
System.Diagnostics.Debug.WriteLine("ini app again");
MainPage = new NavigationPage(new MainPage());
}
MainPage Init
public MainPage()
{
System.Diagnostics.Debug.WriteLine("init Mainpage again");
InitializeComponent();
}
When I click the button pop up the mainpage and go to page1:
private async void bt1_Clicked(object sender, EventArgs e)
{
Application.Current.MainPage = new Page1();
await Navigation.PopToRootAsync(true);
}
Run you my code you will find when you click the back button you actually open the app again and it will show the mainpage again:
Upvotes: 2
Reputation: 5768
I think you should take a look if your app enter in
protected override void OnResume()
or
protected override void OnStart()
Can be that when you put your app in background, your app has been killed by OS so, when you reopen it, it start from the "Start", so from the "Login".
I think you can save a boolean flag in your
Application.Current.Properties
When your app starts, if this flag is true you start from UserPage, otherwise you Start from LoginPage
Upvotes: 0
Reputation: 1381
Your application's main page does not have a navigation stack where it could store the pushed/popped pages.
Can you just try and change your code to be:
Application.Current.MainPage = new NavigationPage(new UserPage());
This should create a NavigationPage wrapper around your userpage that will have the pre-built stack to use for popping and pushing to.
Upvotes: 0