Skip to content

Commit c469bda

Browse files
authored
[Android][Modal] Fixed crash caused by accessing a disposed Java peer when comparing fragments in OnFragmentDestroyed (#809)
1 parent a6fb21c commit c469bda

2 files changed

Lines changed: 22 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## [55.2.4]
2+
- [Android][Modal] Fixed crash caused by accessing a disposed Java peer when comparing fragments in OnFragmentDestroyed
3+
14
## [55.2.3]
25
- [Android][Entry][Editor] Fixed potential crash.
36

src/library/DIPS.Mobile.UI/API/Library/Android/FragmentLifeCycleCallback.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,30 @@ public override void OnFragmentDestroyed(FragmentManager fm, Fragment f)
4646
{
4747
if (s_currentDialogFragmentReferenceStack?.Peek()?.TryGetTarget(out var currentDialogFragment) ?? false)
4848
{
49-
if (currentDialogFragment.Equals(dialogFragment))
49+
// If either Java peer is already disposed, we can't compare via JNI.
50+
// Since this fragment is being destroyed, pop it to avoid a stale entry.
51+
if (currentDialogFragment.Handle == IntPtr.Zero || dialogFragment.Handle == IntPtr.Zero)
5052
{
5153
s_currentDialogFragmentReferenceStack.Pop();
5254
}
55+
else
56+
{
57+
try
58+
{
59+
if (currentDialogFragment.Equals(dialogFragment))
60+
{
61+
s_currentDialogFragmentReferenceStack.Pop();
62+
}
63+
}
64+
catch (ObjectDisposedException)
65+
{
66+
s_currentDialogFragmentReferenceStack.Pop();
67+
}
68+
}
5369
}
70+
71+
base.OnFragmentDestroyed(fm, f);
5472
}
55-
base.OnFragmentDestroyed(fm, f);
5673
}
5774

5875
public override void OnFragmentStopped(FragmentManager fm, Fragment f)

0 commit comments

Comments
 (0)