button back to my app in the background and when y

2019-08-26 22:16发布

问题:

I am developing an app in Xamarin.Forms, before I was trying to make a master detail page to become my MainPage when I logged in to my app, this I have already achieved. Now I have the problem that when I use the button behind the phone my app is miimiza and goes to the background which is the behavior I hope, but when I return to my app does not continue showing my master detail page, but returns to my LginPage.

It is as if my app was running twice or at least there were two instances of LoginPage existing at the same time, this is because in my LoginPage I trigger some DisplayAlert according to some messages that my page is listening through the MessaginCenter and they are they shoot twice.

Can someone tell me how I can return the same to my app on the master detail page and not restart in the strange way described?

LoginView.xaml.cs:

public partial class LogonView : ContentPage
{

    LogonViewModel contexto = new LogonViewModel();

    public LogonView ()
    {
        InitializeComponent ();
        BindingContext = contexto;


        MessagingCenter.Subscribe<LogonViewModel>(this, "ErrorCredentials", async (sender) =>
        {
            await DisplayAlert("Error", "Email or password is incorrect.", "Ok");
        }
        );


    }

    protected override void OnDisappearing()
    {
        base.OnDisappearing();

        MessagingCenter.Unsubscribe<LogonViewModel>(this, "ErrorCredentials");

    }
}

Part of my ViewModel:

if (Loged)
    {
        App.token = token;
        Application.Current.MainPage = new RootView();
    }
    else
    {   
        MessagingCenter.Send(this, "ErrorCredentials");
    }

Thanks.

回答1:

I hope this is in Android. All you can do is, you can override the backbuttonpressed method in MainActivity for not closing on back button pressed of the entry page. like below, you can add some conditions as well.

public override void OnBackPressed()
    {
        Page currentPage = Xamarin.Forms.Application.Current.MainPage.Navigation.NavigationStack.LastOrDefault();


        if (currentPage != null)
        {
            if (currentPage.GetType().Name == "HomePage" || currentPage.GetType().Name == "LoginPage")
            {
                return;
            }
        }
        base.OnBackPressed();
    }


回答2:

When you press the Home button, the application is paused and the current state is saved, and finally the application is frozen in whatever state it is. After this, when you start the app, it is resumed from the last point it was saved with.

However, when you use the Back button, you keep traversing back in the activity stack, closing one activity after another. in the end, when you close the first activity that you opened, your application exits. This is why whenever you close your application like this, it gets restarted when you open it again.

Answer taken from this answer. The original question asks about the native Android platform, but it still applies here.



回答3:

It means you have to Use Setting Plugin or save data in Application properties. You have to add below code in App.xaml.cs file:

if (SettingClass.UserName == null)
                MainPage = new LoginPage();
 else
                MainPage = new MasterDetailPage();

For Setting Plugin you can refer this link.