title: Passing Back Data from a DialogFragment to Its Calling Activity?
body: <p>My goal is to pass a String from a <strong>DialogFragment</strong> to its calling <strong>Activity</strong>.  If I understand correctly, I think there exists a need to implement an interface inside the calling Activity.  I think the purpose includes creating a "listener" method.</p>
  
  <p>When placed in the class extending DialogFragment, "getActivity()" does not seem to reach the calling Activity.  It does not seem to recognize any of the custom methods I add.</p>
  
  <p>How should I approach this?  Thank you for your time.</p>
  
  <p><strong>Edit 01:</strong>  Maybe I am blind to something, but I am not understanding why the "getActivity()" method in the DialogFragment-extended class is referencing the wrong object.  Thanks again.</p>
  
  <p>MainActivity.java :</p>
  
  <pre><code>import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.EditText;
  
  
  public class MainActivity extends AppCompatActivity {       //implements Something
  
      String dateThing;
      EditText date_EditText;
      DatePickerFragment newFragment;
  
  
      @Override
      protected void onCreate( Bundle savedInstanceState ) {
  
          super.onCreate( savedInstanceState );
          setContentView( R.layout.activity_main );
  
          dateThing = "Change This";
  
          //This just changes the title of the ToolBar.
          getSupportActionBar().setTitle( "Date Changing Test" );
  
          date_EditText = findViewById( R.id.dateXML );
          date_EditText.setText( dateThing );
  
      }
  
  
      public void showDatePicker_Thing( View v ) {
  
          newFragment = new DatePickerFragment();
          newFragment.show( getFragmentManager(), "date picker" );
  
      }
  
  
      public void setDate_in_EditText( String date_for_Setting_the_EditText ) {
  
          date_EditText.setText( date_for_Setting_the_EditText );
  
      }
  
  }
  </code></pre>
  
  <p>DatePickerFragment.java :</p>
  
  <pre><code>import android.app.Activity;
  import android.app.DatePickerDialog;
  import android.app.Dialog;
  import android.app.DialogFragment;
  import android.os.Bundle;
  import android.widget.DatePicker;
  import android.widget.Toast;
  
  import java.sql.Date;
  import java.util.Calendar;
  
  
  public class DatePickerFragment extends DialogFragment {
  
      Date testDate;
  
  
      @Override
      public Dialog onCreateDialog( Bundle savedInstanceState ) {
  
          final Calendar c = Calendar.getInstance();
          int year = c.get( Calendar.YEAR );
          int month = c.get( Calendar.MONTH );
          int day = c.get( Calendar.DAY_OF_MONTH );
  
  
          return new DatePickerDialog( getActivity(), dateSetting_Listener, year, month, day );
  
      }
  
  
      private DatePickerDialog.OnDateSetListener dateSetting_Listener =
              new DatePickerDialog.OnDateSetListener() {
  
          public void onDateSet( DatePicker view, int year, int month, int day ) {
  
              testDate = Date.valueOf( view.getYear() + "-" + ( view.getMonth() +1 ) + "-"
                      + view.getDayOfMonth() );
  
              Toast.makeText( getActivity(), "The selected date:  " + testDate.toString(),
                      Toast.LENGTH_LONG ).show();
  
              Activity activityTest = getActivity();
  
  
              //************************************************************************
              //TODO  "setDate_in_EditText()" NOT FOUND AS A METHOD FROM "MainActivity".
              //getActivity().setDate_in_EditText( "test" );
              getActivity();
              //************************************************************************
  
          }
      };
  
  }
  </code></pre>
  
  <p>activity_main.xml :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:gravity="center_horizontal"
  
      android:orientation="vertical"
      android:scrollbarAlwaysDrawVerticalTrack="true"
      android:scrollbars="vertical"
      android:showDividers="none"
  
      tools:context="com.moocow.datepickerfragment_test.MainActivity"
      &gt;
  
  
      &lt;TextView
          android:id="@+id/title"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="\nAdjustments Stuff"
          android:textAppearance="?android:attr/textAppearanceLarge"
          android:textStyle="bold"
          /&gt;
  
  
      &lt;EditText
          android:id="@+id/dateXML"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:inputType="textPersonName"
          android:onClick="showDatePicker_Thing"
          /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>