Setting MainPage = NavigationPage during OnStart in Xamarin.Forms display a blank screen in Android

fraxedasfraxedas Member ✭✭
edited May 2018 in Xamarin.Forms

I have a Xamarin Forms Application where I'm setting the MainPage to NavigationPage during OnStart.
The NavigationPage is static (kept in memory for reuse).
When I press the back button in Android and go back to the app the OnStart method is executed again and the application displays a blank screen.
The repro: OnStart Static NavigationPage

public partial class App : Application
{
    private static readonly NavigationPage NavigationPage = new NavigationPage(new MainPage());
    public App ()
    {
        InitializeComponent();
    }

    protected override void OnStart()
    {
        MainPage = NavigationPage;
    }
}

If I do the same action in the constructor the application behaves as expected: there's no blank screen and the last page visited is displayed.
The repro: Constructor Static NavigationPage

public partial class App : Application
{
    private static readonly NavigationPage NavigationPage = new NavigationPage(new MainPage());
    public App ()
    {
        InitializeComponent();

        MainPage = NavigationPage;
    }
}

What is the difference between setting the MainPage in the constructor and in the OnStart method?

Best Answer

Answers

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭
    edited May 2018

    @fraxedas said:

    What is the difference between setting the navigation in the constructor and in the OnStart method?

    You already answered your own question.

    When I press the back button in Android and go back to the app the OnStart method is executed again

    But a constructor only runs when the object needs to be constructed. If it already exists it doesn't execute.

  • fraxedasfraxedas Member ✭✭
    edited May 2018

    If I create the NavigationPage and assign it to the MainPage during OnStart each time I don't get a blank screen, but I lose the state when the back button is pressed and the app is relaunched.

    public partial class App : Application
    {
        public App ()
        {
            InitializeComponent();
        }
    
        protected override void OnStart()
        {
            MainPage = new NavigationPage(new MainPage());
        }
    }
    

    But a constructor only runs when the object needs to be constructed. If it already exists it doesn't execute.

    You are right, but the constructor for the application is called from the MainActivity.
    Every time I press the back button and reopen the app the MainActivity.OnCreate is called.

    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        protected override void OnCreate(Bundle bundle)
        {
            ...
            LoadApplication(new App());
        }
    }
    
  • fraxedasfraxedas Member ✭✭
    edited May 2018

    If the NavigationPage is static:

    • it seems to fail if it is set in the OnStart method
    • it works fine if it is set in the constructor

    I need to understand why this is happening.
    I added a couple of link showing a screen capture of both cases.

  • fraxedasfraxedas Member ✭✭
    edited May 2018

    @ClintStLaurent thanks for the help. I'll edit the question to better explain my issue.

Sign In or Register to comment.