简体   繁体   中英

Xamarin Android - Splash Screen Doesn't Work On Resume

I followed this article and pieced together information with other articles to create a splash screen: https://docs.microsoft.com/en-us/xamarin/android/user-interface/splash-screen

The splash screen works well when I start the app up by tapping on the app's icon. However, if the app is already running and I switch to it, the screen goes white for a few seconds while the app resumes. Why?

Here is my code:

    [Activity(Label = "Hardfolio", Icon = "@drawable/icon", Theme = "@style/MyTheme.Splash", MainLauncher = true, NoHistory = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class SplashActivity : FormsAppCompatActivity
    {
        static readonly string TAG = "Hardfolio: " + typeof(SplashActivity).Name;

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

        // Launches the startup task 
        protected override void OnResume()
        {
            base.OnResume();
            var startupWork = new Task(AppStartup);
            startupWork.Start();
        }

        // Simulates background work that happens behind the splash screen 
        async void AppStartup()
        {
            StartActivity(new Intent(Application.Context, typeof(MainActivity)));
        }
    }




    [Activity(Label = "Hardfolio", Icon = "@drawable/icon", Theme = "@style/MainTheme", MainLauncher = false, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    [IntentFilter(new[] { UsbManager.ActionUsbDeviceAttached })]
    [MetaData(UsbManager.ActionUsbDeviceAttached, Resource = "@xml/device_filter")]
    public class MainActivity : Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        #region Fields
        private AndroidHidDevice _TrezorHidDevice;
        private UsbDeviceAttachedReceiver _UsbDeviceAttachedReceiver;
        private UsbDeviceDetachedReceiver _UsbDeviceDetachedReceiver;
        private object _ReceiverLock = new object();
        #endregion

        #region Overrides
        protected override void OnCreate(Bundle bundle)
        {
            try
            {
                _TrezorHidDevice = new AndroidHidDevice(GetSystemService(UsbService) as UsbManager, ApplicationContext, 3000, 64, TrezorManager.TrezorVendorId, TrezorManager.TrezorProductId);

                ServicePointManager.ServerCertificateValidationCallback += (o, certificate, chain, errors) => true;

                TabLayoutResource = Resource.Layout.Tabbar;
                ToolbarResource = Resource.Layout.Toolbar;

                base.OnCreate(bundle);

                Xamarin.Forms.Forms.Init(this, bundle);

                RegisterReceiver();

                var application = new App(new CrossPlatformUtilities(new IsolatedStoragePersister(), new AndroidRESTClientFactory()), _TrezorHidDevice, GetPin);

                LoadApplication(application);
            }
            catch (Exception ex)
            {
                Logger.Log("Android crash", ex, nameof(Wallet.Droid));
                Toast.MakeText(ApplicationContext, ex.ToString(), ToastLength.Long).Show();
            }
        }

        private async Task<string> GetPin()
        {
            var taskCompletionSource = new TaskCompletionSource<string>();

            RunOnUiThread(async () =>
            {
                var pin = await TrezorPinPad.GetPin();
                taskCompletionSource.SetResult(pin);
            });

            return await taskCompletionSource.Task;
        }

        protected override void OnResume()
        {
            base.OnResume();

            Logger.Log($"Resuming... Setting up Trezor listeners. _TrezorHidDevice is {(_TrezorHidDevice == null ? "null" : "not null")}", null, nameof(Wallet.Droid));

            RegisterReceiver();
        }

        private void RegisterReceiver()
        {
            try
            {
                lock (_ReceiverLock)
                {
                    if (_UsbDeviceAttachedReceiver != null)
                    {
                        UnregisterReceiver(_UsbDeviceAttachedReceiver);
                        _UsbDeviceAttachedReceiver.Dispose();
                    }

                    _UsbDeviceAttachedReceiver = new UsbDeviceAttachedReceiver(_TrezorHidDevice);
                    RegisterReceiver(_UsbDeviceAttachedReceiver, new IntentFilter(UsbManager.ActionUsbDeviceAttached));

                    if (_UsbDeviceDetachedReceiver != null)
                    {
                        UnregisterReceiver(_UsbDeviceDetachedReceiver);
                        _UsbDeviceDetachedReceiver.Dispose();
                    }

                    _UsbDeviceDetachedReceiver = new UsbDeviceDetachedReceiver(_TrezorHidDevice);
                    RegisterReceiver(_UsbDeviceDetachedReceiver, new IntentFilter(UsbManager.ActionUsbDeviceDetached));
                }
            }
            catch (Exception ex)
            {
                Logger.Log($"Error registering Hid receivers", ex, nameof(Wallet.Droid));
            }
        }
        #endregion
    }

If you start your application, from within another application/or using the ClearTask flag/or if your app is performing a cold start (has been closed in the background), and perhaps i other ways as well, you will see a "Preview" screen, which is the background of your current theme (kind of what you are already doing for your SplashScreen, which shows the theme background)...

But if your "@style/MainTheme" has a simple white background, this will be what you might see when reentering your app.

Therefore you can consider using the "SetTheme" method in OnCreate. There is more about this in this link:

https://developer.android.com/topic/performance/vitals/launch-time

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM