Skip to content

[BUG] [Android] TouchBehavior crashes with MissingMethodException when AccessibilityManager invokes a disposed AccessibilityListener (no (IntPtr, JniHandleOwnership) constructor) #3317

Closed

Description

@mobliam

Is there an existing issue for this?

  • I have searched the existing issues

Did you read the "Reporting a bug" section on Contributing file?

Current Behavior

Same root cause as #2383 and #2950, both auto-closed for lack of a reproduction. This report adds the mechanism, a public reproduction project with a deterministic path, and a proposed fix.

With any accessibility service running on Android (TalkBack, a screen reader, Switch Access), the app crashes with:

System.NotSupportedException: Unable to activate instance of type CommunityToolkit.Maui.Behaviors.TouchBehavior+AccessibilityListener from native handle ...
 ---> System.MissingMethodException: No constructor found for CommunityToolkit.Maui.Behaviors.TouchBehavior+AccessibilityListener::.ctor(System.IntPtr, Android.Runtime.JniHandleOwnership)
 ---> Java.Interop.JavaLocationException: Exception_WasThrown
java.lang.Error: Java callstack:
      at crc640fd0ddb16fe433d4.TouchBehavior_AccessibilityListener.n_onAccessibilityStateChanged(Native Method)
      at crc640fd0ddb16fe433d4.TouchBehavior_AccessibilityListener.onAccessibilityStateChanged(TouchBehavior_Accessibiliener.java:22)
      at android.view.accessibility.AccessibilityManager.lambda$notifyAccessibilityStateChanged$0(AccessibilityManager.java:2488)
      at android.view.accessibility.AccessibilityManager$$ExternalSyntheticLambda0.run(D8$$SyntheticClass:0)
      at android.os.Handler.handleCallback(Handler.java:1095)
      at android.os.Handler.dispatchMessageImpl(Handler.java:135)
      at android.os.Handler.dispatchMessage(Handler.java:125)
      at android.os.Looper.loopOnce(Looper.java:296)
      at android.os.Looper.loop(Looper.java:397)
      at android.app.ActivityThread.main(ActivityThread.java:9523)

It fires shortly after cold start when an accessibility service is already enabled (one crash per TouchBehavior being torn down while the first page is replaced) and also on background/resume, which matches the two earlier reports.

Root cause

TouchBehavior.android.cs registers a private nested AccessibilityListener : Java.Lang.Object with the system AccessibilityManager in OnAttachedTo, and in OnDetachedFrom it removes the listener and immediately calls accessibilityListener.Dispose() on the managed peer.

Two things make that unsafe:

  1. Android delivers state changes from a snapshot. AccessibilityManager.notifyAccessibilityStateChanged() copies the listener map under lock and then post()s one Runnable per listener to the main Handler (see AOSP source). Calling removeAccessibilityStateChangeListener after the snapshot does not cancel the pending Runnable, and the Runnable keeps the Java proxy alive.
  2. The managed peer cannot be re-created. When the posted Runnable runs, the managed peer is already disposed, so .NET for Android tries to re-activate it through the (IntPtr, JniHandleOwnership) activation constructor. AccessibilityListener only has internal AccessibilityListener(TouchBehavior), so Java.Interop.TypeManager.CreateInstance throws MissingMethodException, wrapped in NotSupportedException, which escapes the JNI callback and kills the process.

The window is short but it is hit reliably in two situations: app start with an accessibility service already enabled (the first TouchBehavior attach creates the process AccessibilityManager, which receives its initial state and broadcasts while the first page is being replaced), and resume from background (state is re-broadcast).

There is also a deterministic variant: TouchBehavior.Dispose() → PlatformDispose() disposes accessibilityListener without unregistering it from accessibilityManager, so the next state change always calls the disposed peer.

The identical bug existed in Xamarin Community Toolkit's TouchEffect and was fixed by adding the activation constructor: xamarin/XamarinCommunityToolkit@33c36af

Expected Behavior

A state-change callback arriving for a detached or disposed TouchBehavior is a no-op. The app does not crash.

Steps To Reproduce

The linked repository contains a minimal Android-only MAUI app plus a repro.sh script. Both paths below were verified on an Android 13 (API 33) emulator with TalkBack.

  1. dotnet build -f net10.0-android -t:Install
  2. ./repro.sh simulate (no TalkBack needed) prints:
System.NotSupportedException: Unable to activate instance of type CommunityToolkit.Maui.Behaviors.TouchBehavior+AccessibilityListener from native handle 0x7ff7a29a98 (key_handle 0x8732737).
  --> System.MissingMethodException: No constructor found for CommunityToolkit.Maui.Behaviors.TouchBehavior+AccessibilityListener::.ctor(System.IntPtr, Android.Runtime.JniHandleOwnership)
  --> Java.Interop.JavaLocationException: Exception of type 'Java.Interop.JavaLocationException' was thrown.
  1. ./repro.sh (enables TalkBack through adb) crashes the process through the real AccessibilityManager dispatch:
FATAL EXCEPTION: main
android.runtime.JavaProxyThrowable: [System.NotSupportedException]: Unable to activate instance of type CommunityToolkit.Maui.Behaviors.TouchBehavior+AccessibilityListener from native handle ...
      at Java.Interop.TypeManager.CreateInstance + 0x2b2(Unknown Source)
      at Android.Runtime.AndroidValueManager.CreatePeer + 0xa(Unknown Source)
      at Java.Interop.JniRuntime+JniValueManager.GetPeer + 0x48(Unknown Source)
      at Java.Lang.Object.GetObject + 0xa(Unknown Source)
      at Android.Views.Accessibility.AccessibilityManager+IAccessibilityStateChangeListenerInvoker.n_OnAccessibilityStated_Z + 0xe(Unknown Source)
      at crc640fd0ddb16fe433d4.TouchBehavior_AccessibilityListener.n_onAccessibilityStateChanged(Native Method)
      at crc640fd0ddb16fe433d4.TouchBehavior_AccessibilityListener.onAccessibilityStateChanged(TouchBehavior_Accessibiliener.java:31)
      at android.view.accessibility.AccessibilityManager.lambda$notifyAccessibilityStateChanged$0(AccessibilityManager.java:1881)
      at android.os.Handler.handleCallback(Handler.java:942)
      at android.os.Looper.loopOnce(Looper.java:201)

Manual alternative: launch the app, tap 1 (detach), then tap 2a (exception shown on screen) or tap 2b and toggle TalkBack in Settings > Accessibility (app crashes).

What the app does, so the mechanism is explicit:

  1. A Border has a TouchBehavior attached. Before detaching, the page takes a JNI global reference to the toolkit's Java AccessibilityListener object (read through reflection). This stands in for the Runnable that AccessibilityManager posts to the main looper, which holds the same reference in the real-world race.
  2. border.Behaviors.Remove(touchBehavior): the toolkit's OnDetachedFrom unregisters the listener and disposes the managed peer. The Java object survives.
  3. Either call onAccessibilityStateChanged(true) on that Java object directly (simulate), or add it back to the process AccessibilityManager via JNI and toggle TalkBack so the system dispatches to it exactly as in production.

Side note: calling the public TouchBehavior.Dispose() while attached also leaves the listener registered with a disposed peer (PlatformDispose() disposes without unregistering), but it additionally disposes the host view's platform peer, so the next layout pass dies with a NullPointerException in PlatformInterop.measureAndGetWidthAndHeight first. That looks like a separate defect.

Link to public reproduction project repository

https://github.com/mobliam/TouchBehaviorA11yRepro.git

Environment

- .NET MAUI CommunityToolkit: 15.0.1 (the `main` branch still has no activation constructor as of d8a05a4)
- OS: Android 16 (production) and Android 13 emulator API 33 (reproduction), TalkBack enabled; earlier reports cover Android 11–15
- .NET MAUI: 10.0.100 / .NET 10

Anything else?

Proposed fix, in src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/Touch/TouchBehavior.android.cs:

sealed class AccessibilityListener : Java.Lang.Object,
      AccessibilityManager.IAccessibilityStateChangeListener,
      AccessibilityManager.ITouchExplorationStateChangeListener
{
      // Nullable: an instance re-activated from a Java handle has no owning behavior.
      readonly WeakReference<TouchBehavior?>? platformTouchBehaviorReference;

      internal AccessibilityListener(TouchBehavior platformTouchBehavior)
      {
              platformTouchBehaviorReference = new(platformTouchBehavior);
      }

      // Invoked by the runtime when AccessibilityManager calls back into a listener whose
      // managed peer was already disposed (callback posted before the listener was removed).
      public AccessibilityListener(IntPtr handle, JniHandleOwnership transfer) : base(handle, transfer)
      {
      }

      public void OnAccessibilityStateChanged(bool enabled) => UpdateClickHandler();

      public void OnTouchExplorationStateChanged(bool enabled) => UpdateClickHandler();

      void UpdateClickHandler()
      {
              if (platformTouchBehaviorReference?.TryGetTarget(out var platformTouchBehavior) is true)
              {
                      platformTouchBehavior.UpdateClickHandler();
              }
      }

      protected override void Dispose(bool disposing)
      {
              if (disposing)
              {
                      platformTouchBehaviorReference?.SetTarget(null);
              }

              base.Dispose(disposing);
      }
}

and in PlatformDispose() unregister before disposing:

if (accessibilityManager is not null && accessibilityListener is not null)
{
      accessibilityManager.RemoveAccessibilityStateChangeListener(accessibilityListener);
      accessibilityManager.RemoveTouchExplorationStateChangeListener(accessibilityListener);
}
accessibilityListener?.Dispose();
accessibilityListener = null;
accessibilityManager?.Dispose();
accessibilityManager = null;

Happy to open a PR with this change if maintainers agree with the approach.

Activity

  1. rodolfo-sousa commented on Oct 26, 2026

    @rodolfo-sousa

    We are experiencing the same exception in a deployed .NET 9 MAUI application using CommunityToolkit.Maui.TouchBehavior.

    Environment

    • .NET MAUI: 9
    • CommunityToolkit.Maui: 12.1.0
    • Android: 13
    • Incident: October 24, 2026, 22:24:56–22:24:59

    Additional production observations

    1. 13 identical exceptions were captured within approximately 4 seconds, all from the same application installation.
    2. All 13 exceptions reported the same native handle: 0x7fefb1b4f8, although the key_handle was different each time.
    3. The Java callback originates from TouchBehavior_AccessibilityListener.onAccessibilityStateChanged() through Android's AccessibilityManager.
    4. At the time of the exception, our Shell navigation state was:
      • Current route: //SPLASH/MAIN
      • Navigation stack count: 2
      • Current page: MainView (Home)
    5. Our MainView.xaml contains 17 TouchBehavior declarations, primarily using DefaultScale, PressedScale, and command bindings.
    6. We retain the MainView instance during the authenticated session rather than recreating it on every visit. There is no explicit TouchBehavior.Dispose() call in its code-behind.
    7. The problem is intermittent. We have received only approximately 2–3 incidents this month, despite substantially more normal usage.

    The relevant exception is:

    System.NotSupportedException:
    Unable to activate instance of type
    CommunityToolkit.Maui.Behaviors.TouchBehavior+AccessibilityListener
    from native handle ...
    
    ---> System.MissingMethodException:
    No constructor found for
    TouchBehavior+AccessibilityListener::.ctor(
        System.IntPtr,
        Android.Runtime.JniHandleOwnership)
    

    We have not established whether TalkBack or another accessibility service was enabled on the affected device.

    The repeated callbacks with the same reported native handle may be useful when investigating the listener lifecycle.

    We prefer retaining TouchBehavior because its pressed-scale animation is an important part of our application's UI.

    We can provide a sanitized stack trace or collect additional diagnostics if helpful.

    Thank you for investigating this issue.

    1.txt

  2. DevDachi commented on Oct 31, 2026

    @DevDachi

    We see this in production too, and have a trigger that needs no TalkBack, which may help verify the fix.

    Production

    Trigger without TalkBack
    adb shell uiautomator dump connects a UiAutomation client, which flips AccessibilityManager.isEnabled() for every app, and flips it back when it disconnects. Running it right after popping a page that has about 20 TouchBehaviors crashed a Debug build on an Android 16 (API 36) emulator. It does not crash every time, which fits the snapshot-then-post race described above. So UiAutomator-based test runs and tools (Appium, uiautomator dump) can hit this as well as screen-reader users.

    +1 for the proposed fix: the activation constructor on AccessibilityListener, and unregistering in PlatformDispose().

  3. rodolfo-sousa commented on Nov 8, 2026

    @rodolfo-sousa

    We received another production crash with the same TouchBehavior.AccessibilityListener exception.

    New incident (November 8, 2026):

    • .Net 10, SDK 10.0.200,
    • CommunityToolkit.Maui 15.0.1
    • CommunityToolkit.Mvvm 8.4.2
    • Microsoft.Maui.Controls 10.0.120
    • Microsoft.Maui.Controls.Compatibility 10.0.120
    • Android 15
    • Device: INFINIX X6725
    • Exception: System.NotSupportedException, caused by System.MissingMethodException for TouchBehavior+AccessibilityListener::.ctor(IntPtr, JniHandleOwnership)

    This is the same exception reported in our October 26 comment, but on a different device, Android version and application view.

    We do not explicitly dispose TouchBehavior instances. Our pages and ViewModels use singleton lifetimes.

    This additional occurrence appears consistent with the accessibility-listener lifecycle issue described here. We cannot confirm whether an accessibility service was active or whether navigation triggered the listener detachment.

    CrashLog.txt

  4. TheCodeTraveler commented on Nov 8, 2026

    @TheCodeTraveler
    Collaborator

    Thanks! I've confirmed this in #3344.

    I'll prioritize this bug after we merge #3344 and #3331.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bugSomething isn't workingneeds discussionDiscuss it on the next Monthly standup

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions