Search code examples
androidxamarinxamarin.formsxamarin.androidsplash-screen

Between loading my Mainpage and Splash Screen, there is white screen occurring on Xamarin.Android


I have a Xamarin.Forms app that has splash screen for ios and android. It works on ios perfectly. But I have a problem on android. The problem is: splash screen launches, after that between loading my Mainpage and splash screen, there is white screen occurring.

I did a sample project for this problem.

I did this steps for splash screen:

I added splash_screen.xml to Android drawable folder

<?xml version="1.0" encoding="utf-8" ?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <item>
    <shape android:shape="rectangle">
      <solid android:color="@color/colorPrimary" />
      <padding android:left="25dp"
               android:right="25dp"
               android:top="0dp"
               android:bottom="0dp" />
      <!-- Android -> Resources-> values -> colors.xml içerisinde tanımlı bu renk -->
    </shape>
  </item>
  <item>
    <bitmap
        android:src="@mipmap/icon"
        android:tileMode="disabled"
        android:gravity="center" />
  </item>
</layer-list>

I added splashscreen style to styles.xml

<style name="splashscreen" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:windowBackground">@drawable/splash_screen</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowActionBar">true</item>
  </style>

I set MainLauncher value as false on MainActivity.

[Activity(Label = "SplahScreenSample", 
        Icon = "@mipmap/icon",
        LaunchMode = LaunchMode.SingleInstance,
        Theme = "@style/MainTheme",
        MainLauncher = false,
        ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
  //...
}

I added SplashActivity to Android project: (It's main launcher value is true)

[Activity(Label = "SplahScreenSample", Icon = "@mipmap/icon", Theme = "@style/splashscreen", MainLauncher = true, NoHistory = true)]
    public class SplashActivity : AppCompatActivity
    {
        public override void OnCreate(Bundle savedInstanceState, PersistableBundle persistentState)
        {
            base.OnCreate(savedInstanceState, persistentState);
        }

        protected override void OnResume()
        {
            base.OnResume();
            StartActivity(new Intent(Application.Context, typeof(MainActivity)));
        }
    }

Where is my problem? Thank you in advance.

NOTE: Actually I understand problem. Problem is: MainTheme has not splash screen but MainActivity Theme is MainTheme. But I don't know how can I fix this. I tried to add splash screen to MainTheme or I tried to set MainActivity Theme is splashscreen. But these didn't work.


Solution

  • Best approach works in this link: https://xamarinhelp.com/creating-splash-screen-xamarin-forms/

    So I set MainActivity Theme as splashscreen (Theme = "@style/splashscreen"). And I changed theme in MainActivity OnCreate method in this way:

    protected override void OnCreate(Bundle bundle)
    {
        base.Window.RequestFeature(WindowFeatures.ActionBar);
        // Name of the MainActivity theme you had there before.
        // Or you can use global::Android.Resource.Style.ThemeHoloLight
        base.SetTheme(Resource.Style.MainTheme);
    
        base.OnCreate(bundle);
    
        ...
     }
    

    This worked.