title: How to use interface to communicate between two activities
body: <p>I am trying to make listener interface between two activities <code>Act1 and Act2</code>. <code>Act1</code> will start <code>Act2</code>. If there is some event occurred in <code>Act2</code>, it will inform it to <code>Act1</code>. Problem is that I am starting new activity using Intent, so how Act1 will assign itself as listener to Act2's interface?</p>
  
  <p><strong>Act1.java</strong></p>
  
  <pre class="lang-java prettyprint-override"><code>public class Act1 extends ActionBarActivity implements
          ActionBar.OnNavigationListener {
  
      ActionBar actionbar;
      Intent pizzaIntent;
      boolean visibleFirstTime = true;
  
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.menutab);
  
          //set actionbar here
      }
  
  @Override
      public boolean onNavigationItemSelected(int arg0, long arg1)// item pos,
                                                                  // itemid
      {
          switch (arg0) {
          case 0:
              if(this.visibleFirstTime == false)
              {
              if(pizzaIntent == null)
              {
                  pizzaIntent = new Intent(this,Act2.class);
                  //how to call setChangeListener?
              }
              startActivity(pizzaIntent);
              }
              else
                  this.visibleFirstTime = false;
              break;
          case 1:
              System.out.println("selected: " + arg0);
              break;
          case 2:
              System.out.println("selected: " + arg0);
              break;
          case 3:
              System.out.println("selected: " + arg0);
              break;
          default:
              break;
          }
          return true;
      }
  }
  </code></pre>
  
  <p><strong>Act2.java</strong></p>
  
  <pre class="lang-java prettyprint-override"><code>public class Act2 extends Activity {
  
       selectionChangeListener listener;
  
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.pizza_slice_selection);
      }
  
      public void setChangeListener(selectionChangeListener listener)
      {
          this.listener = listener;
      }
  
      private interface selectionChangeListener
      {
          public void selectionMadeAtIndex(int index);
      }
  }
  </code></pre>
  
  <p><strong>Note:</strong> Please don't suggest me to use fragments. I want to use activities currently.</p>