Fix - Prevent race condition between activity create and dispose. (#6596)
authorKevin Petit <kevin.petit@outlook.com>
Fri, 19 Jul 2019 10:50:02 +0000 (12:50 +0200)
committerRui Marinho <me@ruimarinho.net>
Fri, 19 Jul 2019 10:50:02 +0000 (11:50 +0100)
Xamarin.Forms.Platform.Android/AppCompat/FormsAppCompatActivity.cs

index eb20ae6..965fc39 100644 (file)
@@ -3,6 +3,7 @@
 using System;
 using System.ComponentModel;
 using System.Linq;
+using System.Threading;
 using Android.App;
 using Android.Content;
 using Android.Content.Res;
@@ -10,7 +11,6 @@ using Android.OS;
 using Android.Runtime;
 using Android.Support.V7.App;
 using Android.Views;
-using Android.Widget;
 using Xamarin.Forms.Platform.Android.AppCompat;
 using Xamarin.Forms.PlatformConfiguration.AndroidSpecific;
 using Xamarin.Forms.PlatformConfiguration.AndroidSpecific.AppCompat;
@@ -40,6 +40,8 @@ namespace Xamarin.Forms.Platform.Android
                bool _activityCreated;
                PowerSaveModeBroadcastReceiver _powerSaveModeBroadcastReceiver;
 
+               static readonly ManualResetEventSlim PreviousActivityDestroying = new ManualResetEventSlim(true);
+
                // Override this if you want to handle the default Android behavior of restoring fragments on an application restart
                protected virtual bool AllowFragmentRestore => false;
 
@@ -143,6 +145,9 @@ namespace Xamarin.Forms.Platform.Android
 
                        application.PropertyChanged += AppOnPropertyChanged;
 
+                       // Wait if old activity destroying is not finished
+                       PreviousActivityDestroying.Wait();
+
                        Profile.FramePartition(nameof(SetMainPage));
 
                        SetMainPage();
@@ -206,6 +211,8 @@ namespace Xamarin.Forms.Platform.Android
 
                protected override void OnDestroy()
                {
+                       PreviousActivityDestroying.Reset();
+
                        if (_application != null)
                                _application.PropertyChanged -= AppOnPropertyChanged;
 
@@ -215,6 +222,8 @@ namespace Xamarin.Forms.Platform.Android
 
                        Platform?.Dispose();
 
+                       PreviousActivityDestroying.Set();
+
                        // call at the end to avoid race conditions with Platform dispose
                        base.OnDestroy();
                }