waqas waqas
waqas waqas

Reputation: 237

Xamarin.Forms: App crash after splash activity

I have splash screen, When I run my app its crash. Here is my splash screen code

If I don't use splash screen app is working fine but when I use splash screen and close my app and run again its crash after splash screen.

public class SplashActivity : Activity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
      var mainIntent = new Intent(Application.Context, typeof(MainActivity));
        if (Intent.Extras != null)
        {
            mainIntent.PutExtras(Intent.Extras);
        }
        mainIntent.SetFlags(ActivityFlags.SingleTop);

        StartActivity(mainIntent);
    }
}

Upvotes: 0

Views: 1307

Answers (2)

yair
yair

Reputation: 1

Happened to me on .net Maui, It turned out I didn't register the services correctly, so the builder could not generate the page.

Upvotes: 0

Martin Zikmund
Martin Zikmund

Reputation: 39072

I presume the issues is connected with the fact that you are trying to start the already started SingleTop activity for the second time.

However, the recommendation is to write a splash screen a bit differently - without needing separate activity. See this nice blog post by Adam Pedley on splash screen implementation in Xamarin.Forms.

Instead of having a separate activity, you can just temporarily apply a "splash" theme to the main activity, before the activity loads. This is useful, because it makes your app load faster than having a full separate splash screen activity.

Create a style in Resources/values/styles.xml:

<?xml version="1.0" encoding="utf-8" ?>
<resources>
  <style name="splashscreen" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowBackground">@drawable/splashscreen</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsTranslucent">false</item>
    <item name="android:windowIsFloating">false</item>
    <item name="android:backgroundDimEnabled">true</item>
  </style>
<resources>

Set this theme to the MainActivity via attribute:

[Activity(Label = "Mobile App", 
          Theme = "@style/splashscreen", 
          Icon = "@drawable/icon", 
          MainLauncher = true, 
          ConfigurationChanges = ConfigChanges.ScreenSize | 
                                 ConfigChanges.Orientation, 
          LaunchMode = LaunchMode.SingleTop)]
 public class MainActivity : Xamarin.Forms.Platform.Android.FormsAppCompatActivity

And then override the OnCreate method to set the actual theme back:

protected override void OnCreate(Bundle bundle)
{
    base.Window.RequestFeature(WindowFeatures.ActionBar);

    // For global use "global::" prefix - global::Android.Resource.Style.ThemeHoloLight
    base.SetTheme(Resource.Style.MainTheme);

    base.OnCreate(bundle);

    ...
}

Upvotes: 1

Related Questions