You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Xamarin.Android启动页与主页间出现白屏问题求助

Fixing Android White Screen After Splash Screen in Xamarin.Forms

Hi there, let's figure out why you're seeing that annoying white screen after the splash screen on your Android app. The root issue here is the window background of your MainActivity showing through while Xamarin.Forms initializes and loads your MainPage. Here's a breakdown of what's happening and how to fix it:

What's Causing the White Screen?

When your SplashActivity launches MainActivity, the MainActivity uses the MainTheme by default. While Xamarin.Forms spins up and renders your MainPage, the MainActivity's window displays whatever background is defined in MainTheme—which is likely white if you haven't customized it. This creates the jarring white flash between the splash screen and your app's first page.

Your previous attempts (adding the splash screen to MainTheme or setting MainActivity's theme to splashscreen) might not have worked because either:

  • You didn't switch back to the normal theme after Forms loaded, causing styling issues later
  • The theme transition wasn't handled properly during the initialization phase

Step-by-Step Fix

1. Update Your MainTheme to Use the Splash Background Temporarily

First, modify your styles.xml to make MainTheme use your splash screen background initially. This ensures the splash visual stays visible until your MainPage is fully ready:

<style name="MainTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Use the splash screen background to avoid white flash -->
    <item name="android:windowBackground">@drawable/splash_screen</item>
    <!-- Keep your existing theme properties -->
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="android:windowFullscreen">true</item>
</style>

2. Switch Back to Normal Background in MainActivity

Once Xamarin.Forms has finished loading your app, you can reset the window background to your app's normal background (or transparent, if your MainPage handles the background styling). Update your MainActivity code:

[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
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        // Initialize Xamarin.Forms and load your app
        global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
        LoadApplication(new App());

        // After loading, reset the window background to your app's default
        // Replace with your app's specific background color or drawable if needed
        Window.SetBackgroundDrawableResource(Android.Resource.Color.Transparent);
    }
}

3. Optimize Your SplashActivity (Optional but Clean)

You can simplify your SplashActivity to ensure smoother navigation—there's no need for an empty OnCreate with PersistableBundle:

[Activity(Label = "SplahScreenSample", Icon = "@mipmap/icon", Theme = "@style/splashscreen", MainLauncher = true, NoHistory = true)]
public class SplashActivity : AppCompatActivity
{
    protected override void OnResume()
    {
        base.OnResume();
        // Launch MainActivity as soon as the splash screen is ready
        StartActivity(new Intent(this, typeof(MainActivity)));
    }
}

Why This Works

By setting MainTheme's window background to your splash screen, you maintain the splash visual while Xamarin.Forms initializes. Once your MainPage is loaded, switching the window background ensures your app's normal styling takes over without any white flashes.


内容的提问来源于stack exchange,提问作者Pelin Konaray

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 15:12:32