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
11 changes: 10 additions & 1 deletion android/src/legacy/java/com/rive/RiveReactNativeView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ class RiveReactNativeView(context: ThemedReactContext) : FrameLayout(context) {
if (willDispose) {
riveAnimationView?.dispose()
removeEventListeners()
// Post-dispose reads must resolve null, not the retained (released)
// instance; also stops pinning it for the view's remaining lifetime.
lastKnownViewModelInstance = null
}
super.onDetachedFromWindow()
}
Expand Down Expand Up @@ -181,7 +184,13 @@ class RiveReactNativeView(context: ThemedReactContext) : FrameLayout(context) {
@Volatile
private var lastKnownViewModelInstance: ViewModelInstance? = null

private val mainHandler = android.os.Handler(android.os.Looper.getMainLooper())

private fun readViewModelInstanceOnMain(): ViewModelInstance? {
// A refresh posted before dispose() can run after it; reading the
// torn-down controller's stale list here would re-cache a released
// instance right after onDetachedFromWindow cleared it.
if (willDispose) return null
val stateMachines = riveAnimationView?.controller?.stateMachines
return if (!stateMachines.isNullOrEmpty()) {
stateMachines.first().viewModelInstance
Expand All @@ -195,7 +204,7 @@ class RiveReactNativeView(context: ThemedReactContext) : FrameLayout(context) {
lastKnownViewModelInstance = readViewModelInstanceOnMain()
return lastKnownViewModelInstance
}
android.os.Handler(android.os.Looper.getMainLooper()).post {
mainHandler.post {
lastKnownViewModelInstance = readViewModelInstanceOnMain()
}
return lastKnownViewModelInstance
Expand Down
Loading