title: Java Tapestry : why is onActivate method called twice?
body: <p>I have a tapestry page in which I have two onActivate() methods with different parameters. </p>
  
  <p><strong>// both the methods are called in this case</strong></p>
  
  <pre><code>void onActivate(String filterSetJson) {
      schema = getSchema();
      if (ValueHelper.isDefined(filterSetJson) &amp;&amp; !filterSetJson.equals("all")) {
          setFilterSet( filterSetJson );
      }
      else {
          setFilterSet("{}"); // Empty filter set
      }
      this.loadedTagSummary = false;
      sortOrderForTags="count";
      sortOrderForTypes="count";
  }
  
  
  void onActivate(String filterSetJson, int pageCount) {
      //onActivate( filterSetJson );
      this.pageCount = pageCount;
  }
  </code></pre>
  
  <p>Sometimes this pages is invoked with two arguments, sometimes with one. The problem is that when the page is invoked with two parameters, both onActivate() methods are called. Only the method which accepts two parameters should be called.</p>
  
  <p>As suggested in link below, I modified code to have just one onActivate() method which accepts EventContext as a parameter.
  <a href="http://apache-tapestry-mailing-list-archives.1045711.n5.nabble.com/Multiple-onActivate-methods-td2434897.html#a2434901" rel="nofollow">http://apache-tapestry-mailing-list-archives.1045711.n5.nabble.com/Multiple-onActivate-methods-td2434897.html#a2434901</a></p>
  
  <p>So the modified code is as follows.</p>
  
  <p><strong>// the following method is called twice in this case</strong></p>
  
  <pre><code>Object onActivate(EventContext eContext){
      int paramCount = eContext.getCount();
      if( paramCount == 0 )   return true;
      String filterSetJson = eContext.get(String.class, 0);
      if( paramCount &gt; 1 ){
          int pageCount = eContext.get(Integer.class, 1);
          this.pageCount = pageCount;
      }
      schema = getSchema();
      if (ValueHelper.isDefined(filterSetJson) &amp;&amp; !filterSetJson.equals("all")) {
          setFilterSet( filterSetJson );
      }
      else {
          setFilterSet("{}"); // Empty filter set
      }
      this.loadedTagSummary = false;
      sortOrderForTags="count";
      sortOrderForTypes="count";
      return true;    
  }
  </code></pre>
  
  <p>Now, the problem is that onActivate() is still called twice. Once the parameters are present and the other time there are no parameters(I think the EventContext is empty this time). Therefore I had to add the check for parameters and return if they are not present. This avoids the whole code being executed twice but is still not a clean fix. I don't understand why the method is called twice. I have considered following potential reasons as suggested in some mailing list posts.</p>
  
  <p>1: I am sure there is no image on page being given a relative path. All images are loaded with 'context'</p>
  
  <p>2: There is no super class implementing onActivate() method. It is only present in this class.</p>
  
  <p>Interestingly as suggested in link below, if I return true from both onActivate() methods, then only one method is called and the problem is fixed!
  <a href="http://apache-tapestry-mailing-list-archives.1045711.n5.nabble.com/T5-onActivate-called-twice-td2428923.html" rel="nofollow">http://apache-tapestry-mailing-list-archives.1045711.n5.nabble.com/T5-onActivate-called-twice-td2428923.html</a></p>
  
  <p>This is the code which works fine.</p>
  
  <p><strong>// only one method is called in this case. This is the correct behavior</strong> </p>
  
  <pre><code>Object onActivate(String filterSetJson) {
      schema = getSchema();
      if (ValueHelper.isDefined(filterSetJson) &amp;&amp; !filterSetJson.equals("all")) {
          setFilterSet( filterSetJson );
      }
      else {
          setFilterSet("{}"); // Empty filter set
      }
      this.loadedTagSummary = false;
      sortOrderForTags="count";
      sortOrderForTypes="count";
      return true;
  }
  
  
  Object onActivate(String filterSetJson, int pageCount) {
      //onActivate( filterSetJson );
      this.pageCount = pageCount;
      return true;
  }
  </code></pre>
  
  <p>I can live with this solution but I don't understand what is happening here. Can anyone throw light on why both onActivate() methods are called  when they don't return true or why onActivate(EventContext eContext) is called twice?</p>
  
  <p>The other reason for posting this question is to document this for myself and for others since I have spent a lot of time in this issue.</p>
  
  <p>Tapestry version is 5.3</p>