title: WhenActivated is called twice when used in Views and ViewModels hosted in ViewModelViewHost control
body: <p>My app uses views, which implement <code>IViewFor&lt;T&gt;</code> interface. The views are registered with the dependency resolver in <code>AppBootstrapper</code>. The app displays the views using <code>ViewModelViewHost</code> control by assigning a corresponding view model to control's <code>ViewModel</code> property. All the view models implement <code>ISupportsActivation</code> interface.</p>
  
  <p>I noticed that <code>WhenActivated</code> is called twice. First it's called when a view and view model get activated. Then on deactivation all disposables are disposed and <code>WhenActivated</code> is called again immediately followed by disposing the disposables.</p>
  
  <p>I am testing with the following code both in view and view model:</p>
  
  <pre><code>this.WhenActivated(disposables =&gt;
  {
      Debug.WriteLine("ViewModel activated.");
  
      Disposable
          .Create(() =&gt;
          {
              Debug.WriteLine("ViewModel deactivated.");
          })
          .AddTo(disposables);
  });
  </code></pre>
  
  <p>As a result the output looks like this:</p>
  
  <pre><code>// App displays the view:
  
  ViewModel activated.
  View activated.
  
  // App hides the view:
  
  ViewModel deactivated.
  View deactivated.
  ViewModel activated.
  View activated.
  ViewModel deactivated.
  View deactivated.
  </code></pre>
  
  <p>The view is hidden by setting ViewModel property of ViewModelViewHost control to null.</p>
  
  <p>Am I doing something wrong?</p>
  
  <p><strong>Edit:</strong> here's the complete source code: <a href="https://gist.github.com/dmakaroff/e7d84e06e0a48d7f5298eb6b7d6187d0" rel="nofollow noreferrer">https://gist.github.com/dmakaroff/e7d84e06e0a48d7f5298eb6b7d6187d0</a></p>
  
  <p>Pressing first Show and then Hide buttons produces the following output:</p>
  
  <pre><code>SubViewModel activated.
  SubView activated.
  SubViewModel deactivated.
  SubView deactivated.
  SubViewModel activated.
  SubView activated.
  SubViewModel deactivated.
  SubView deactivated.
  </code></pre>