title: Lifecycle of a Layout (or Component) in Vaadin?
body: <p>What hooks do I have into the lifecycle of a Layout specifically, or Component generally in Vaadin 7?</p>
  
  <h1>Lifecycle Events?</h1>
  
  <p>I want to know when a Layout is starting (being born) and stopping (dying). Specifically, I suspect I really care about "attach" and "detach". The Layout could exist off screen before use on-screen and even afterwards if I might re-display that Layout later in the user’s work session. I think attachment is that process of joining/leaving the displayed UI, but I’m not sure.</p>
  
  <h1>Motivation: Notification, in Observer Pattern</h1>
  
  <p>Not that it matters, but the reason I care about the lifecycle is shown in the <a href="https://vaadin.com/book/vaadin7/-/page/advanced.push.html" rel="nofollow">Push page</a> of the <a href="https://vaadin.com/book" rel="nofollow">Book of Vaadin 7</a>. As seen there, I want to notify a bunch of users’ Layouts (in UIs) about a change in the data represented by their widgets. So each Layout needs to have a method invoked, to make them update their widgets with fresh data.</p>
  
  <p>To notify each Layout, I need to know which Layout instances exist and are interested in this kind of update. So I keep a collection of Layouts that register themselves. Basically, the <a href="http://en.wikipedia.org/wiki/Observer_pattern" rel="nofollow">Observer pattern</a>. That Push page shows such a registry collection. </p>
  
  <p>The missing piece: Where in the Layout's code do I make the Layout instance register and unregister? I suppose I could the Java constructor and <code>finalize</code> method. But I suspect I really want registration to revolve around the attach and detach events.</p>
  
  <h1>Attach/Detach Events</h1>
  
  <p>The <a href="https://vaadin.com/api/com/vaadin/ui/Layout.html" rel="nofollow"><code>Layout</code></a> doc lists many attach/detach listeners and <code>attach</code>/<code>detach</code> methods. I'm not sure which I should be using, if any.</p>