Skip to content

[Android] Shell: Don't throw when resuming the fragment of a removed ShellSection - #40052

Merged
kubaflo merged 2 commits into
dotnet:candidate/11.0.1xx-sr2from
Dreamescaper:fix/shell-section-fragment-resume-after-disconnect
Nov 10, 2026
Merged

kubaflo merged 2 commits into
dotnet:candidate/11.0.1xx-sr2from
Dreamescaper:fix/shell-section-fragment-resume-after-disconnect

Conversation

@Dreamescaper

Copy link
Copy Markdown
Contributor

Note

Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!

Description of Change

On Android with the handler-based Shell, an app crashes with InvalidOperationException: VirtualView cannot be null here in ShellSectionWrapperFragment.OnResume() when it resumes after a ShellItem was replaced while it was in the background.

When a section is removed, ShellSectionHandlerAdapter.Dispose() disconnects its handler. Since #37048 it leaves the wrapper fragment to FragmentManager, which can still resume it. OnResume() already returns early for a disconnected handler, but it checks _handler.VirtualView is null. That reads the typed ElementHandler<ShellSection, AView>.VirtualView, whose getter throws once disconnected, so the guard itself throws.

The fix checks the untyped ((IElementHandler)_handler).VirtualView, the same way ShellSectionHandler.iOS.cs does.

Not changed here: several other VirtualView is null checks in ShellSectionHandler.Android.cs (for example in IsCurrentlyActiveSection() and SetupViewPagerAdapter()) have the same problem. They aren't reached on this path, so I left them out to keep this PR small.

Tests

Added ShellHandlerTests_Shell.ResumingFragmentOfRemovedShellSectionDoesNotThrow. It creates a section renderer, disposes it the same way ShellItemHandler.UpdateSections does for a removed section, and resumes its wrapper fragment.

Ran ShellHandlerTests_Shell locally on an Android emulator (API 36, x86_64):

  • With the fix: 80/80 pass.
  • Without the fix: the new test fails with System.InvalidOperationException: VirtualView cannot be null here at ShellSectionWrapperFragment.OnResume(); the other 79 pass.

Issues Fixed

Fixes #40051

…ShellSection

ShellSectionWrapperFragment.OnResume guarded against a disconnected handler
with _handler.VirtualView is null, but the typed ElementHandler<,>.VirtualView
getter throws once the handler is disconnected. FragmentManager keeps the
fragment after its section is removed and can resume it later, e.g. when the
section was removed while the app was in the background, so the app crashed
with "VirtualView cannot be null here". Check the untyped VirtualView instead.

Fixes dotnet#40051
@github-actions

github-actions Bot commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

🚀 Dogfood this PR with:

⚠️ WARNING: Do not do this without first carefully reviewing the code of this PR to satisfy yourself it is safe.

curl -fsSL https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.sh | bash -s -- 40052

Or

  • Run remotely in PowerShell:
iex "& { $(irm https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.ps1) } 40052"

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service dotnet-policy-service Bot added the community ✨ Community Contribution label Nov 8, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Hey there @@Dreamescaper! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Hey there @Dreamescaper! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@kubaflo

This comment has been minimized.

@kubaflo

This comment has been minimized.

@github-actions github-actions Bot added the s/agent-review-in-progress AI review is currently running for this PR label Nov 8, 2026
@azure-pipelines

This comment has been minimized.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expert Review — 0 findings

Reviewed commit 25d3ba3.

@MauiBot

MauiBot commented Nov 8, 2026

Copy link
Copy Markdown
Collaborator

PR Test Gate

@Dreamescaper — existing-test verification for commit 25d3ba39.

Result PASSED Platform Android Commit 25d3ba39


🧪 Gate analysis — click to expand

✅ Passed. The selected verification satisfied the gate's passing conditions. Unmatched, skipped, or inconclusive groups are not passing evidence.

📱 Device tests
Test group Without fix With fix
ShellHandlerTests_Shell (ResumingFragmentOfRemovedShellSectionDoesNotThrow) ✅ Failed as expected (559s) ✅ Passed (423s)
Execution log excerpts

Without fix ShellHandlerTests_Shell (ResumingFragmentOfRemovedShellSectionDoesNotThrow)

moryBytes": 8322981888
          },
          "target": {
            "kind": "emulator",
            "name": "sdk_gphone_x86_64",
            "identifier": "emulator-5554",
            "operatingSystem": "Android",
            "operatingSystemVersion": "11",
            "apiLevel": 30,
            "architecture": "x86_64",
            "supportedArchitectures": [
              "x86_64",
              "x86",
              "arm64-v8a",
              "armeabi-v7a",
              "armeabi"
            ],
            "manufacturer": "Google",
            "model": "sdk_gphone_x86_64",
            "productName": "sdk_gphone_x86_64",
            "buildFingerprint": "google/sdk_gphone_x86_64/generic_x86_64_arm64:11/RSR1.201211.001/7027799:user/release-keys",
            "cpuModel": "0",
            "cpuMaxFrequencyHertz": 2000,
            "totalMemoryBytes": 2077544448
          }
        }
      }
      <<XHARNESS_RESULT_END>>
info: Attempting to remove apk 'com.microsoft.maui.controls.devicetests'..
info: Successfully uninstalled com.microsoft.maui.controls.devicetests
XHarness exit code: 1 (TESTS_FAILED)
  Passed: 0
  Failed: 1
  Skipped: 0
  Total: 1
  Tests completed with exit code: 1

With fix ShellHandlerTests_Shell (ResumingFragmentOfRemovedShellSectionDoesNotThrow)

            "totalMemoryBytes": 8322981888
          },
          "target": {
            "kind": "emulator",
            "name": "sdk_gphone_x86_64",
            "identifier": "emulator-5554",
            "operatingSystem": "Android",
            "operatingSystemVersion": "11",
            "apiLevel": 30,
            "architecture": "x86_64",
            "supportedArchitectures": [
              "x86_64",
              "x86",
              "arm64-v8a",
              "armeabi-v7a",
              "armeabi"
            ],
            "manufacturer": "Google",
            "model": "sdk_gphone_x86_64",
            "productName": "sdk_gphone_x86_64",
            "buildFingerprint": "google/sdk_gphone_x86_64/generic_x86_64_arm64:11/RSR1.201211.001/7027799:user/release-keys",
            "cpuModel": "0",
            "cpuMaxFrequencyHertz": 2000,
            "totalMemoryBytes": 2077544448
          }
        }
      }
      <<XHARNESS_RESULT_END>>
info: Attempting to remove apk 'com.microsoft.maui.controls.devicetests'..
info: Successfully uninstalled com.microsoft.maui.controls.devicetests
XHarness exit code: 0
  Passed: 1
  Failed: 0
  Skipped: 0
  Total: 1
  Tests completed successfully

PR head: 25d3ba39.

Without-fix baseline: 9fb91b46.

@MauiBot MauiBot added s/agent-gate-passed AI verified tests catch the bug (fail without fix, pass with fix) and removed s/agent-review-in-progress AI review is currently running for this PR labels Nov 8, 2026
@kubaflo
kubaflo changed the base branch from net11 to candidate/11.0.1xx-sr2 November 10, 2026 21:44
@kubaflo
kubaflo merged commit c476908 into dotnet:candidate/11.0.1xx-sr2 Nov 10, 2026
165 of 175 checks passed
kubaflo added a commit that referenced this pull request Nov 11, 2026
…ShellSection (#40052)

<!-- Please let the below note in for people that find this PR -->
> [!NOTE]
> Are you waiting for the changes in this PR to be merged?
> It would be very helpful if you could [test the resulting
artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
Thank you!

### Description of Change

On Android with the handler-based Shell, an app crashes with
`InvalidOperationException: VirtualView cannot be null here` in
`ShellSectionWrapperFragment.OnResume()` when it resumes after a
ShellItem was replaced while it was in the background.

When a section is removed, `ShellSectionHandlerAdapter.Dispose()`
disconnects its handler. Since #37048 it leaves the wrapper fragment to
`FragmentManager`, which can still resume it. `OnResume()` already
returns early for a disconnected handler, but it checks
`_handler.VirtualView is null`. That reads the typed
`ElementHandler<ShellSection, AView>.VirtualView`, whose getter throws
once disconnected, so the guard itself throws.

The fix checks the untyped `((IElementHandler)_handler).VirtualView`,
the same way `ShellSectionHandler.iOS.cs` does.

Not changed here: several other `VirtualView is null` checks in
`ShellSectionHandler.Android.cs` (for example in
`IsCurrentlyActiveSection()` and `SetupViewPagerAdapter()`) have the
same problem. They aren't reached on this path, so I left them out to
keep this PR small.

### Tests

Added
`ShellHandlerTests_Shell.ResumingFragmentOfRemovedShellSectionDoesNotThrow`.
It creates a section renderer, disposes it the same way
`ShellItemHandler.UpdateSections` does for a removed section, and
resumes its wrapper fragment.

Ran `ShellHandlerTests_Shell` locally on an Android emulator (API 36,
x86_64):
- With the fix: 80/80 pass.
- Without the fix: the new test fails with
`System.InvalidOperationException: VirtualView cannot be null here at
ShellSectionWrapperFragment.OnResume()`; the other 79 pass.

### Issues Fixed

Fixes #40051

Co-authored-by: Jakub Florkowski <42434498+kubaflo@users.noreply.github.com>
kubaflo added a commit that referenced this pull request Nov 13, 2026
…ShellSection (#40052)

<!-- Please let the below note in for people that find this PR -->
> [!NOTE]
> Are you waiting for the changes in this PR to be merged?
> It would be very helpful if you could [test the resulting
artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
Thank you!

### Description of Change

On Android with the handler-based Shell, an app crashes with
`InvalidOperationException: VirtualView cannot be null here` in
`ShellSectionWrapperFragment.OnResume()` when it resumes after a
ShellItem was replaced while it was in the background.

When a section is removed, `ShellSectionHandlerAdapter.Dispose()`
disconnects its handler. Since #37048 it leaves the wrapper fragment to
`FragmentManager`, which can still resume it. `OnResume()` already
returns early for a disconnected handler, but it checks
`_handler.VirtualView is null`. That reads the typed
`ElementHandler<ShellSection, AView>.VirtualView`, whose getter throws
once disconnected, so the guard itself throws.

The fix checks the untyped `((IElementHandler)_handler).VirtualView`,
the same way `ShellSectionHandler.iOS.cs` does.

Not changed here: several other `VirtualView is null` checks in
`ShellSectionHandler.Android.cs` (for example in
`IsCurrentlyActiveSection()` and `SetupViewPagerAdapter()`) have the
same problem. They aren't reached on this path, so I left them out to
keep this PR small.

### Tests

Added
`ShellHandlerTests_Shell.ResumingFragmentOfRemovedShellSectionDoesNotThrow`.
It creates a section renderer, disposes it the same way
`ShellItemHandler.UpdateSections` does for a removed section, and
resumes its wrapper fragment.

Ran `ShellHandlerTests_Shell` locally on an Android emulator (API 36,
x86_64):
- With the fix: 80/80 pass.
- Without the fix: the new test fails with
`System.InvalidOperationException: VirtualView cannot be null here at
ShellSectionWrapperFragment.OnResume()`; the other 79 pass.

### Issues Fixed

Fixes #40051

Co-authored-by: Jakub Florkowski <42434498+kubaflo@users.noreply.github.com>
kubaflo added a commit that referenced this pull request Nov 16, 2026
…ShellSection (#40052)

<!-- Please let the below note in for people that find this PR -->
> [!NOTE]
> Are you waiting for the changes in this PR to be merged?
> It would be very helpful if you could [test the resulting
artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
Thank you!

### Description of Change

On Android with the handler-based Shell, an app crashes with
`InvalidOperationException: VirtualView cannot be null here` in
`ShellSectionWrapperFragment.OnResume()` when it resumes after a
ShellItem was replaced while it was in the background.

When a section is removed, `ShellSectionHandlerAdapter.Dispose()`
disconnects its handler. Since #37048 it leaves the wrapper fragment to
`FragmentManager`, which can still resume it. `OnResume()` already
returns early for a disconnected handler, but it checks
`_handler.VirtualView is null`. That reads the typed
`ElementHandler<ShellSection, AView>.VirtualView`, whose getter throws
once disconnected, so the guard itself throws.

The fix checks the untyped `((IElementHandler)_handler).VirtualView`,
the same way `ShellSectionHandler.iOS.cs` does.

Not changed here: several other `VirtualView is null` checks in
`ShellSectionHandler.Android.cs` (for example in
`IsCurrentlyActiveSection()` and `SetupViewPagerAdapter()`) have the
same problem. They aren't reached on this path, so I left them out to
keep this PR small.

### Tests

Added
`ShellHandlerTests_Shell.ResumingFragmentOfRemovedShellSectionDoesNotThrow`.
It creates a section renderer, disposes it the same way
`ShellItemHandler.UpdateSections` does for a removed section, and
resumes its wrapper fragment.

Ran `ShellHandlerTests_Shell` locally on an Android emulator (API 36,
x86_64):
- With the fix: 80/80 pass.
- Without the fix: the new test fails with
`System.InvalidOperationException: VirtualView cannot be null here at
ShellSectionWrapperFragment.OnResume()`; the other 79 pass.

### Issues Fixed

Fixes #40051

Co-authored-by: Jakub Florkowski <42434498+kubaflo@users.noreply.github.com>
kubaflo added a commit that referenced this pull request Nov 16, 2026
…ShellSection (#40052)

<!-- Please let the below note in for people that find this PR -->
> [!NOTE]
> Are you waiting for the changes in this PR to be merged?
> It would be very helpful if you could [test the resulting
artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
Thank you!

### Description of Change

On Android with the handler-based Shell, an app crashes with
`InvalidOperationException: VirtualView cannot be null here` in
`ShellSectionWrapperFragment.OnResume()` when it resumes after a
ShellItem was replaced while it was in the background.

When a section is removed, `ShellSectionHandlerAdapter.Dispose()`
disconnects its handler. Since #37048 it leaves the wrapper fragment to
`FragmentManager`, which can still resume it. `OnResume()` already
returns early for a disconnected handler, but it checks
`_handler.VirtualView is null`. That reads the typed
`ElementHandler<ShellSection, AView>.VirtualView`, whose getter throws
once disconnected, so the guard itself throws.

The fix checks the untyped `((IElementHandler)_handler).VirtualView`,
the same way `ShellSectionHandler.iOS.cs` does.

Not changed here: several other `VirtualView is null` checks in
`ShellSectionHandler.Android.cs` (for example in
`IsCurrentlyActiveSection()` and `SetupViewPagerAdapter()`) have the
same problem. They aren't reached on this path, so I left them out to
keep this PR small.

### Tests

Added
`ShellHandlerTests_Shell.ResumingFragmentOfRemovedShellSectionDoesNotThrow`.
It creates a section renderer, disposes it the same way
`ShellItemHandler.UpdateSections` does for a removed section, and
resumes its wrapper fragment.

Ran `ShellHandlerTests_Shell` locally on an Android emulator (API 36,
x86_64):
- With the fix: 80/80 pass.
- Without the fix: the new test fails with
`System.InvalidOperationException: VirtualView cannot be null here at
ShellSectionWrapperFragment.OnResume()`; the other 79 pass.

### Issues Fixed

Fixes #40051

Co-authored-by: Jakub Florkowski <42434498+kubaflo@users.noreply.github.com>
kubaflo added a commit that referenced this pull request Nov 17, 2026
…ShellSection (#40052)

<!-- Please let the below note in for people that find this PR -->
> [!NOTE]
> Are you waiting for the changes in this PR to be merged?
> It would be very helpful if you could [test the resulting
artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
Thank you!

### Description of Change

On Android with the handler-based Shell, an app crashes with
`InvalidOperationException: VirtualView cannot be null here` in
`ShellSectionWrapperFragment.OnResume()` when it resumes after a
ShellItem was replaced while it was in the background.

When a section is removed, `ShellSectionHandlerAdapter.Dispose()`
disconnects its handler. Since #37048 it leaves the wrapper fragment to
`FragmentManager`, which can still resume it. `OnResume()` already
returns early for a disconnected handler, but it checks
`_handler.VirtualView is null`. That reads the typed
`ElementHandler<ShellSection, AView>.VirtualView`, whose getter throws
once disconnected, so the guard itself throws.

The fix checks the untyped `((IElementHandler)_handler).VirtualView`,
the same way `ShellSectionHandler.iOS.cs` does.

Not changed here: several other `VirtualView is null` checks in
`ShellSectionHandler.Android.cs` (for example in
`IsCurrentlyActiveSection()` and `SetupViewPagerAdapter()`) have the
same problem. They aren't reached on this path, so I left them out to
keep this PR small.

### Tests

Added
`ShellHandlerTests_Shell.ResumingFragmentOfRemovedShellSectionDoesNotThrow`.
It creates a section renderer, disposes it the same way
`ShellItemHandler.UpdateSections` does for a removed section, and
resumes its wrapper fragment.

Ran `ShellHandlerTests_Shell` locally on an Android emulator (API 36,
x86_64):
- With the fix: 80/80 pass.
- Without the fix: the new test fails with
`System.InvalidOperationException: VirtualView cannot be null here at
ShellSectionWrapperFragment.OnResume()`; the other 79 pass.

### Issues Fixed

Fixes #40051

Co-authored-by: Jakub Florkowski <42434498+kubaflo@users.noreply.github.com>
kubaflo added a commit that referenced this pull request Nov 18, 2026
…ShellSection (#40052)

<!-- Please let the below note in for people that find this PR -->
> [!NOTE]
> Are you waiting for the changes in this PR to be merged?
> It would be very helpful if you could [test the resulting
artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
Thank you!

### Description of Change

On Android with the handler-based Shell, an app crashes with
`InvalidOperationException: VirtualView cannot be null here` in
`ShellSectionWrapperFragment.OnResume()` when it resumes after a
ShellItem was replaced while it was in the background.

When a section is removed, `ShellSectionHandlerAdapter.Dispose()`
disconnects its handler. Since #37048 it leaves the wrapper fragment to
`FragmentManager`, which can still resume it. `OnResume()` already
returns early for a disconnected handler, but it checks
`_handler.VirtualView is null`. That reads the typed
`ElementHandler<ShellSection, AView>.VirtualView`, whose getter throws
once disconnected, so the guard itself throws.

The fix checks the untyped `((IElementHandler)_handler).VirtualView`,
the same way `ShellSectionHandler.iOS.cs` does.

Not changed here: several other `VirtualView is null` checks in
`ShellSectionHandler.Android.cs` (for example in
`IsCurrentlyActiveSection()` and `SetupViewPagerAdapter()`) have the
same problem. They aren't reached on this path, so I left them out to
keep this PR small.

### Tests

Added
`ShellHandlerTests_Shell.ResumingFragmentOfRemovedShellSectionDoesNotThrow`.
It creates a section renderer, disposes it the same way
`ShellItemHandler.UpdateSections` does for a removed section, and
resumes its wrapper fragment.

Ran `ShellHandlerTests_Shell` locally on an Android emulator (API 36,
x86_64):
- With the fix: 80/80 pass.
- Without the fix: the new test fails with
`System.InvalidOperationException: VirtualView cannot be null here at
ShellSectionWrapperFragment.OnResume()`; the other 79 pass.

### Issues Fixed

Fixes #40051

Co-authored-by: Jakub Florkowski <42434498+kubaflo@users.noreply.github.com>
kubaflo added a commit that referenced this pull request Nov 18, 2026
…ShellSection (#40052)

<!-- Please let the below note in for people that find this PR -->
> [!NOTE]
> Are you waiting for the changes in this PR to be merged?
> It would be very helpful if you could [test the resulting
artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
Thank you!

### Description of Change

On Android with the handler-based Shell, an app crashes with
`InvalidOperationException: VirtualView cannot be null here` in
`ShellSectionWrapperFragment.OnResume()` when it resumes after a
ShellItem was replaced while it was in the background.

When a section is removed, `ShellSectionHandlerAdapter.Dispose()`
disconnects its handler. Since #37048 it leaves the wrapper fragment to
`FragmentManager`, which can still resume it. `OnResume()` already
returns early for a disconnected handler, but it checks
`_handler.VirtualView is null`. That reads the typed
`ElementHandler<ShellSection, AView>.VirtualView`, whose getter throws
once disconnected, so the guard itself throws.

The fix checks the untyped `((IElementHandler)_handler).VirtualView`,
the same way `ShellSectionHandler.iOS.cs` does.

Not changed here: several other `VirtualView is null` checks in
`ShellSectionHandler.Android.cs` (for example in
`IsCurrentlyActiveSection()` and `SetupViewPagerAdapter()`) have the
same problem. They aren't reached on this path, so I left them out to
keep this PR small.

### Tests

Added
`ShellHandlerTests_Shell.ResumingFragmentOfRemovedShellSectionDoesNotThrow`.
It creates a section renderer, disposes it the same way
`ShellItemHandler.UpdateSections` does for a removed section, and
resumes its wrapper fragment.

Ran `ShellHandlerTests_Shell` locally on an Android emulator (API 36,
x86_64):
- With the fix: 80/80 pass.
- Without the fix: the new test fails with
`System.InvalidOperationException: VirtualView cannot be null here at
ShellSectionWrapperFragment.OnResume()`; the other 79 pass.

### Issues Fixed

Fixes #40051

Co-authored-by: Jakub Florkowski <42434498+kubaflo@users.noreply.github.com>
kubaflo added a commit that referenced this pull request Nov 18, 2026
…ShellSection (#40052)

<!-- Please let the below note in for people that find this PR -->
> [!NOTE]
> Are you waiting for the changes in this PR to be merged?
> It would be very helpful if you could [test the resulting
artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
Thank you!

### Description of Change

On Android with the handler-based Shell, an app crashes with
`InvalidOperationException: VirtualView cannot be null here` in
`ShellSectionWrapperFragment.OnResume()` when it resumes after a
ShellItem was replaced while it was in the background.

When a section is removed, `ShellSectionHandlerAdapter.Dispose()`
disconnects its handler. Since #37048 it leaves the wrapper fragment to
`FragmentManager`, which can still resume it. `OnResume()` already
returns early for a disconnected handler, but it checks
`_handler.VirtualView is null`. That reads the typed
`ElementHandler<ShellSection, AView>.VirtualView`, whose getter throws
once disconnected, so the guard itself throws.

The fix checks the untyped `((IElementHandler)_handler).VirtualView`,
the same way `ShellSectionHandler.iOS.cs` does.

Not changed here: several other `VirtualView is null` checks in
`ShellSectionHandler.Android.cs` (for example in
`IsCurrentlyActiveSection()` and `SetupViewPagerAdapter()`) have the
same problem. They aren't reached on this path, so I left them out to
keep this PR small.

### Tests

Added
`ShellHandlerTests_Shell.ResumingFragmentOfRemovedShellSectionDoesNotThrow`.
It creates a section renderer, disposes it the same way
`ShellItemHandler.UpdateSections` does for a removed section, and
resumes its wrapper fragment.

Ran `ShellHandlerTests_Shell` locally on an Android emulator (API 36,
x86_64):
- With the fix: 80/80 pass.
- Without the fix: the new test fails with
`System.InvalidOperationException: VirtualView cannot be null here at
ShellSectionWrapperFragment.OnResume()`; the other 79 pass.

### Issues Fixed

Fixes #40051

Co-authored-by: Jakub Florkowski <42434498+kubaflo@users.noreply.github.com>
kubaflo added a commit that referenced this pull request Nov 19, 2026
…ShellSection (#40052)

<!-- Please let the below note in for people that find this PR -->
> [!NOTE]
> Are you waiting for the changes in this PR to be merged?
> It would be very helpful if you could [test the resulting
artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
Thank you!

### Description of Change

On Android with the handler-based Shell, an app crashes with
`InvalidOperationException: VirtualView cannot be null here` in
`ShellSectionWrapperFragment.OnResume()` when it resumes after a
ShellItem was replaced while it was in the background.

When a section is removed, `ShellSectionHandlerAdapter.Dispose()`
disconnects its handler. Since #37048 it leaves the wrapper fragment to
`FragmentManager`, which can still resume it. `OnResume()` already
returns early for a disconnected handler, but it checks
`_handler.VirtualView is null`. That reads the typed
`ElementHandler<ShellSection, AView>.VirtualView`, whose getter throws
once disconnected, so the guard itself throws.

The fix checks the untyped `((IElementHandler)_handler).VirtualView`,
the same way `ShellSectionHandler.iOS.cs` does.

Not changed here: several other `VirtualView is null` checks in
`ShellSectionHandler.Android.cs` (for example in
`IsCurrentlyActiveSection()` and `SetupViewPagerAdapter()`) have the
same problem. They aren't reached on this path, so I left them out to
keep this PR small.

### Tests

Added
`ShellHandlerTests_Shell.ResumingFragmentOfRemovedShellSectionDoesNotThrow`.
It creates a section renderer, disposes it the same way
`ShellItemHandler.UpdateSections` does for a removed section, and
resumes its wrapper fragment.

Ran `ShellHandlerTests_Shell` locally on an Android emulator (API 36,
x86_64):
- With the fix: 80/80 pass.
- Without the fix: the new test fails with
`System.InvalidOperationException: VirtualView cannot be null here at
ShellSectionWrapperFragment.OnResume()`; the other 79 pass.

### Issues Fixed

Fixes #40051

Co-authored-by: Jakub Florkowski <42434498+kubaflo@users.noreply.github.com>
kubaflo added a commit that referenced this pull request Nov 22, 2026
…ShellSection (#40052)

<!-- Please let the below note in for people that find this PR -->
> [!NOTE]
> Are you waiting for the changes in this PR to be merged?
> It would be very helpful if you could [test the resulting
artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
Thank you!

### Description of Change

On Android with the handler-based Shell, an app crashes with
`InvalidOperationException: VirtualView cannot be null here` in
`ShellSectionWrapperFragment.OnResume()` when it resumes after a
ShellItem was replaced while it was in the background.

When a section is removed, `ShellSectionHandlerAdapter.Dispose()`
disconnects its handler. Since #37048 it leaves the wrapper fragment to
`FragmentManager`, which can still resume it. `OnResume()` already
returns early for a disconnected handler, but it checks
`_handler.VirtualView is null`. That reads the typed
`ElementHandler<ShellSection, AView>.VirtualView`, whose getter throws
once disconnected, so the guard itself throws.

The fix checks the untyped `((IElementHandler)_handler).VirtualView`,
the same way `ShellSectionHandler.iOS.cs` does.

Not changed here: several other `VirtualView is null` checks in
`ShellSectionHandler.Android.cs` (for example in
`IsCurrentlyActiveSection()` and `SetupViewPagerAdapter()`) have the
same problem. They aren't reached on this path, so I left them out to
keep this PR small.

### Tests

Added
`ShellHandlerTests_Shell.ResumingFragmentOfRemovedShellSectionDoesNotThrow`.
It creates a section renderer, disposes it the same way
`ShellItemHandler.UpdateSections` does for a removed section, and
resumes its wrapper fragment.

Ran `ShellHandlerTests_Shell` locally on an Android emulator (API 36,
x86_64):
- With the fix: 80/80 pass.
- Without the fix: the new test fails with
`System.InvalidOperationException: VirtualView cannot be null here at
ShellSectionWrapperFragment.OnResume()`; the other 79 pass.

### Issues Fixed

Fixes #40051

Co-authored-by: Jakub Florkowski <42434498+kubaflo@users.noreply.github.com>
kubaflo added a commit that referenced this pull request Nov 23, 2026
…ShellSection (#40052)

<!-- Please let the below note in for people that find this PR -->
> [!NOTE]
> Are you waiting for the changes in this PR to be merged?
> It would be very helpful if you could [test the resulting
artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
Thank you!

### Description of Change

On Android with the handler-based Shell, an app crashes with
`InvalidOperationException: VirtualView cannot be null here` in
`ShellSectionWrapperFragment.OnResume()` when it resumes after a
ShellItem was replaced while it was in the background.

When a section is removed, `ShellSectionHandlerAdapter.Dispose()`
disconnects its handler. Since #37048 it leaves the wrapper fragment to
`FragmentManager`, which can still resume it. `OnResume()` already
returns early for a disconnected handler, but it checks
`_handler.VirtualView is null`. That reads the typed
`ElementHandler<ShellSection, AView>.VirtualView`, whose getter throws
once disconnected, so the guard itself throws.

The fix checks the untyped `((IElementHandler)_handler).VirtualView`,
the same way `ShellSectionHandler.iOS.cs` does.

Not changed here: several other `VirtualView is null` checks in
`ShellSectionHandler.Android.cs` (for example in
`IsCurrentlyActiveSection()` and `SetupViewPagerAdapter()`) have the
same problem. They aren't reached on this path, so I left them out to
keep this PR small.

### Tests

Added
`ShellHandlerTests_Shell.ResumingFragmentOfRemovedShellSectionDoesNotThrow`.
It creates a section renderer, disposes it the same way
`ShellItemHandler.UpdateSections` does for a removed section, and
resumes its wrapper fragment.

Ran `ShellHandlerTests_Shell` locally on an Android emulator (API 36,
x86_64):
- With the fix: 80/80 pass.
- Without the fix: the new test fails with
`System.InvalidOperationException: VirtualView cannot be null here at
ShellSectionWrapperFragment.OnResume()`; the other 79 pass.

### Issues Fixed

Fixes #40051

Co-authored-by: Jakub Florkowski <42434498+kubaflo@users.noreply.github.com>
@kubaflo kubaflo added this to the .NET 11 SR2 milestone Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community ✨ Community Contribution s/agent-gate-passed AI verified tests catch the bug (fail without fix, pass with fix)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants