Xamarin Android - 启动画面不能继续使用简历

时间:2018-06-11 01:32:15

标签: java c# android xamarin.android splash-screen

我按照这篇文章将信息与其他文章拼凑在一起以创建一个启动画面: https://docs.microsoft.com/en-us/xamarin/android/user-interface/splash-screen

当我通过点按应用程序图标启动应用程序时,启动画面效果很好。但是,如果应用程序已在运行且我切换到该应用程序,则应用程序恢复时屏幕将变为白色几秒钟。为什么呢?

这是我的代码:

    [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
    }

1 个答案:

答案 0 :(得分:1)

如果您从另一个应用程序/或使用ClearTask标志启动您的应用程序/或者如果您的应用程序执行冷启动(已在后台关闭),也许还有其他方式,您将看到&#34;预览&#34;屏幕,这是你当前主题的背景(你已经为你的SplashScreen做了什么,它显示了主题背景)......

但如果你的&#34; @ style / MainTheme&#34;有一个简单的白色背景,这将是您重新进入应用程序时可能会看到的。

因此,您可以考虑使用&#34; SetTheme&#34; OnCreate中的方法。在此链接中有更多相关信息:

Vault official documentation

希望它有所帮助。