Xamarin Forms tips: Smooth Launch on Android

Xamarin Forms tips: Smooth Launch on Android


Xamarin Forms is an interesting solution, it abstracts the UI platform specific code in order to maximize code reuse, but by doing so it also introduces some performance constraints that can become potential issues if you are not aware of them.

App strat on Android

In order for Xamarin Forms to start, one has to call the Xamarin.Forms.Forms.Init method in the launch activity, this initialization can take a long time to complete and because of the UI operations it performs, it must be executed on the main thread.

This means that your app’s UI thread will be blocked during this initialization.

To smooth the launch, a Splashscreen can be used. On iOS, the system already enforces the use of a Splashscreen so there is nothing special to do but it is not the case on Android. Google recommends to avoid Splashscreens in favor of instantaneous app startup, but unfortunately we don’t really have a choice with Xamarin.Forms, so the solution will be to define a Splash screen in our app.

To create a Splashscreen, you should create a SplashActivity which will be defined as the launch Activity. This activity will in turn launch the MainActivity which initializes Forms and then, to avoid returning on the Splash, will finish itself.

To create your Splashscreen you can either define a custom layout used as the content view or use a specific theme which defines your splash drawable as the window background. As explained by Cyril Mottier, it is always preferable to rely on the theme to define the app’s background instead of the layout. And the drawables should be flexible enough for you to create a responsive splash image ( scalable background with some text/image drawn over it ). And to avoid overdraw, your MainActivity’s theme should also define its background as the window background.

As you can expect, you will not be able to use animations in your Splashscreen because the UI thread is blocked during the Forms initialization.

This mandatory Splashscreen could be a good opportunity to prefetch some data in a background thread, but only because the Splash is really necessary. If you do this I would recommend to avoid blocking on the splash if your loading is not finished yet, but it will require some synchronization.


← Back to the hub