title: Eclipse call ViewPart saveState on View close
body: <p>I have a Eclipse plugin that uses a view which extends <code>ViewPart</code>. <code>ViewPart</code> has a <code>saveState</code> method which requires an <code>IMemento</code>.</p>
  
  <p>I added my code to <code>saveState</code> and the corresponding <code>init</code> method and it works. Unfortunately, <code>saveState</code> is only called if the entire workspace is shutting down. My view is not of such great importance that I can expect it to be opened the entire time. Hence, it would be cool if <code>saveState</code> would be called on view closure.</p>
  
  <p>I found a view-part listener as mean to react on view closure, but what I do not get is where the <code>IMemento</code> comes from. Where do I get the memento object that is used on workspace closure? Or where do I have to store my own memento object to make the view part use it in the <code>init</code> method if the view is (re)opened?</p>
  
  <pre><code>@Override
  public void saveState(IMemento memento) {
      super.saveState(memento);
      memento = memento.createChild(MEMENTO_GUI_STATE);
      memento.putBoolean(MEMENTO_IS_FLAT, !isHierarchicalModeActive());
      memento.putBoolean(MEMENTO_IS_CATEGORY_MODE_ACTIVE, comboViewer.isVisible());
  }
  </code></pre>
  
  <p>This is my <code>saveState</code> - can I tell my view somehow tell to call it every time the view closes?</p>