Xamarin.Forms tips : Smooth Launch on Android

INTRODUCTION

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 START 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.

EXAMPLE

MainActivity.cs

1. [Activity (Label = "Label", Icon = "@drawable/ic_launcher",Theme = "@style/SplashTheme" , MainLaunche = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
2. public class SplashActivity : FormsApplicationActivity
3. {
4. protected override void OnStart ()
5. {
6. base.OnStart ();
7.
8. StartActivity(new Intent(this,typeof(MainActivity)));
9. Finish ();
10. }
11. }

SplashTheme.xml

1. <style name="SplashTheme" parent="@android:style/Theme.Holo.NoActionBar">
2. <item name="android:windowBackground">@drawable/splash</item>
3. </style>

splash.xml

1. <?xml version="1.0" encoding="UTF-8"?>
2. <layer-list
3. xmlns:android="http://schemas.android.com/apk/res/android">
4. <item>
5. <shape android:shape="rectangle">
6. <solid android:color="#00a6cf" />
7. </shape>
8. </item>
9. <item>
10. <bitmap android:src="@drawable/splashicon"
11. android:gravity="center" />
12. </item>
13. </layer-list>

By Yannick Gerard, Android Developer

Xamarin.Android tips

This article will expose some performance issues you might encounter when developing an Android app using Xamarin. Typically you will encounter them if you develop your app without taking the way Xamarin works on Android into account.

Read more

Continuous Build, Integration, Deployment and Delivery

The terms “Continuous Integration”, “Fail Fast” and “Agile” are more and more becoming part of today’s developers discussions. Old development practices such as Waterfall are no longer valid to fullfill the needs of flexibility, modularity and performance of current and future projects.

Read more