0

I'm experiencing crashes in my Compose-based app when cleaning up AndroidView components that contain third-party ad views. The crash occurs during view detachment:

Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.dispatchDetachedFromWindow()' on a null object reference at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:4219) at android.view.ViewGroup.removeAllViewsInLayout(ViewGroup.java:6108) at androidx.compose.ui.viewinterop.AndroidViewHolder.onDeactivate(AndroidViewHolder.android.kt:222) at androidx.compose.ui.node.LayoutNode.onDeactivate(LayoutNode.kt:1350) at androidx.compose.runtime.CompositionImpl$RememberEventDispatcher.dispatchRememberObservers(Composition.kt:1283) at androidx.compose.runtime.CompositionImpl.applyChangesInLocked(Composition.kt:984) at androidx.compose.runtime.CompositionImpl.applyChanges(Composition.kt:1005) at androidx.compose.runtime.Recomposer$runRecomposeAndApplyChanges$2$1.invoke(Recomposer.kt:639) at androidx.compose.runtime.Recomposer$runRecomposeAndApplyChanges$2$1.invoke(Recomposer.kt:551) at androidx.compose.ui.platform.AndroidUiFrameClock$withFrameNanos$2$callback$1.doFrame(AndroidUiFrameClock.android.kt:41) at androidx.compose.ui.platform.AndroidUiDispatcher.performFrameDispatch(AndroidUiDispatcher.android.kt:109) at androidx.compose.ui.platform.AndroidUiDispatcher.access$performFrameDispatch(AndroidUiDispatcher.android.kt:41) at androidx.compose.ui.platform.AndroidUiDispatcher$dispatchCallback$1.doFrame(AndroidUiDispatcher.android.kt:69) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1688) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1699) at android.view.Choreographer.doCallbacks(Choreographer.java:1154) at android.view.Choreographer.doFrame(Choreographer.java:1070) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1647) at android.os.Handler.handleCallback(Handler.java:958) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loopOnce(Looper.java:230) at android.os.Looper.loop(Looper.java:319) at android.app.ActivityThread.main(ActivityThread.java:9063) at java.lang.reflect.Method.invoke(Method.java) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:588) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1103)

The crash tends to occur when rapidly navigating between screens or when switching between tabs in a HorizontalPager that contains third-party ad views (integrated via AndroidView). Unfortunately, the stack trace doesn’t provide enough detail, and I can’t reproduce the crash on my own devices or Genymotion emulators.

How should I properly clean up AndroidView components containing third-party ad views in Compose? How can I ensure a proper view detachment when navigating or switching pages, so I avoid this NullPointerException in dispatchDetachedFromWindow()? Is there a known lifecycle or race condition related to AndroidView that might trigger this crash?

4
  • What are you currently doing when a user navigates away from a page that contains ads? Commented Jan 10 at 14:37
  • We have a news app that integrates multiple ad providers (Nativo, Taboola, PubMatic) using AndroidView within Jetpack Compose. While navigating between pages/fragments, we’re seeing occasional crashes related to ad view cleanup—but we can’t reproduce these crashes on any of our test devices. Current flow: HomeFragment has a HorizontalPager showing: NewsScreen (Nativo, PubMatic) FeedsScreen (Nativo, PubMatic) Article Details: We navigate to a ViewPager2, which creates/destroys fragments as needed (using an adapter). Commented Jan 10 at 17:50
  • Each fragment is a NewsDetailsComposeFragment that shows one article. As the user swipes between articles, ViewPager2 manages each fragment’s lifecycle (creation & destruction). We suspect the crashes might be happening when fragments or composables are destroyed (for example, when a user swipes quickly between articles) and the ad views don’t get properly cleaned up. However, we haven’t been able to reproduce it on our own devices. Commented Jan 10 at 17:50
  • Please share the relevant logic in code. Commented Jan 11 at 6:35

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.