Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/Core/src/Platform/Android/ViewExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -556,8 +556,10 @@ internal static IDisposable OnLoaded(this View view, Action action)
EventHandler<AView.ViewAttachedToWindowEventArgs>? routedEventHandler = null;
ActionDisposable? disposable = new ActionDisposable(() =>
{
if (routedEventHandler != null)
if (routedEventHandler is not null && view.IsAlive())
{
view.ViewAttachedToWindow -= routedEventHandler;
}
});

routedEventHandler = (_, __) =>
Expand Down Expand Up @@ -596,16 +598,29 @@ internal static IDisposable OnUnloaded(this View view, Action action)
EventHandler<AView.ViewDetachedFromWindowEventArgs>? routedEventHandler = null;
ActionDisposable? disposable = new ActionDisposable(() =>
{
if (routedEventHandler != null)
if (routedEventHandler is not null)
view.ViewDetachedFromWindow -= routedEventHandler;
});

routedEventHandler = (_, __) =>
routedEventHandler = (sender,args) =>
{
// This event seems to fire prior to the view actually being
// detached from the window
if (view.IsLoaded() && Looper.MyLooper() is Looper q)
{
// We unsubscribe here because if we wait for the looper
// to schedule the work the view might get disposed by the time
// the unsubscribe code runs
if (disposable is not null)
{
if (args.DetachedView.IsAlive())
{
args.DetachedView.ViewDetachedFromWindow -= routedEventHandler;
}

routedEventHandler = null;
}

new Handler(q).Post(() =>
{
if (disposable is not null)
Expand Down
Loading