fix(android): prevent crash from double release during navigation#46
Merged
Conversation
The Rive Android SDK's RiveViewLifecycleObserver releases dependencies on fragment lifecycle onDestroy. During React Navigation screen transitions, fragments are destroyed before views are actually disposed, causing release() to be called when refs are already at 0. Crash: java.lang.IllegalArgumentException: Failed requirement. at app.rive.runtime.kotlin.controllers.RiveFileController.release() Fix ports the proven willDispose pattern from rive-react-native: - ReactNativeRiveViewLifecycleObserver: overrides onDestroy to skip auto-release, adds explicit dispose() method - ReactNativeRiveAnimationView: custom RiveAnimationView that uses our lifecycle observer - RiveReactNativeView: adds willDispose flag, only cleans up in onDetachedFromWindow when flag is set - RiveViewManager: extends HybridRiveViewManager to call dispose() in onDropViewInstance (when React Native removes the view) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
HayesGordon
approved these changes
Nov 27, 2025
Contributor
HayesGordon
left a comment
There was a problem hiding this comment.
LGTM! Hopefully we can avoid this complexity in the new Android runtime
mfazekas
added a commit
that referenced
this pull request
Mar 13, 2026
Upgrades nitrogen CLI and nitro-modules runtime to 0.35.0. Fixes `ArrayBufferHolder` → `ArrayBuffer` rename and regenerates all nitrogen bindings. Nitro 0.35 made generated ViewManagers `final` ([nitro#1181](mrousavy/nitro#1181)), which broke our `RiveViewManager.onDropViewInstance()` override needed for proper view lifecycle cleanup ([#46](#46)). Added a nitrogen postprocess script that patches `HybridRiveViewManager` back to `open` so we can subclass it again. Without `onDropViewInstance`, `onDetachedFromWindow` disposes Rive resources on every navigation (not just permanent removal), causing state loss. GC-based cleanup was tested and confirmed unreliable — neither Hermes GC nor JVM GC triggers dispose. ## Added test for navigation lifecycle on onDetachedFromWindow: Verified that it's broken without the workaround ```diff override fun onDetachedFromWindow() { - if (willDispose) { + if (true || willDispose) { riveAnimationView?.dispose() removeEventListeners() } ``` <img width="330" height="119" alt="image" src="https://github.com/user-attachments/assets/3c635403-7a89-45bf-84c0-6592c2d234a6" /> <details> <summary>Reproducer: NavigationLifecycle.tsx</summary> Uses [counter.riv](https://rive.app/community/files/26808-50366-counter) with a tap counter. Navigate to QuickStart and back; without the fix the counter resets to 0. ```tsx import { StyleSheet, Text, View, TouchableOpacity } from 'react-native'; import { useRouter } from 'expo-router'; import { Fit, useRiveFile, RiveView } from '@rive-app/react-native'; import { type Metadata } from '../../shared/metadata'; function RiveGraphic() { const { riveFile, isLoading, error } = useRiveFile( require('../../../assets/rive/counter.riv') ); if (isLoading) { return ( <View style={styles.loadingContainer}> <Text>Loading...</Text> </View> ); } if (error || !riveFile) { return ( <View style={styles.errorContainer}> <Text>Error: {error ?? 'No file loaded'}</Text> </View> ); } return <RiveView file={riveFile} fit={Fit.Contain} style={styles.rive} />; } export default function NavigationLifecycle() { const router = useRouter(); return ( <View style={styles.container}> <Text style={styles.title}>Navigation Lifecycle Test</Text> <Text style={styles.description}> This screen has a Rive view. Navigate to another Rive screen and back to test lifecycle handling. </Text> <Text style={styles.steps}> 1. This screen shows Rive graphics{'\n'} 2. Tap button to go to QuickStart (another Rive screen){'\n'} 3. Press Back to return here{'\n'} {'\n'} If the fix works, no crash should occur. </Text> <RiveGraphic /> <TouchableOpacity style={styles.button} onPress={() => router.push('/QuickStart' as any)} > <Text style={styles.buttonText}>Go to QuickStart (has Rive)</Text> </TouchableOpacity> </View> ); } NavigationLifecycle.metadata = { name: 'Navigation Lifecycle', description: 'Tests Android lifecycle handling during navigation (PR #46)', } satisfies Metadata; const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#fff', padding: 16 }, title: { fontSize: 24, fontWeight: 'bold', marginBottom: 16, textAlign: 'center' }, description: { fontSize: 16, color: '#666', marginBottom: 12 }, steps: { fontSize: 14, color: '#333', backgroundColor: '#f5f5f5', padding: 16, borderRadius: 8, marginBottom: 16, lineHeight: 22 }, button: { backgroundColor: '#6c5ce7', padding: 16, borderRadius: 8, alignItems: 'center', marginTop: 16 }, buttonText: { color: '#fff', fontSize: 16, fontWeight: 'bold' }, rive: { width: '100%', height: 200, backgroundColor: '#f0f0f0' }, loadingContainer: { width: '100%', height: 200, justifyContent: 'center', alignItems: 'center', backgroundColor: '#f0f0f0' }, errorContainer: { width: '100%', height: 200, justifyContent: 'center', alignItems: 'center', backgroundColor: '#ffebee' }, }); ``` </details> BREAKING CHANGE: minimum nitro-modules version is now 0.35.0 Closes: #169
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The Rive Android SDK's RiveViewLifecycleObserver releases dependencies on fragment lifecycle onDestroy. During React Navigation screen transitions, fragments are destroyed before views are actually disposed, causing release() to be called when refs are already at 0.
Crash:
java.lang.IllegalArgumentException: Failed requirement. at app.rive.runtime.kotlin.controllers.RiveFileController.release()
Fix ports the proven willDispose pattern from rive-react-native:
Reproducer:
Fixes crash log: