3
votes

I'm creating an app that shows a splash screen and then creates the main activity. I'm following this tutorial that seems pretty straight forward: https://developer.xamarin.com/guides/android/user_interface/creating_a_splash_screen/

After implementing that I can successfully see the splash but there are some times (1 out of 20) that with an S5 I see the following screen:

Wrong screen

Followed by the (right) splash (taken from the Emulator but just to make my point):

enter image description here

So my guess would be that sometimes Xamarin is taking to long to load the app therefore it has a delay showing the splash. Is there any way to prevent that?

UPDATE 1 I've followed the tutorial but I've removed the sleep for this:

Insights.Initialize ("<APP_KEY>", Application.Context);
StartActivity(typeof (MainActivity));
2
Did you set the correct image in the splash theme and set the theme for the activity, as in the tutorial? It looks like your activity doesn't have a windowBackground image. Have you checked on other versions of Android?dylansturg
Yes I've set everything like the tutorial and 19 in 20 it looks good is just 1 out of 20 that shows the first screen for less than a second and then shows the second one with the right layout.Federico M. Rinaldi
@FedericoM.Rinaldi ,i think you facing OOM IN bitmap handling.Goto Output window ,see log ,anyway samusing devices going to hell in few years ,i was face the same issue in myJagadeesh Govindaraj
@matthewrdev thanks for the reply but I'm not using the Sleep, I've updated my questionFederico M. Rinaldi

2 Answers

5
votes

The example invokes the Thread.Sleep(10000); on the UI thread... This will lock up the app and generate an ANR!

Fix it by backgrounding the sleep and then triggering the next activity:

namespace SplashScreen
{
    using System.Threading;

    using Android.App;
    using Android.OS;

    [Activity(Theme = "@style/Theme.Splash", MainLauncher = true, NoHistory = true)]
    public class SplashActivity : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            Task.Run (() => {
                Thread.Sleep (10000); // Simulate a long loading process on app startup.
                RunOnUiThread (() => { 
                    StartActivity (typeof(Activity1));
                });
            });
        }
    }
}
1
votes

Even if this post is rather old, I had a similar experience when implementing the SplashScreen and could resolve this by updating the style/theme of the SplashScreen. The screen @frederico m rinaldi sometimes sees is commonly created by using Android's default (Holo) theme.

Although you didn't supply the style applied to the SplashScreen (see Theme = @style/Theme.Splash of the accepted answer) , here's mine. Maybe you can check if they differ.

<style name="Theme.Splash" parent ="Theme.AppCompat.Light.NoActionBar">
  <!-- Use a fully opaque color as background. -->
  <item name="android:windowBackground">@android:color/black</item>
  <!-- This removes the title bar seen within the first screen. -->
  <item name="windowNoTitle">true</item>
  <!-- Let the splash screen use the entire screen space by enabling full screen mode -->
  <item name="android:windowFullscreen">true</item>
  <!-- Hide the ActionBar (Might be already defined within the parent theme) -->
  <item name="windowActionBar">false</item>
</style>

You may notice that I simply use the color black as background, because my SplashScreen uses a custom layout file instead of static image (this.SetContentView(Resource.Layout.SplashScreen);). Also, loading the image (drawable) might require some time which may be the main reason you can see the default theme instead of your splash screen. Additionally, I omitted the android: XML namespace for some attributes which is due to Google's internal implementation of the Android support library features.

Please note that in order to use the AppCompat themes, your application must include the AppCompat support library and your Activity must subclass Android.Support.V7.App.AppCompatActivity.