Repository navigation
Open
Labels
Description
Environment
- CLI: 5.2.0
- Cross-platform modules: 5.2.1
- Android Runtime: 5.2.1
Describe the bug
Related with #6312 #6201 #6339
System.err: 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:4658)
System.err: at android.view.ViewGroup.addView(ViewGroup.java:4499)
System.err: at android.view.ViewGroup.addView(ViewGroup.java:4439)
System.err: at android.view.ViewGroup.addView(ViewGroup.java:4412)
System.err: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1466)
System.err: at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
System.err: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
System.err: at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:802)
System.err: at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
System.err: at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
System.err: at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
System.err: at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
System.err: at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:733)
System.err: at android.os.Handler.handleCallback(Handler.java:751)
System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
System.err: at android.os.Looper.loop(Looper.java:154)
System.err: at android.app.ActivityThread.main(ActivityThread.java:6776)
System.err: at java.lang.reflect.Method.invoke(Native Method)
System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1518)
System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
To Reproduce
After app is opened press Back button to "close" app. Then run (resume) app again by pressing app icon.
Sample project
import * as app from "tns-core-modules/application";
import {parse} from "tns-core-modules/ui/builder";
import {StackLayout} from "tns-core-modules/ui/layouts/stack-layout";
import {Page} from "tns-core-modules/ui/page";
import {Frame} from "tns-core-modules/ui/frame";
const rootLayoutXml = `<StackLayout><Frame id="rootLayoutFrame"/></StackLayout>`;
const rootLayoutView = <StackLayout>parse(rootLayoutXml);
const pageXml = `<Page><StackLayout><Label text="Text"/></StackLayout></Page>`;
const pageView = <Page>parse(pageXml);
const rootLayoutFrame = <Frame>rootLayoutView.getViewById("rootLayoutFrame");
app.run({
create: () => {
rootLayoutFrame.navigate({
create: () => pageView
});
return rootLayoutView;
}
});
Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.
Activity
[-]The specified child already has a parent. You must call removeView() on the child's parent first.[/-][+]The specified child already has a parent (when dynamically creating Frame & Page)[/+]NickIliev commentedon Feb 20, 2019
@webleaf move the page creation inside the inner
createIn that line of thoughts, you should probably also do the same for your
Frame(not tested on my side but do it to avoid similar issue when navigating)webleaf commentedon Feb 20, 2019
@NickIliev I don't like idea to parse
Viewon every navigate. That's why I use "singleton" (once) parsed Views. Also that's why I don't use xml view's. Looks like NS parse XML on every navigate. It's overhead and works slow.Sample project was done to reproduce the issue. Of course my project is more complex. And there was not such issue until 5.2.0
And I can't rewrite all as you recommend, first of all because it was done to avoid parsing xml on every navigate.
webleaf commentedon Feb 20, 2019
@NickIliev I found some workaround, but I think it does not solve the issue. Workaround looks so:
Now we can suspend app via
Backbutton and when we reopen it again error don't appear.But it produce other issue. After such app reopening
topmost()call become returnundefined, so such error could be produced:Other question is Why after suspending app via
Backbutton and reopening it againlaunchEventraises beforeresumeEvent? On thislaunchEventcreate: () => View(ofapp.run()) function is called and it is one of the reason of this issue. OnlyresumeEventshould raise in such case.To illustrate described problems I've created this snippet (just copy/past to your
app.ts):@manoldonev please, could you check this?
NickIliev commentedon Feb 22, 2019
@webleaf regarding Why after suspending app via
Backbutton and reopening it againlaunchEventraises before `resumeEventNativeScript is not chaning the way the activity lifecycles are working. You can test it with the native ActivityLifecycles and observe that again the 'onResume' event is triggered after
onCreateandonStart- refer to the diagram in this documentation article. Here is a Playground app that demonstrates both the NativeScript lifecycle events and the native Android activity lifecycle events.Logs from the Playgrond app (downloaded locally so we could observe the initial start events)
Hitting the hardware back (with no navigation stack in your current frame) will exit the app thus triggering the
suspendevent. then on returning to the app thelaunchevent will be fired and finally theresumeevent.Regarding the workaround with 'currentPage' and the navigation error.
You need to recreate the NativeScript entries (Page, Frame, etc) and this is the way NativeScript works.
In your workaround you are guarding the first error but that won't do the job as the Frame (which you are using for the safety check) again needs to be recreated so it could have all the events, lifecycles, etc. to be reattached. The error
Cannot read property 'navigate' of undefinedshows that the frame reference is no longer valid as it was not recreated during the application start.Parsing the XML is not a big overhead - even in pretty complex UI we are talking about milliseconds and again this is the way NativeSript works when you have are creating XML files for your UI (with XML files or through XML strings and
ui/builder).webleaf commentedon Feb 22, 2019
But it works. Since I already have a reference to the
Framecreated on the first app start (rootLayoutFrame). So if I dorootLayoutFrame.navigate()it works and I don't need recreate it.Just
topmost()don't work in such case (it's not big problem, but maybe this indicates thattopmost()don't work correctly).NavigationEntrycreate: () => {}function should returnView. It happens (no matter when it was created). Everything else is a sign of NS architectural problems in this place. But you are trying to convince me that it should be so.NickIliev commentedon Feb 22, 2019
@webleaf Can you let me know if you are experiencing the same issue if you try to access the frame via
getFrameByIdinstead withtopmostwebleaf commentedon Feb 22, 2019
@NickIliev
getFrameByIddon't work too in such case.itsmerockingagain commentedon Apr 26, 2021
I am also facing same issue, is there any workaround for this issue?