title: Where to put onClickListener for Button in recycled fragment
body: <p>New to android/java here. I'm trying to create an app with a recycled fragment piece to control power switches over the internet. I created a class to define each remote switch's values. I pass these in as a Bundle with setArguments when creating a new instance of the fragment.
  I presumed since I was recycling the component for multiple remote-switches that I would want a separate fragment activity class to handle the dirty work. I need to do things like detect a click of a button to send a json call to trigger a toggle of the remote switch, and then handle a callback (eventually) to actually change the indicator when the remote responds that it has actually toggled the relay.
  I assume these are the sorts of things that would take place in the Activity class, but I'm unsure first off how to access properties in the fragment (which includes the definition of the remote switch and where I pulled out the handles for the layout items) in order to manipulate the fragment display and assign the onClickListener to the toggle button and such.</p>
  
  <p>The first thing I need to do is know where the onClickListener for the button should be defined. I know I can retrieve the Button with another findViewById but that seems kind of silly when they are already defined in the fragment. I just don't know how to get back to the fragment from within the activity itself to access the properties and methods therein. And I'm not sure where the click listener should be defined (conventionally) anyway.</p>
  
  <p>Here's my fragment code:</p>
  
  <p>res/layout/fragment_piswitch.xml
      </p>
  
  <pre><code>    &lt;Button
          android:id="@+id/piToggle1"
          android:layout_width="35dp"
          android:layout_height="35dp"
          android:text="O" /&gt;
  
      &lt;Switch
          android:id="@+id/piStatus1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginLeft="38dp"
          android:layout_marginTop="4dp"
          android:clickable="false"
          android:text="PiSwitch1"
          android:theme="@style/ToggleSwitchTheme" /&gt;
  
      &lt;TextView
          android:id="@+id/piInfo1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginLeft="6dp"
          android:layout_marginTop="9dp"
          android:layout_gravity="right"
          android:text="(output)" /&gt;
  
  
  &lt;/FrameLayout&gt;
  </code></pre>
  
  <p>PiSwitchFragment.java</p>
  
  <pre><code>public class PiSwitchFragment extends Fragment {
      PiSwitchFragmentActivity listener;
  
      private PiSwitch fragSwitch = new PiSwitch();
  
      public Button fragToggle;
      public Switch fragStatus;
      public TextView fragInfo;
  
      public PiSwitchFragment() {
          // Required empty public constructor
      }
  
      // This event fires 1st, before creation of fragment or any views
      // The onAttach method is called when the Fragment instance is associated with an Activity.
      // This does not mean the Activity is fully initialized.
      @Override
      public void onAttach(Context context) {
          super.onAttach(context);
          if (context instanceof Activity){
              this.listener = (PiSwitchFragmentActivity) context;
          }
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
  
          // check for arguments to initialize the PiSwitch
          Bundle args = getArguments();
          if(args != null) {
              this.fragSwitch.setPiName(args.getString("name","(Unknown)"));
              this.fragSwitch.setPiId(args.getCharArray("id"));
              this.fragSwitch.setSwitchPin(args.getInt("pin",0));
              this.fragSwitch.setSwitchState(args.getBoolean("state", false));
              this.fragSwitch.setPiAddress(args.getString("addr",""));
          }
          // Inflate the layout for this fragment
          return inflater.inflate(R.layout.fragment_piswitch, container, false);
      }
  
      // This event is triggered soon after onCreateView().
      // Any view setup should occur here.  E.g., view lookups and attaching view listeners.
      @Override
      public void onViewCreated(View view, Bundle savedInstanceState) {
          // Setup any handles to view objects here
          this.fragToggle = (Button)view.findViewById(R.id.piToggle1);
          this.fragStatus = (Switch)view.findViewById(R.id.piStatus1);
          this.fragInfo = (TextView)view.findViewById(R.id.piInfo1);
  
          this.updateInfo(this.fragSwitch.getPiName()); // update the display name
          this.updateStatus(this.fragSwitch.getSwitchState()); // set the current displayed switch state
      }
  
      public void updateStatus(Boolean status) {
          this.fragStatus.setChecked(status);
      }
  
      public void updateInfo(String inf) {
          this.fragInfo.setText(inf);
      }
  }
  </code></pre>
  
  <p>PiSwitchFragmentActivity</p>
  
  <pre><code>public class PiSwitchFragmentActivity extends FragmentActivity {
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          //////////////////////////////////////////////////////////////
          // not sure how to access the associated fragment from here //
          //////////////////////////////////////////////////////////////
          // where should onClickListener for piToggle1 button be defined? //
      };
  }
  </code></pre>