Skip to content

Crash on navigate on app resume: The specified child already has a parent #6708

Closed

Description

@manoldonev

Environment

  • CLI: 5.1.0
  • Cross-platform modules: 5.1.0
  • Android Runtime: 5.1.0

Describe the bug
Navigating to a page when app resumes from background causes the app to crash with the following exception:

An uncaught Exception occurred on "main" thread.
java.lang.RuntimeException: Unable to resume activity {org.nativescript.bl32/com.tns.NativeScriptActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
	at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3103)
	at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3134)
	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1388)
	at android.os.Handler.dispatchMessage(Handler.java:102)
	at android.os.Looper.loop(Looper.java:148)
	at android.app.ActivityThread.main(ActivityThread.java:5417)
	at java.lang.reflect.Method.invoke(Native Method)
	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

To Reproduce

  1. Create app from blank template
  2. Add new Page second-page.xml
  3. Add the following application.resumeEvent handler to app.ts:
import * as application from "tns-core-modules/application";
import { topmost } from "tns-core-modules/ui/frame";

application.on(application.resumeEvent, (context) => {
    topmost().navigate("home/second-page");
});

application.run({ moduleName: "app-root" });
  1. Start app
  2. Suspend app
  3. Resume app -- app crashes

Originally reported by @nudiref here: #6201

Activity

  1. self-assigned this
    on Dec 14, 2018
  2. manoldonev commented on Dec 14, 2018

    @manoldonev
    ContributorAuthor

    @nudiref as a workaround you can wrap the navigate action inside a setTimeout(...) call with 0ms delay and that should allow you to achieve the desired effect:
    Instead of:

    application.on(application.resumeEvent, (context) => {
        topmost().navigate("home/second-page");
    });

    Use:

    application.on(application.resumeEvent, (context) => {
        setTimeout(() => topmost().navigate("home/second-page"), 0);
    });
  3. wagnerlino commented on Dec 15, 2018

    @wagnerlino

    Hi @manoldonev,

    In my project I have the following structure:

    Side drawer as root with an observable variable as defaultPage
    <Frame defaultPage="{{ startPage }}"></Frame>

    If my user is logged then defaultPage is set with a home page, otherwise defaultPage becomes the login page.

    For me the issue happens when I navigate from login to home page, navigates back to login and then open another app. When I back to my app, the crash happens.

    Wrapping my navigate functions inside setTimeout did not work, but I figured out that removing the observable from defaultPage and setting directly a page, defaultPage="login/login-page", the issue stops.

    Is my structure correct? Is there other way to achive the auto-login funcionality with a side drawer as root?

  4. manoldonev commented on Dec 17, 2018

    @manoldonev
    ContributorAuthor

    when I navigate from login to home page, navigates back to login and then open another app.

    Usually when navigating from login screen you would set clearHistory to true as user should not be able to return to login screen unless he/she decides to log out?

    Find attached a modified version of the drawer navigation template that implements such login/logout functionality with observable defaultPage property binding.

    bl33.zip

  5. nudiref commented on Dec 18, 2018

    @nudiref

    @nudiref as a workaround you can wrap the navigate action inside a setTimeout(...) call with 0ms delay and that should allow you to achieve the desired effect:
    Instead of:

    application.on(application.resumeEvent, (context) => {
        topmost().navigate("home/second-page");
    });

    Use:

    application.on(application.resumeEvent, (context) => {
        setTimeout(() => topmost().navigate("home/second-page"), 0);
    });

    thanks for the info and workaround Manol. i hope there will be a fix soon

  6. ghost removed on Jan 8, 2019
  7. vosonha89 commented on Jan 11, 2019

    @vosonha89

    @nudiref as a workaround you can wrap the navigate action inside a setTimeout(...) call with 0ms delay and that should allow you to achieve the desired effect:
    Instead of:

    application.on(application.resumeEvent, (context) => {
        topmost().navigate("home/second-page");
    });

    Use:

    application.on(application.resumeEvent, (context) => {
        setTimeout(() => topmost().navigate("home/second-page"), 0);
    });

    It worked for me :). Thank you so much

  8. lock commented on Jan 11, 2020

    @lock

    This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

  9. locked and limited conversation to collaborators on Jan 11, 2020
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