0

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);
    }
}
Martin Zikmund
  • 38,440
  • 7
  • 70
  • 91
waqas waqas
  • 197
  • 9
  • 22

2 Answers2

1

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);

    ...
}
Martin Zikmund
  • 38,440
  • 7
  • 70
  • 91
  • I know that this post is over a year old, I'm hoping you're still around. I 've been trying to get a splash screen to work for some time now and now I'm attempting to use your solution. When I do, I don't see my splash screen and when the app tries to load the next activity I get the error `Message=AppCompat does not support the current theme features: { windowActionBar: false, windowActionBarOverlay: false, android:windowIsFloating: false, windowActionModeOverlay: false, windowNoTitle: false }`. My app is ready to deploy, I just need to get a splash screen that will work. Frustrating. – Prescott Chartier Mar 08 '20 at 14:12
  • Hi, sorry for my late reply, I am on vacation. Before I return, could you post the styles.xml file you have in your app? Also could you try the following suggestion: https://stackoverflow.com/questions/29784124/java-lang-illegalargumentexception-appcompat-does-not-support-the-current-theme ? – Martin Zikmund Mar 13 '20 at 10:56
  • The suggestion in the link provided had no effect. Same problem. I am unable to post my entire Styles.xml in a comment, this is the splash screen style ' ` – Prescott Chartier Mar 13 '20 at 12:41
  • What are the min/target/compile versions of Android you are targeting with your app? – Martin Zikmund Mar 14 '20 at 08:45
  • API 26, API 29. Xamarin 16.5.000.472 (d16-5@63ed925), Xamarin.Android SDK 10.2.0.100 (d16-5/988c811). – Prescott Chartier Mar 14 '20 at 09:36
  • By any chance, would you be able to make a small repo on GitHub? I would test it locally – Martin Zikmund Mar 14 '20 at 10:29
0

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

yair
  • 1
  • 3