title: popup outside of a function call
body: <p>please can someone assist-</p>
  
  <p>I have a popup that I have created in a function called pop that creates a popupwindow. I also have another function that when a user clicks on a button in the popupwindow, it dismisses the popupWindow. I have created a separate function for the dismissal. I would like to pass the popupwindow into the dismiss function - what is the best way to do this. I would like to reuse the dismiss function.</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
  }
  
  public void popup(View v) {
      LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
      View layout = layoutInflater.inflate(R.layout.activity_alert_dialog,null);
      final PopupWindow popup= new PopupWindow(layout, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
  
      View vi=(View) findViewByID(R.id.Layout);
      popup.showAtLocation(vi, 0, 20, -50);
  
  }
  
  public void dismiss(View v) {
       popup.dismiss();
  
  } 
  }
  </code></pre>
  
  <p>My dismiss method is executed from within a resource layout file:</p>
  
  <pre><code>&lt;Button
      android:text="cancel"
      android:onClick="dismiss"/&gt;
  </code></pre>