Open tsonevn opened 6 years ago
Hi @prolink007, Thank you for reporting this case, I review this scenario and all events seem to be fired in the same order for both iOS and Android except the loadedEvent. Firing the loaded event is something, which depends on the specifics of the platform. On iOS it will be fired at the beginning, however, on Android, the native component will be created and added to the visual tree a little bit later. This difference caused this behavior with the loaded event.
In case you need to an event which is fired at the same time, you could use ngOnInit
orngAfterViewInit
From @NathanaelA on June 16, 2017 17:39
@tsonevn - Shouldn't this be marked as a bug and fixed? That is why we have a framework to create consistencies between the platforms.
ngOnInit
or 'ngAfterViewInit` in PAN. From @prolink007 on June 16, 2017 19:47
@tsonevn Thank you for the information. However, iOS firing the loaded
event after ngAfterViewInit
seems like a bug to me. ngAfterViewInit
is defined as "initialized the component's views and child views". And it is not. This seems like a bug to me and makes ngAfterViewInit
unreliable.
From @vakrilov on June 20, 2017 9:0
I agree that event firing in a different order is a great source of confusion so I will try to clarify.
The angular hooks are related to the angular views and are guaranteed to execute in a particular order. This is order is the same for NS and Web projects. The note "initialized the component's views and child views", however views in this context are actually the angular components and views. So, it is safe to rely on ngAfterViewInit
to resolve @ViewChildren
queries.
The navigatingTo
, loaded
, navigatedTo
events are part of the NativeScript views lifecycle and are also guaranteed to execute in this order for each page. The recommendation is to access the action nativeElement (ex. UIButton
or android.widget.Button
) in the loaded
event when it is available.
I see two problems here. Let's try to define them and decide on how best to approach this issue.
This is because the lifecycle of the native views is different for Android and IOS. In IOS native views can be created any time, but in Android you need the activity context(which is available only when the view is added to its parent) to call a nativeView's constructor.
The rule the thumb here is - "if you want to access the nativeViews - use loaded
. If you want to access angular components or ViewChildren - use the angular hooks.".
I'm guessing that the real problem is - what happens when I need both ViewChildren and native views in my app in a same method? Is there something I'm missing?
onNavigatedTo
is not fired in IOSProbably the event is fired before you attach to it in the component constructor. This happens only for the startup component. Again - can you elaborate that is the scenario, and decide if these events are the most appropriate for it.
From @NathanaelA on June 20, 2017 9:14
@vakrilov - I can't really comment on the Angular per-sey, but onNavigatedTo
should always fire after loaded
event, as the NavigatedTo
event is the last event you can hook into in a PAN app for when you need to have the entire dom tree created to do anything with... So if this is firing before the loaded
event that is a major regression from v2.... The event order should be Navigating
, Loaded
, Navigated
...
From @speigg on January 5, 2018 4:22
I was expecting the new createNativeView
function to be called before the "navigatedTo" event on iOS, but I guess "navigatedTo" is supposed to happen before createNativeView
(on iOS)
From @prolink007 on June 15, 2017 18:37
It seems the lifecycle states are not fired in the same order on android and ios. And also do not appear to be in a logical order.
For example:
Android fire order:
iOS fire order:
iOS does not seem to get
onNavigatedTo
.I would also assume that
onPageLoaded
should never be called beforengAfterViewInit
. My impression ofngAfterViewInit
is the contents of the page is fully loaded and ready. SoonPageLoaded
should be called at the same time asngAfterViewInit
.Keep in mind that my
onPageLoaded
is on the top element of the template. The name seems a little confusing. I just named it that for testing. What we are really talking about here is theloaded
callback for elements in the template.Copied from original issue: NativeScript/NativeScript#4398