title: Android Fragment Callbacks to set Member Variables
body: <p><strong>TLDR:</strong> I'm wondering if there is any harm in using <code>Fragment</code> callbacks to pass in arguments instead of using a <code>Fragment#newInstance</code> method and setting up a bundle.</p>
  
  <hr>
  
  <p>As described <a href="https://stackoverflow.com/a/9245510/1290264">here</a>, the "proper" way of setting up a <code>Fragment</code> is by creating a static method that calls the empty constructor and sets up bundles. This is needed because Android sometimes needs to recreate your <code>Fragment</code> and can only use the empty constructor to do so.</p>
  
  <p>For complex datatypes this means implementing <a href="https://stackoverflow.com/a/7181792/1290264"><code>Parcelable</code></a> which can be a lot of work. I'm wondering if there is any harm in using <code>Fragment</code> callbacks to pass in these arguments by having the parent <code>Activity</code> implement these callbacks and send in parameters.</p>
  
  <h2><strong>Example:</strong></h2>
  
  <p><strong>Fragment Class:</strong></p>
  
  <pre><code>class MyFragment extends Fragment {
      ComplexDatatype = mComplexDatatype;
      OnFragmentInteractionListener = mListener;
  
      @Override
      public void onAttach(Activity activity) {
          super.onAttach(activity);
          try {
              mListener = (OnFragmentInteractionListener) activity;
          } catch (ClassCastException e) {
              throw new ClassCastException(activity.toString()
                  + " must implement OnFragmentInteractionListener");
          }
      }
  
      @Override
      public void onActivityCreate(){
          // Initialized member variables now
          mComplexDatatype = mListener.onGetComplexDatatype();
      }
  
      public interface OnFragmentInteractionListener {
          public ComplexDataType onGetComplexDatatype();
      }
  }
  </code></pre>
  
  <p><strong>Parent Activity Class:</strong></p>
  
  <pre><code>class MyActivity extends Activity
      implements MyFragment.OnInteractionListener {
  
      @Override
      public ComplexDatatype onGetComplexDatatype(){
          return new ComplexDatatype(...);
      }
  }
  </code></pre>