In a view with a form where to initialize the Binder for the form ? Constructor or on onAttach(..) ?

Like written in the title, what is the recommend way to initialize the Binder ?


Usually you don’t need to initialize the binder everytime you are navigating to the view, so the constructor or the postconstruct method is a good place to initialize the binder. The afternavigation is a good place to read/set the bean

I am initializing the binder in the constructor but setting it in onAttach, I never considered afternavigation. It’s not clear what is the role of onAttach vs afternavigation

I generally use onattach when you have a on Detach. For a binder in a view, I’m using afternavigation to check the permission, read the data and set the bean.

OnAttach is called when you add/remove the component in the view, that doesn’t sound like the good place to bind your data.

the same if you have to initialize data in a grid ?

Same. If you hide/show the grid you might want to get the data only when it’s visible and refresh it if you redisplay the grid.

sorry if I insist but it’s still not clear to me onAttach vs afterNavigtion, when to use one or the other. For example on onAttach doc is written: If the content of a component depends on resources that are not available during the construction of the component, you can postpone content creation until the component attaches to the UI, by overriding the onAttach() method (provided by the Component class).

So you can update components on onAttach

the classic example is that you get the session user in onAttach and update a component according to it

a grid data, for example. You can do the same on afterNavigation for sure

Personal rule of thumb: only use onAttach if you wanna be notified about the detach as well - use before or after navigation If you just wanna update something on view change

I don’t like onAttach because it will be called if you remove them add a component in a layout, for a tab it’s not what I want. It’s also not in the navigation livecycle. So I don’t know the order when there is setParameter, afternavigation, beforenavigation and onAttach.