Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

Commit a628c46

Browse files
hartezrmarinho
authored andcommitted
Avoid exception when removing a page from navigation during Appearing handler (#1094)
* Theoretical repro * Repro * Remove unnecessary forced execution of pending transactions * Update issue data
1 parent c6da274 commit a628c46

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using System.Linq;
2+
using Xamarin.Forms.CustomAttributes;
3+
using Xamarin.Forms.Internals;
4+
5+
#if UITEST
6+
using Xamarin.UITest;
7+
using NUnit.Framework;
8+
using Xamarin.Forms.Core.UITests;
9+
#endif
10+
11+
namespace Xamarin.Forms.Controls.Issues
12+
{
13+
#if UITEST
14+
[Category(UITestCategories.Navigation)]
15+
#endif
16+
17+
[Preserve(AllMembers = true)]
18+
[Issue(IssueTracker.None, 1134, "Removing page during OnAppearing throws exception", PlatformAffected.Android)]
19+
public class RemovePageOnAppearing : TestContentPage
20+
{
21+
const string Success = "Success";
22+
23+
protected override void Init()
24+
{
25+
Appearing += async (sender, args) =>
26+
{
27+
var nav = new NavigationPage(Root());
28+
Application.Current.MainPage = nav;
29+
await nav.PushAsync(Intermediate());
30+
await nav.PushAsync(new PageWhichRemovesAnEarlierPageOnAppearing());
31+
};
32+
}
33+
34+
static ContentPage Root()
35+
{
36+
return new ContentPage { Content = new Label {Text = "Root"} };
37+
}
38+
39+
static ContentPage Intermediate()
40+
{
41+
return new ContentPage { Content = new Label {Text = "Intermediate page"} };
42+
}
43+
44+
[Preserve(AllMembers = true)]
45+
class PageWhichRemovesAnEarlierPageOnAppearing : ContentPage
46+
{
47+
public PageWhichRemovesAnEarlierPageOnAppearing()
48+
{
49+
var instructions = new Label { Text = "If you can see this, the test has passed" };
50+
51+
Content = new StackLayout { Children = { instructions, new Label { Text = Success } } };
52+
}
53+
54+
protected override void OnAppearing()
55+
{
56+
var toRemove = Navigation.NavigationStack.Skip(1).First();
57+
58+
// toRemove should be the IntermediatePage
59+
Navigation.RemovePage(toRemove);
60+
61+
base.OnAppearing();
62+
}
63+
}
64+
65+
#if UITEST
66+
[Test]
67+
public void RemovePageOnAppearingDoesNotCrash()
68+
{
69+
RunningApp.WaitForElement(Success);
70+
}
71+
#endif
72+
}
73+
}

Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@
212212
<Compile Include="$(MSBuildThisFileDirectory)Bugzilla39829.cs" />
213213
<Compile Include="$(MSBuildThisFileDirectory)Bugzilla39458.cs" />
214214
<Compile Include="$(MSBuildThisFileDirectory)Bugzilla39853.cs" />
215+
<Compile Include="$(MSBuildThisFileDirectory)RemovePageOnAppearing.cs" />
215216
<Compile Include="$(MSBuildThisFileDirectory)PlatformSpecifics_iOSTranslucentNavBarX.xaml.cs">
216217
<DependentUpon>PlatformSpecifics_iOSTranslucentNavBarX.xaml</DependentUpon>
217218
<SubType>Code</SubType>

Xamarin.Forms.Platform.Android/AppCompat/NavigationPageRenderer.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,6 @@ void RemovePage(Page page)
553553
transaction.DisallowAddToBackStack();
554554
transaction.Remove(fragment);
555555
transaction.CommitAllowingStateLoss();
556-
FragmentManager.ExecutePendingTransactions();
557556

558557
// And remove the fragment from our own stack
559558
_fragmentStack.Remove(fragment);

0 commit comments

Comments
 (0)