title: Get and Put extra, DialogFragment
body: <p>
  I'm writing an app where the user describes a problem and then receives advice. The user presses a button which shows a dialog with an <code>EditText</code>. Once the user presses <code>OK</code>, I want to get their input, but I'm having trouble with the extras. I've read similar questions, but I can't seem to find the problem. On a summary screen where I display the information, no text ever appears. Any help is appreciated! </p>
  
  <p>I think the problem is when  I call <code>getText()</code> on the <code>EditText</code>. Using <code>log.d</code> the <code>mText</code> is just an empty String.</p>
  
  <p>Here is my code:</p>
  
  <p>The fragment <code>AdviceFragment</code> from which the dialog is called:</p>
  
  <pre><code>private static final String DIALOG_TEXT = "text";
  private static final int REQUEST_TEXT = 0;
  private Advice mAdvice;
  private boolean hasText;
  
  ...
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState)
  {
      ...
  
      mTextButton = (Button) v.findViewById(R.id.textButton);
      mTextButton.setOnClickListener(new View.OnClickListener()
      {
          public void onClick(View v)
          {
              FragmentManager fm = getActivity().getSupportFragmentManager();
              InputTextFragment dialog = new InputTextFragment();
              dialog.setTargetFragment(AdviceFragment.this, REQUEST_TEXT);
              dialog.show(fm, DIALOG_TEXT);
          }
      });
  }
  
  ...
  
  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data)
  {
      if (resultCode != Activity.RESULT_OK)
      {
          return;
      }
  
      if (resultCode == REQUEST_TEXT)
      {
          String text = data.getStringExtra(InputTextFragment.EXTRA_TEXT);
          if (text.length() &gt; 0)
          {
              mAdvice.setText(text);
              hasText = true;
          }
          else
          {
              mAdvice.setText(null);
              hasText = false;
          }
      }
  </code></pre>
  
  <p><code>InputTextFragment</code> dialog:</p>
  
  <pre><code>public class InputTextFragment extends DialogFragment
  {
      public static final String EXTRA_TEXT = "text";
      private String mText;
  
      private void sendResult(int resultCode)
      {
          if (getTargetFragment() == null)
          {
              return;
          }
  
          Intent i = new Intent();
          i.putExtra(EXTRA_TEXT, mText.toString());
  
          getTargetFragment().onActivityResult(getTargetRequestCode(), resultCode, i);
      }
  
      @Override
      public Dialog onCreateDialog(Bundle savedInstanceState)
      {
          View v = getActivity().getLayoutInflater().inflate(R.layout.dialog_input_text, null);
  
          final EditText editText = new EditText(getActivity());
  
          return new AlertDialog.Builder(getActivity())
                  .setView(v)
                  .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener()
                  {
                      @Override
                      public void onClick(DialogInterface dialog, int whichButton)
                      {
                          String input = editText.getText().toString();
                          if (input.length() &gt; 0)
                          {
                              mText = input;
                          }
                          else
                          {
                              return;
                          }
  
                          sendResult(Activity.RESULT_OK);               
                      }
                  })
                  .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener()
                  {
                      ...
                  })
                  .create();
      }
  }
  </code></pre>