Xamarin Forms: App is always restarting when resuming from background

Sreejith Sreenivasan 1,001 Reputation points
2023-06-13T07:02:18.7033333+00:00

The datepicker UI is breaking and it is showing on top of the splash screen background like below:

TOR6W

Code:

<DatePicker

	x:Name="startdate_datepicker"

	Grid.Column="1"

	Format="MM/dd/yyyy"

	PropertyChanged="StartDateChoosed"

	HorizontalOptions="StartAndExpand"

	VerticalOptions="Center"/>

My Style:

<style name="MainTheme" parent="MainTheme.Base">

	<item name="android:forceDarkAllowed">false</item>

	<item name="android:windowBackground">@drawable/splashscreen</item>

	<item name="android:textAllCaps">false</item>

	<item name="colorPrimary">#e6e6e6</item>

	<item name="colorPrimaryDark">#000000</item>

	<item name="colorAccent">#808080</item>

I was using a single style for the entire app and that is the reason for this issue. For solving this issue I made some updates on Style and MainActivity.

Created a new style for splash:

<style name="MyTheme.Splash" parent="Theme.AppCompat.Light">

	<item name="android:forceDarkAllowed">false</item>

	<item name="android:windowBackground">@drawable/splashscreen</item>

	<item name="android:textAllCaps">false</item>

	<item name="colorPrimary">#e6e6e6</item>

	<item name="colorPrimaryDark">#000000</item>

	<item name="colorAccent">#808080</item>

</style>

<style name="MyTheme.Main" parent ="MainTheme.Base">

	<item name="windowActionBar">false</item>

	<item name="windowNoTitle">true</item>

	<item name="android:forceDarkAllowed">false</item>

	<item name="android:textAllCaps">false</item>

	<item name="colorPrimary">#e6e6e6</item>

	<item name="colorPrimaryDark">#000000</item>

	<item name="colorAccent">#808080</item>

</style>

MainActivity:

[Activity(

	Label = "App Name", 

	Theme = "@style/MyTheme.Main", 

	Icon = "@drawable/launcher_icon", 

	MainLauncher = false,

	NoHistory = true,

	Exported =true,

	ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation, 

	ScreenOrientation = ScreenOrientation.Portrait)]

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity

{

	protected override void OnCreate(Bundle savedInstanceState)

	{

		base.OnCreate(savedInstanceState, persistentState);

	}

}

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

	}

}

But now my app is always restarting when resuming from background due to the OnResume() code in SplashActivity. I need to stay on the same page if I resume from background. For that What change I need to do on SplashActivity?

Developer technologies .NET Xamarin
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 50,126 Reputation points Microsoft External Staff
    2023-06-19T05:47:31.3666667+00:00

    Hello,

    According to the community's security rules, demos in external links cannot be downloaded.

    According to the code you provided, this issue is caused by recreating the MainActivity when calling the resume method when restoring the app from the background.

    In fact, when creating SplashScreen, you can achieve this requirement with just one file from MainActivity.

    Please refer to the following code sample.

    [Activity(Label = "Mobile App",
            Theme = "@style/Your_Splash_Theme",
            Icon = "@drawable/Your_Icon",
            MainLauncher = true,
            ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation,
            LaunchMode = LaunchMode.SingleTop)]
    
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
        {
            protected override void OnCreate(Bundle savedInstanceState)
            {
                base.Window.RequestFeature(WindowFeatures.ActionBar);
                // Name of the MainActivity theme you had there before.
                base.SetTheme(Resource.Style.MainTheme);
                base.OnCreate(savedInstanceState);
    
                Xamarin.Essentials.Platform.Init(this, savedInstanceState);
                global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
                LoadApplication(new App());
            }
    
          // other methods
    }
    

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.