title: How to save the state of an interface in my fragment?
body: <p>I have an <code>Activity</code> which uses a <code>ViewPager</code> to create three <code>Fragment</code>s in the <code>ViewPager</code> class I set the <code>Fragment</code>s to <code>setRetainInstace(true);</code> when they are created. </p>
  
  <p>Inside one of my <code>Fragment</code>s I am displaying some editable info. This <code>Fragment</code> launches a <code>DialogFragment</code> to edit the info. </p>
  
  <p>When the user doesn't change orientation I am able to Update the Info and send the results back to the Fragment in View, however, once I change the Orientation, and make an Update to the Info my <code>Interface</code> which is attached in the <code>DialogFragment</code>s <code>onAttach()</code> method i am getting a <code>NullPointerException</code>.</p>
  
  <p>I don't understand why though because, each time the new <code>DialogFragment</code> is launched the <code>onAttach()</code> Method is always called. </p>
  
  <p>How should I solve this? 
  Can I save the state of the Interface ? and if so How?</p>
  
  <p>Here is my DialogFragment Code:
  <strong>The GenericDialogFragment Class is only used to make changes to the Appearance</strong></p>
  
  <pre><code>public class Fragment1 extends GenericDialogFragment implements WebServiceResult{
  
  
  
  // -------------------------------------------------------------------------
  // Member Variables
  //--------------------------------------------------------------------------
  //Webservice Callback
  private WSRequest mActiveRequest = null;
  // The Current Context of the Application
  private Context mClassContext = null;
  // Interface reference for communication
  private static CommunicateResults communicateResults = null;
  
  
  // -------------------------------------------------------------------------
  // New Instance Method
  //--------------------------------------------------------------------------    
  
  public static Fragment1 newInstance(int userId, GenericObject [] objects, GenericGroup [] groups, Object currentObject){
      // Initialize a new Fragment1
      Fragment1 fragment = new Fragment1();
      // Create a new Bundle
      Bundle args = new Bundle();
  
      fragment.setArguments(args);
  
      // Return the Fragment1
      return fragment;
  }
  
  // -------------------------------------------------------------------------
  // Class Functions / Methods
  //--------------------------------------------------------------------------    
  
  // States that the Interface is attached to the parent activity
  @Override public void onAttach(Activity activity)
  {
      // Perform the Default Behavior
      super.onAttach(activity);
      Log.d("ONAttach()","On attach() is called" );
      // Try 
      try{
          // Attach the interface to the activity
          communicateResults = (CommunicateResults) ((MainActivity)  getActivity()).findFragment(EditableFragment1.class);
  
      }catch(Exception ex){
          // Print the stack trace
          ex.printStackTrace();
      }
  }
  
  // States that the Dialog's View has been Created
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
      // Return the Inflated XML Layout
      return inflater.inflate(R.layout.fragment1, container, false);
  }
  
  // States that the fragment has been created, last chance to update the UI
  @Override
  public void onActivityCreated(Bundle savedInstanceState){
      // Perform the Default behavior
      super.onActivityCreated(savedInstanceState); 
  
      mClassContext = getActivity();
  
      // Get the Arguments passed into the Fragment instance
      savedStateData = getArguments();
  
      // Reference all the UI Elements in the View    
  
  
      // Add listeners to the Button Widgets
  
  
      // If the savedInstanceState is not null, get the current object
      if(savedStateData != null){
  
          // Get the object out of the state data
          mCurrentObject = savedStateData.getParcelable(STATE_DATA_CURRENT_OBJECT);
  
      }
  
  
  
  }
  
  
  //-----------------------------------------------------------------------------
  // Webservice Callback methods
  //-----------------------------------------------------------------------------
  
  // States that the web service has succeeded 
  @Override public void webserviceSucceeded(WebServiceBase finishedService, Object responseData) 
  {
  
      Log.d("EDIT Object", responseData.toString());
  
      if(responseData != null){
  
          if(mDeletingObject){
  
  
  
              // Send Back to the object to remove
              communicateResults.sendBackData(mCurrentObject, ACTION_DELETE);
  
          }else{
  
              JSONObject tempObject = (JSONObject) responseData;
  
              try{
  
                  // Parse Data ...
  
  
  
              }catch(Exception ex){
  
                  ex.printStackTrace();
                  // TODO: The Object was deleted from the Lest
              }
  
              // If we are creating a object, bundle the information to pass to the parent activity
              if(mCreatingObject){
  
                  // Create a new Workout Object
                  mCurrentObject = new Object();
  
  
                  // Callback to Parent Activity to notify that data has changed
                  communicateResults.sendBackData(mCurrentObject, ACTION_CREATE);
  
                  // Else the Object was updated
              }else{
                  // Create a new  Object
                  mCurrentObject = new Object();
  
  
                  // Callback to Parent Activity to notify that data has changed
                  communicateResults.sendBackData(mCurrentObject, ACTION_UPDATE);
              }
          }
  
  
      }
  
      // Dismiss the fragment
  
  
  }
  
  
  // States that the web service has failed
  @Override
  public void webserviceFailed(WebServiceBase finishedService,
          Object errorData) {
  
  
      // Display the Error
      displayErrorData(errorData);
  
  }
  </code></pre>
  
  <p>}</p>