András Tóth‘s professional blog
banditoth.net

Hey there 👋, I’m banditoth a .NET MAUI developer from Hungary.
I write about software development with .NET technologies.

You can find me on:
LinkedIn | Github | StackOverflow | X / Twitter | Threads

Xamarin Android: SplashScreenActivity létrehozása

This content has 5 years. Some of the information in this post may be out of date or no longer work. Please, read this page keeping its age in your mind.

Ahhoz, hogy legyen egy betöltő Activitynk, ami nem fehér képernyőt jelnít meg akkor amikor az applikáció először töltődik be, ahhozlétre kell hozni egy SplasActivity.cs-t az alábbiak alapján:

	[Activity(Label = "APPLICATIONNAME", Icon = "@drawable/android_app_icon", Theme = "@style/MainTheme.Splash", MainLauncher = true, NoHistory = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation, 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 mainIntent = new Intent(Application.Context, typeof(MainActivity));

			if (Intent.Extras != null)
			{
				mainIntent.PutExtras(Intent.Extras);
			}
			mainIntent.SetFlags(ActivityFlags.SingleTop);
			StartActivity(mainIntent);
		}
	}

Az OnResume metódusban át kell adni az intenteket a MainActivitynk számára, hogy pl a Push üzenetek feldolgozásra kerüljenek. Ezekről itt ovlashatsz: https://www.banditoth.hu/2020/01/16/xamarin-android-splashscreenactivity-es-pushnotification-megnyitas-kilott-alkalmazasnal/

A MainActivity Activity attribútumát a következőképpen kell módosítani:

[Activity(Theme = "@style/MainTheme", MainLauncher = false, Exported = true,  ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation, ScreenOrientation = ScreenOrientation.Portrait,
        LaunchMode = LaunchMode.SingleTop)]

Ahogy láthatjuk, a SplashActivitynk az alábbi témát fogja használni: “Theme = “@style/MainTheme.Splash””, ezért létre kell hozni a styles.xml-ben egy új style-t az alábbi alapján:

<style name="MainTheme.Splash" parent ="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowBackground">@drawable/splash_screen</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowActionBar">true</item>
  </style>

Ez a téma háttérként a “@drawable/splash_screen” resource-t fogja használni, ezért ezt is létre kell hozni a drawable mappa alá az alábbi mintára:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <item>
    <color android:color="@color/splash_background"/>
  </item>
  <item>
    <bitmap
        android:src="@drawable/icon"
        android:tileMode="disabled"
        android:gravity="center"/>
  </item>
</layer-list>

Az itt használt @color és @drawable iconok változhatnak, csak a bemutatás célját szolgálják.

https://docs.microsoft.com/en-us/xamarin/android/user-interface/splash-screen

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.