Splash screen dispose/remove after loaded

Viewed 2070

I am launching a splash screen in my xamarin android application and what happens is the splash screen keeps appearing on other pages as background.

Whatever I do it's there.

I have tried to "Finish" the activity,NoHistory=true but nothing ,keeps showing on other pages in the background.

Taken from https://alexdunn.org/2017/02/07/creating-a-splash-page-for-xamarin-forms-android/

Any ideas why?

      [Activity(Label = "MyApp",
    Theme = "@style/MyTheme.Splash",
    Icon = "@drawable/icon", 
    MainLauncher = true, 
    NoHistory = true,
    ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        protected override void OnCreate(Bundle bundle)
        {
            TabLayoutResource = Resource.Layout.tabs;
            ToolbarResource = Resource.Layout.toolbar;

            base.OnCreate(bundle);

            Xamarin.Forms.Forms.Init(this, bundle);
            LoadApplication(new App(new AndroidInitializer()));      

        }
    }


     [Activity(
            Theme = "@style/MyTheme.Splash",
            MainLauncher = true,
            NoHistory = true,
            ScreenOrientation = ScreenOrientation.Portrait)]
        public class SplashActivity : AppCompatActivity
        {

            public override void OnCreate(Bundle savedInstanceState, PersistableBundle persistentState)
            {
                base.OnCreate(savedInstanceState, persistentState);

            }


          protected override void OnResume()
         {
           base.OnResume();
             var startUp = new Task(() =>
            {
             var intent = new Intent(this, typeof(MainActivity));
            StartActivity(intent);
        });
    startUp.ContinueWith(t => Finish());

    startUp.Start();
    }

      <style name="MyTheme.Splash" parent ="Theme.AppCompat.Light">
        <item name="android:windowBackground">@drawable/splash_screen</item>
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
      </style>
3 Answers

This is caused by Theme = "@style/MyTheme.Splash". Both activities use the same theme.

Create a different theme for the other activities.

<style name="MyTheme.Main" parent ="Theme.AppCompat.Light">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

Change the theme in Activity:

[Activity(
    Theme = "@style/MyTheme.Main",
    MainLauncher = true,
    NoHistory = true,
    ScreenOrientation = ScreenOrientation.Portrait)]
public class SplashActivity : AppCompatActivity
{

    public override void OnCreate(Bundle savedInstanceState, PersistableBundle persistentState)
    {
        base.OnCreate(savedInstanceState, persistentState);

    }
}

Another solution instead of creating a new theme is to set a background color in the layout file for the parent view in every activity that isn't the SplashScreen activity.

Your problem is mainly caused by two activities using the same theme.

In the "MainActivity" change the theme from "@style/MyTheme.Splash" to "@style/MainTheme", this will make the main activity use the default theme.

Related