Xamarin.Forms: Reopening application best pratices

If you have experienced that reopening your application from the phone’s app drawer or launcher is fails because it crashes your application, or displays the first set main page again instead of displaying the latest one, keep reading this article.

If you have crashes, or malfunctions, you are probably initializing something in the constructor of the App.xaml.cs, or in the overridden method called ‘OnStart’ like this:

        public App()
        {
            InitializeComponent();
            InitalizeOnlyOnceClass.Initalize();
        }

        protected override void OnStart()
        {
            AnAnotherInitalizeOnlyOnceClass.Initalize();
        }

Imaginary InitalizeOnlyOnceClass’s Initialize method can be called only once, for the second call, it throws an exception. After you have started your program, sent to background, and bringing it back throws the second Initalization’s exception. This is because the App class gets constructed again when reopening application from the drawer.

To handle this, you should make a boolean for the application’s initialization progress.

        private static bool isInitalized;

        public App()
        {
            InitializeComponent();

            if(isInitalized == false)
            {
                InitalizeOnlyOnceClass.Initalize();
            }
        }

If you receive a blank white screen, you are probably forget to set the Main Page

Make sure, that the Application.Current.MainPage always gets a value.
If you have implemented the Initialization by your self, or an another way, make sure you have handled correctly the else statement also.
Even if the application has been initialized once, the Application.Current.MainPage have to be set always when reconstructing the App.

Continue with the last page opened in the application

If you want to continue always with the last page opened, you need to store the last page always, when you are navigating from one to an another.

You can store the last page with making a class used for navigation, like this:

    public static class SimpleNavigationLogic
    {
        private static Xamarin.Forms.Page lastNavigatedPage;

        public static void ChangeMainPage(Xamarin.Forms.Page pageToSet)
        {
            Xamarin.Forms.Device.BeginInvokeOnMainThread(() =>
            {
                Application.Current.MainPage = pageToSet;
            });
            lastNavigatedPage = pageToSet;
        }

        public static void RestoreMainPage()
        {
            Xamarin.Forms.Device.BeginInvokeOnMainThread(() =>
            {
                Application.Current.MainPage = lastNavigatedPage;
            });
        }
    }

Than you can make the initialization for your application like this:

        public App()
        {
            InitializeComponent();

            if(!isInitalized)
            {
                InitalizeOnlyOnceClass.Initalize();
                SimpleNavigationLogic.ChangeMainPage(new AwesomeMainPage());
            }
            else
            {
                SimpleNavigationLogic.RestoreMainPage();
            }
        }

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.