Xamarin Forms Android Splash Screen and Start Application from Push Notification

When developing mobile applications with Xamarin forms you will find default splash screens for IOS and Windows Phone projects but not for android. Android itself does not like splash screens because googles recommendation is start the app without wasting user’s time. But since android application take some time to load it is good to show something attractive to user while the app is loading. Google also use splash screens in their new updates. So when you use a splash screen try to use something easy to render fast.

Xamarin forms android only has a single activity. Therefore to showing a splash screens correctly we need to add a new activity. No need to have a layout for this new activity and it should mark as “Main Launcher”. In this activity we only do is start our main activity. We need to create a small theme Style which contain splash screen drawable and set up as the theme for this activity.

1). Create new Activity called “SplashScreenActivity”

2). Create a new Theme Style in “Resources>values>styles.xml” file

3). Responsive Splash Screen Drawable

I have added small image called “splash_icon” to center of the screen. Notice that I have mark Splash activity as NoHistory = true. Setting that attribute is prevent adding the splash activity to your navigation stack so “Main Activity” remains the bottom of the stack.

Now the application has a Splash Screen and no difference in app navigation.

So what happen when the application is open from a push notification? First we need to open the application when user click the push notification. It can done by passing “Intent” to the push notification creation.

4). When we create the push notification pass intent of type “MainActivity” and I have also pass an extra value to the intent

Using Notification Manger you can create the notification with this pending intent.

Now our application can start by clicking on the push notification and “Main Activity” will receive the extra value I passed. But if user click on the push notification when the app is already running? It will create another instance of MainActivity and our navigation will not work as we need. So to overcome that we need to make sure our MainActivity only create single instance. We can do that by adding an attribute to the main activity.

5). Mark the MainActivity as SingleTask

Note : in here LaunchMode.SingleInstance will not work. If you use SingleInstance you will see an empty screen when start the application because it permit no other activities to be part of that task. So you will not see the splash activity if the application is already started once. more info about android activity launch modes

But then we have another problem. How we pass extra value when our MainActivity only create one time. In android activity there is a way to do that. We need to override the “OnNewIntent” method.

6). Override OnNewIntent Method

Now our application has a splash screen and can start from a push notification. Most importantly our navigation is remain the same. Now if you want you can start different pages in your application according to the values pass from push notifications.



Leave a Reply

Your email address will not be published. Required fields are marked *