Skip to content

Crash on app resume: The specified child already has a parent. #6201

Closed

Description

@abhayastudios

Not sure if related to #6199 (not using tabs), but I am having a serious issue where the app crashes after it comes back from being in the background: The specified child already has a parent. You must call removeView() on the child's parent first.

The full exception is below.

System.err: java.lang.RuntimeException: Unable to resume activity {com.abhayastudios.myapp/com.tns.NativeScriptActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
System.err:   at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3788)
System.err:   at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3828)
System.err:   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1746)
System.err:   at android.os.Handler.dispatchMessage(Handler.java:105)
System.err:   at android.os.Looper.loop(Looper.java:164)
System.err:   at android.app.ActivityThread.main(ActivityThread.java:6938)
System.err:   at java.lang.reflect.Method.invoke(Native Method)
System.err:   at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
System.err:   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
System.err: Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
System.err:   at android.view.ViewGroup.addViewInner(ViewGroup.java:5122)
System.err:   at android.view.ViewGroup.addView(ViewGroup.java:4953)
System.err:   at android.view.ViewGroup.addView(ViewGroup.java:4893)
System.err:   at android.view.ViewGroup.addView(ViewGroup.java:4866)
System.err:   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1425)
System.err:   at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1750)
System.err:   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1819)
System.err:   at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:797)
System.err:   at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2590)
System.err:   at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2377)
System.err:   at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2332)
System.err:   at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2239)
System.err:   at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:379)
System.err:   at android.support.v4.app.FragmentActivity.onResume(FragmentActivity.java:461)
System.err:   at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1361)
System.err:   at android.app.Activity.performResume(Activity.java:7353)
System.err:   at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3763)
System.err:   ... 8 more

Which platform(s) does your issue occur on?

Android

Please provide the following version numbers that your issue occurs with:

  • CLI: 4.2
  • Cross-platform modules: 4.3.0-2018-08-17-04
  • Runtime(s): 4.2
  • Plugin(s): (look for the version number in the package.json file of your
    project)

Please tell us how to recreate the issue in as much detail as possible.

Is there code involved? If so, please share the minimal amount of code needed to recreate the problem.

Activity

  1. tsonevn commented on Aug 21, 2018

    @tsonevn
    Contributor

    HI @abhayastudios,
    Can you provide sample project or at least some code snippets, that can help in recreating the issue on our side?

  2. abhayastudios commented on Aug 21, 2018

    @abhayastudios
    ContributorAuthor

    @tsonevn sorry for not proving a sample earlier, I was still trying to isolate the problem to try to find out during which commit it started to happen and I thought maybe the stack trace would be somehow meaningful to you.

    Anyway, in the meantime I was able to isolate the issue to tns-core-modules@next (currently 4.3.0-2018-08-17-04). I will update here later today once I have minimal sample showing the issue.

  3. changed the title [-]NS 4.2 crash on app resume: The specified child already has a parent.[/-] [+]Crash on app resume: The specified child already has a parent.[/+] on Aug 21, 2018
  4. abhayastudios commented on Aug 22, 2018

    @abhayastudios
    ContributorAuthor

    @tsonevn it took me a couple of hours to take my "big app" and do elimination until I found the issue, but I was able to reproduce it in an almost clean demo project.

    If you run it you will get to a page with a button and if you suspend the app and return, you will get a white page and after a while you will get the error above in this issue (if you run tns debug at least). As I wrote above, the issue only occurs on Android and seems to be in tns-core-modules@next whereas with tns-core-modules@4.2 it is working fine.

    It seems that the problem is with nesting of the page-router-outlet. If you look at the app.routing.ts it will now look like below where PageRouterComponent contains a page-router-outlet (so nested in the one from AppComponent):

    const routes:Routes = [
      { path: "", component: PageRouterComponent,
        children: [
          { path: "", redirectTo: "dashboard", pathMatch: "full" },
          { path: "dashboard", component: DashboardPage },          
        ]
      }
    ];
    

    If I comment out the PageRouterComponent like below, then the app will behave correctly even when getting back from suspend:

    const routes:Routes = [
    //  { path: "", component: PageRouterComponent,
    //    children: [
          { path: "", redirectTo: "dashboard", pathMatch: "full" },
          { path: "dashboard", component: DashboardPage },          
    //    ]
    //  }
    ];
    

    You may ask why I need this nested page-router-outlet on top of the one from the AppComponent, but in my real app the routing is of course more complex and I am first landing in a login page and am then navigating away to the PageRouterComponent which has a nested component with a router-outlet that I don't ever want to return in this session.

    I know there were some changes in NS 4.0/4.1 around frames/routing, but like I said up until 4.2 it was working fine. Let me know if I need to explain more or if the issue I am seeing here is new expected behavior for 4.3 and I should change my routing.

    Thanks!

  5. tsonevn commented on Aug 23, 2018

    @tsonevn
    Contributor

    Hi @abhayastudios,
    Thank you for the provided project.
    I tested it on my side, however, was unable to recreate the problem. I suspended the app and after resuming everything seems to work as expected. Are there any specific steps, which should be followed while resuming the app.

  6. abhayastudios commented on Aug 23, 2018

    @abhayastudios
    ContributorAuthor

    Hi @tsonevn no nothing special. I tried again now by removing the old project, downloading from my github and building from scratch. I then open the app, I will see the button, suspend the app (I put my phone in sleep mode) then when I return to the app it immediately crashes and shows a blank screen.

    Trying to think what else may influence the build... The version of tns I am using is 4.2.2.

  7. abhayastudios commented on Aug 28, 2018

    @abhayastudios
    ContributorAuthor

    @tsonevn still happening with the latest tns-core-modules@4.3.0-2018-08-28-01. Any advice? Were you still not able to reproduce? Can you maybe ask someone else to try to reproduce?

  8. NickIliev commented on Aug 30, 2018

    @NickIliev
    Contributor

    @abhayastudios In the demo project you are bootstrapping the AppComponent which has a page-router-outlet which navigates in ngOnInit to dashboard and then you are using page-router.component which has again a page-router-outlet which is using the very same app routing table ... why this structure and simply using the main page-router-outlet from AppComponent?

  9. NickIliev commented on Aug 30, 2018

    @NickIliev
    Contributor

    Nested page-router-outlet will be supported via this feature - closing as related to the linked feature

  10. abhayastudios commented on Aug 30, 2018

    @abhayastudios
    ContributorAuthor

    @NickIliev yeah like I wrote above my real app is more complex that the demo project and I needed this nested page-router-oulet to make it work (before 4.1) , but I had another go at it yesterday and now it seems that in 4.2/4.3 at least I don't need the nested page-router-oulet anymore (probably since changes in 4.1) so my issue now is solved.

    However, I think that the app should still not crash when this does occur, so it's good that supporting nested outlets is in the works.

    Thanks!

  11. self-assigned this
    on Oct 1, 2018
  12. ghost removed on Oct 2, 2018
  13. cerireyhan commented on Nov 29, 2018

    @cerireyhan

    I still have this bug. The app crashes when I open the app from background notification and try to navigate. It can be fixed by navigating inside timeout(0 seconds is enough...) but its just an ugly workaround.

  14. manoldonev commented on Nov 29, 2018

    @manoldonev
    Contributor

    @cerireyhan can you confirm you are using the latest version of tns-core-modules@5.0.5?

  15. cerireyhan commented on Nov 29, 2018

    @cerireyhan

    @manoldonev Yes, I'm using tns-core-modules 5.0.5, nativescript-angular: 7.0.2 and tns-android: 5.0.0

  16. manoldonev commented on Nov 30, 2018

    @manoldonev
    Contributor

    @cerireyhan can you open a new issue for your specific use case (playground project or steps to reproduce would be useful) so we can investigate and address the problem?

  17. nudiref commented on Dec 13, 2018

    @nudiref

    ​i have this bug after upgrading to NativeScript 5 as well. was anyone able to solve it?

    here is my configuration:

    nativescript: 5.1.0
    tns-core-modules: next ("^5.2.0-next-2018-12-13-170345-02")
    tns-android: next ("5.2.0-2018-12-13-170001-01")

    scenario:

    navigate to a page when app resumes from background.
    following code in app.js produces the problem:

    var frameModule = require('ui/frame');
    application.on(application.resumeEvent, function (context) {
    frameModule.topmost().navigate('./views/home/home');
    });

    error:
    An uncaught Exception occurred on "main" thread.
    java.lang.RuntimeException: Unable to resume activity {com.A2ZFieldServices.FieldCompass/com.tns.NativeScriptActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

  18. manoldonev commented on Dec 14, 2018

    @manoldonev
    Contributor

    @nudiref I was able to reproduce the problem you are describing and logged it as a new issue #6708 -- we will continue the discussion / investigation there.

  19. manoldonev commented on Dec 14, 2018

    @manoldonev
    Contributor

    Locking this conversation as the original problem is now addressed. Should you experience a new manifestation of the same exception, please open a separate issue with steps to reproduce / sample project / Playground project.

    Thanks!

  20. locked as resolved and limited conversation to collaborators on Dec 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions