Fix MasterDetailPage event cycle (#754)
authoradrianknight89 <adrianknight89@outlook.com>
Tue, 14 Mar 2017 11:57:32 +0000 (06:57 -0500)
committerRui Marinho <me@ruimarinho.net>
Tue, 14 Mar 2017 11:57:32 +0000 (11:57 +0000)
sample code

add tag

change message

changes

changes 2

changes 3

changes 4

Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla52318.cs [new file with mode: 0644]
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems
Xamarin.Forms.Platform.Android/AppCompat/FragmentContainer.cs

diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla52318.cs b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla52318.cs
new file mode 100644 (file)
index 0000000..8d86565
--- /dev/null
@@ -0,0 +1,55 @@
+using Xamarin.Forms.CustomAttributes;
+using Xamarin.Forms.Internals;
+
+#if UITEST
+using Xamarin.UITest;
+using NUnit.Framework;
+#endif
+
+namespace Xamarin.Forms.Controls.Issues
+{
+       [Preserve(AllMembers = true)]
+       [Issue(IssueTracker.Bugzilla, 52318, "OnAppearing/Disappearing triggers for all pages in navigationstack backgrounding/foregrounding app", PlatformAffected.Android)]
+       public class Bugzilla52318 : TestMasterDetailPage // or TestMasterDetailPage, etc ...
+       {
+               protected override void Init()
+               {
+                       Master = new ContentPage { Title = "Master page", Content = new Label { Text = "Master page" } };
+                       Detail = new NavigationPage(new ContentPage52318());
+               }
+       }
+
+       [Preserve(AllMembers = true)]
+       public class ContentPage52318 : ContentPage
+       {
+               public ContentPage52318()
+               {
+                       var stackLayout = new StackLayout();
+                       var label = new Label
+                       {
+                               Text = "Tap on the Navigate button as many times as you like to add to the navigation stack. An alert should be visible on page appearing. Hit the Home button and come back. Only the last page should alert."
+                       };
+                       stackLayout.Children.Add(label);
+
+                       var button = new Button
+                       {
+                               Text = "Navigate to a new page",
+                               Command = new Command(async () =>
+                               {
+                                       await Navigation.PushAsync(new ContentPage52318());
+                               })
+                       };
+                       stackLayout.Children.Add(button);
+
+                       Content = stackLayout;
+               }
+
+               protected override void OnAppearing()
+               {
+                       int count = (Parent as NavigationPage).Navigation.NavigationStack.Count;
+                       Title = $"Page: {count}";
+                       DisplayAlert("", Title + " appearing.", "OK");
+                       base.OnAppearing();
+               }
+       }
+}
\ No newline at end of file
index 83a5995..ef469ec 100644 (file)
     <Compile Include="$(MSBuildThisFileDirectory)Bugzilla47923.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Bugzilla48236.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Bugzilla47971.cs" />
+    <Compile Include="$(MSBuildThisFileDirectory)Bugzilla52318.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Bugzilla37290.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Bugzilla51553.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Bugzilla51802.cs" />
index 432fcd3..6cc493a 100644 (file)
@@ -120,28 +120,35 @@ namespace Xamarin.Forms.Platform.Android.AppCompat
 
                public override void OnPause()
                {
-                       var shouldSendEvent = Application.Current.OnThisPlatform().GetSendDisappearingEventOnPause();
+                       bool shouldSendEvent = Application.Current.OnThisPlatform().GetSendDisappearingEventOnPause();
                        if (shouldSendEvent)
-                       {
-                               Page currentPage = (Application.Current.MainPage as IPageContainer<Page>)?.CurrentPage;
-                               if (currentPage == null || currentPage == PageController)
-                                       PageController?.SendDisappearing();
-                       }
+                               SendLifecycleEvent(false);
 
                        base.OnPause();
                }
 
                public override void OnResume()
                {
-                       var shouldSendEvent = Application.Current.OnThisPlatform().GetSendAppearingEventOnResume();
+                       bool shouldSendEvent = Application.Current.OnThisPlatform().GetSendAppearingEventOnResume();
                        if (shouldSendEvent)
-                       {
-                               Page currentPage = (Application.Current.MainPage as IPageContainer<Page>)?.CurrentPage;
-                               if (UserVisibleHint && (currentPage == null || currentPage == PageController))
-                                       PageController?.SendAppearing();
-                       }
+                               SendLifecycleEvent(true);
 
                        base.OnResume();
                }
+
+               void SendLifecycleEvent(bool isAppearing)
+               {
+                       var masterDetailPage = Application.Current.MainPage as MasterDetailPage;
+                       var pageContainer = (masterDetailPage != null ? masterDetailPage.Detail : Application.Current.MainPage) as IPageContainer<Page>;
+                       Page currentPage = pageContainer?.CurrentPage;
+
+                       if(!(currentPage == null || currentPage == PageController))
+                               return;
+
+                       if (isAppearing && UserVisibleHint)
+                               PageController?.SendAppearing();
+                       else if(!isAppearing)
+                               PageController?.SendDisappearing();
+               }
        }
 }
\ No newline at end of file