[v2] Tried to create view after it has already been destroyed
My app users are complaining about random crashes on Android. I have Crashlytics installed and I verified by using user and time filters that this crashes are caused by the following error:

I can't reproduce the problem in the Android simulator and it seems Android exclusive, as it appears. Can anyone help me what might be happening? Thanks in advance.
Environment
- React Native Navigation version: 2.0.2377
- React Native version: 0.55.4
- Platform(s) (iOS, Android, or both?): Android
- Device info (Simulator/Device? OS version? Debug/Release?): Only device, apparently
same
somebody pls remember me to show you how to reproduce, i know how, very busy right now, might forget
See the same crash in Android only. In my case, it happens when a child screen is popping (the pop animation is running) when the setRoot() method is called. If I set the pop animation's duration to 0, it works without any problem. So I think the pop animation is the culprit here.
Hope this information can help you resolve the issue.
Unfortunately I can't fix this one without a reproduction, @nezaidu ?
getting the same thing but no consistent way of reproducing this.
@starzle Have you tried awaiting the pop command so root is set after pop completes?
@guyca In our case, the setRoot is called when componentDidAppear is triggered. And apparently, componentDidAppear is triggered before the pop animation ends. So we don't have a place where we can await the pop.
But if we simply add a delay to the setRoot call, then as long as the delay is long enough to make sure pop animation is already finish (onAnimationEnd called if I am correct) when setRoot is called, then the crash goes away. In short, pop's onAnimationEnd needs to be called before setRoot.

Hope this info can help you.
after add code push relative, code push update restart app, android app will crash, logcat console out put this log
java.lang.RuntimeException: Tried to create view after it has already been destroyed
i have same issue
componentDidMount(){
this._getDeviceToken();
FCM.getInitialNotification().then(notif => {
console.log(notif,'notif tapped')
if (notif.gotoActivity==="dashboard") {
startMainTab(2);
return;
}
else if (notif.gotoActivity==="activity") {
startMainTab(0);
return;
}
});
FCM.on(FCMEvent.Notification, this.handleNotification);
this.tokenToState();
}
this also occurs when you have a root view, navigate to a new view with pushTo from a modal and then try to setStackRoot within the main application.
To add to my colleague @reganperkins above, our error is the same but the stacktrace does not seem entirely identical.
09-19 14:13:26.303 5362-5362/co.elk.rn D/ERROR: java.lang.RuntimeException: Tried to create view after it has already been destroyed
at com.reactnativenavigation.viewcontrollers.ViewController.getView(ViewController.java:153)
at com.reactnativenavigation.viewcontrollers.ViewController.lambda$runOnPreDraw$2$ViewController(ViewController.java:266)
at com.reactnativenavigation.viewcontrollers.ViewController$$Lambda$2.run(Unknown Source)
at com.reactnativenavigation.utils.UiUtils$1.onPreDraw(UiUtils.java:27)
at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:944)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2205)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6337)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
at android.view.Choreographer.doCallbacks(Choreographer.java:686)
at android.view.Choreographer.doFrame(Choreographer.java:621)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
We managed to find a workaround that may (or may not!) clarify the situation. As she was saying, the control flow in our scenario is this:
- Start on a root view.
- Open a modal (as it happens, to create a new item).
- From the modal, navigate via
pushTo()to a new view (the new item). - From the new item, navigate via
setStackRoot()to the root component. (Of course, in this specific scenario, we could pop, but the code is more generic.)
The workaround: Close the modal before pushing the new view, deferring the push using setImmediate(). Now, for some reason, it no longer crashes. Perhaps the open modal does something strange to the stack?
I can confirm this happening as well. In my case I'm calling a setRoot on a login screen to bump an already logged in user into the app.
Heres the stack from the crash, only affects android devices.
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.lawnlove.provider, PID: 17401
java.lang.RuntimeException: Tried to create view after it has already been destroyed
at com.reactnativenavigation.viewcontrollers.ViewController.getView(ViewController.java:152)
at com.reactnativenavigation.viewcontrollers.ParentController.getView(ParentController.java:63)
at com.reactnativenavigation.viewcontrollers.stack.StackController.lambda$push$4$StackController(StackController.java:157)
at com.reactnativenavigation.viewcontrollers.stack.StackController$$Lambda$4.run(Unknown Source:0)
at com.reactnativenavigation.anim.NavigationAnimator$1.onAnimationEnd(NavigationAnimator.java:64)
at android.animation.Animator$AnimatorListener.onAnimationEnd(Animator.java:552)
at android.animation.AnimatorSet.endAnimation(AnimatorSet.java:1293)
at android.animation.AnimatorSet.doAnimationFrame(AnimatorSet.java:1079)
at android.animation.AnimationHandler.doAnimationFrame(AnimationHandler.java:146)
at android.animation.AnimationHandler.-wrap2(Unknown Source:0)
at android.animation.AnimationHandler$1.doFrame(AnimationHandler.java:54)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:909)
at android.view.Choreographer.doCallbacks(Choreographer.java:723)
at android.view.Choreographer.doFrame(Choreographer.java:655)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
E/UncaughtException: java.lang.RuntimeException: Tried to create view after it has already been destroyed
at com.reactnativenavigation.viewcontrollers.ViewController.getView(ViewController.java:152)
at com.reactnativenavigation.viewcontrollers.ParentController.getView(ParentController.java:63)
at com.reactnativenavigation.viewcontrollers.stack.StackController.lambda$push$4$StackController(StackController.java:157)
at com.reactnativenavigation.viewcontrollers.stack.StackController$$Lambda$4.run(Unknown Source:0)
at com.reactnativenavigation.anim.NavigationAnimator$1.onAnimationEnd(NavigationAnimator.java:64)
at android.animation.Animator$AnimatorListener.onAnimationEnd(Animator.java:552)
at android.animation.AnimatorSet.endAnimation(AnimatorSet.java:1293)
at android.animation.AnimatorSet.doAnimationFrame(AnimatorSet.java:1079)
at android.animation.AnimationHandler.doAnimationFrame(AnimationHandler.java:146)
at android.animation.AnimationHandler.-wrap2(Unknown Source:0)
at android.animation.AnimationHandler$1.doFrame(AnimationHandler.java:54)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:909)
at android.view.Choreographer.doCallbacks(Choreographer.java:723)
at android.view.Choreographer.doFrame(Choreographer.java:655)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Any update on this @guyca ?
Wrapping the setStackroot calls with InteractionManager.runAfterInteractions, solved this for me.
@makirby can you please share a snippet for the solution?
Basically where we were making a setStackRoot call soon after another push in a modal stack, I would do something like this.
InteractionManager.runAfterInteractions(() => {
Navigation.setStackRoot(STACK_IDS.main, {
component: { name: SCREEN_MAP.Home },
})
})
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you believe the issue is still relevant, please test on the latest Detox and report back. Thank you for your contributions.
probably not stale
I also get this error randomly when existing the app via back button press and then re-entering the app
makirby's solution is not working in my case...