Xamarin Forms: White screen between page push and pop solved

If you are experiencing white screen when pushing to Navigation or Modal stack on Android, read on.

I’m not sure is this a bug in Xamarin Forms or not, but I guess this is, because it comes only in certain scenarios, and not always.

What is happening, how do you notice this error?

You have got a NavigationPage. You are pushing a new page to the navigationstack, and the page is not getting rendered, only a blank white screen displays.

If you are rotating the device, the page is getting rendered well.

My environment was:
Xamarin.Forms: 4.8 up to 5.0
Device: Samsung Galaxy A12
Visual Studio 2019 Professional with Xamarin.Android SDK 11.4.0.5

Solution

Always invoke the INavigation’s methods on the applications Main Thread. UI changes must go always on the UI thread of the application.

Create a class which wraps the INavigation presented by your Views. It’s handy to store a reference in this class to the Applications Current MainPage’s INavigation instance, so try to build your code to supply the actual INavigation Instance every time to this class when the application’s mainpage is set.

	public class NavigationDispatcher : INavigation
	{
		private INavigation _navigation;

		public IReadOnlyList<Page> ModalStack => _navigation?.ModalStack;

		public IReadOnlyList<Page> NavigationStack => _navigation?.NavigationStack;

		private void SetNavigation(INavigation navigation)
		{
			_navigation = navigation;
		}

		public void InsertPageBefore(Page page, Page before)
		{
			_ = Xamarin.Essentials.MainThread.InvokeOnMainThreadAsync(() =>
			  {
				  _navigation.InsertPageBefore(page, before);
			  });
		}

		public Task<Page> PopAsync()
		{
			return Xamarin.Essentials.MainThread.InvokeOnMainThreadAsync(async () =>
			{
				return await _navigation.PopAsync();
			});
		}

		public Task<Page> PopAsync(bool animated)
		{
			return Xamarin.Essentials.MainThread.InvokeOnMainThreadAsync(async () =>
			{
				return await _navigation.PopAsync(animated);
			});
		}

		public Task<Page> PopModalAsync()
		{
			return Xamarin.Essentials.MainThread.InvokeOnMainThreadAsync(async () =>
			{
				return await _navigation.PopModalAsync();
			});
		}

		public Task<Page> PopModalAsync(bool animated)
		{
			return Xamarin.Essentials.MainThread.InvokeOnMainThreadAsync(async () =>
			{
				return await _navigation.PopModalAsync(animated);
			});
		}

		public Task PopToRootAsync()
		{
			return Xamarin.Essentials.MainThread.InvokeOnMainThreadAsync(async () =>
			{
				await _navigation.PopToRootAsync();
			});
		}

		public Task PopToRootAsync(bool animated)
		{
			return Xamarin.Essentials.MainThread.InvokeOnMainThreadAsync(async () =>
			{
				await _navigation.PopToRootAsync(animated);
			});
		}

		public Task PushAsync(Page page)
		{
			return Xamarin.Essentials.MainThread.InvokeOnMainThreadAsync(async () =>
			{
				await _navigation.PushAsync(page);
			});
		}

		public Task PushAsync(Page page, bool animated)
		{
			return Xamarin.Essentials.MainThread.InvokeOnMainThreadAsync(async () =>
			{
				await _navigation.PushAsync(page, animated);
			});
		}

		public Task PushModalAsync(Page page)
		{
			return Xamarin.Essentials.MainThread.InvokeOnMainThreadAsync(async () =>
			{
				await _navigation.PushModalAsync(page);
			});
		}

		public Task PushModalAsync(Page page, bool animated)
		{
			return Xamarin.Essentials.MainThread.InvokeOnMainThreadAsync(async () =>
			{
				await _navigation.PushModalAsync(page, animated);
			});
		}

		public void RemovePage(Page page)
		{
			_ = Xamarin.Essentials.MainThread.InvokeOnMainThreadAsync(() =>
			  {
				  _navigation.RemovePage(page);
			  });
		}
	}

Remarks

Consider a check for the current thread in the methods body.
If they are being executed in the main thread, you won’t need to switch to the main again
.

Bug is reported on Github: https://github.com/xamarin/Xamarin.Forms/issues/11993

This content has 2 years. Some of the information in this post may be out of date or no longer work. Please, read this page keeping its age in your mind.

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();
            }
        }
This content has 3 years. Some of the information in this post may be out of date or no longer work. Please, read this page keeping its age in your mind.