简体   繁体   English

Xamarin Android-启动屏幕无法在恢复中正常工作

[英]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 我关注了这篇文章,并将信息与其他文章拼凑起来以创建启动屏幕: https : //docs.microsoft.com/zh-cn/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)... 如果您从另一个应用程序内部启动应用程序,或者使用ClearTask标志启动此应用程序,或者如果您的应用程序正在执行冷启动(已在后台关闭),也许还有其他方式,您将看到“预览”屏幕,它是当前主题的背景(类似于您为SplashScreen所做的工作,它显示了主题背景)...

But if your "@style/MainTheme" has a simple white background, this will be what you might see when reentering your app. 但是,如果您的“ @ style / MainTheme”具有简单的白色背景,这将是您重新进入应用程序时看到的内容。

Therefore you can consider using the "SetTheme" method in OnCreate. 因此,您可以考虑在OnCreate中使用“ SetTheme”方法。 There is more about this in this link: 在此链接中有关于此的更多信息:

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

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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