Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's another recipe without using any Interface. Just making use of the <code>setTargetFragment</code> and <code>Bundle</code> to pass data between DialogFragment and Fragment.</p> <pre><code>public static final int DATEPICKER_FRAGMENT = 1; // class variable </code></pre> <p>1. Call the <code>DialogFragment</code> as shown below:</p> <pre><code>// create dialog fragment DatePickerFragment dialog = new DatePickerFragment(); // optionally pass arguments to the dialog fragment Bundle args = new Bundle(); args.putString("pickerStyle", "fancy"); dialog.setArguments(args); // setup link back to use and display dialog.setTargetFragment(this, DATEPICKER_FRAGMENT); dialog.show(getFragmentManager().beginTransaction(), "MyProgressDialog") </code></pre> <p>2. Use the extra <code>Bundle</code> in an <code>Intent</code> in the <code>DialogFragment</code> to pass whatever info back to the target fragment. The below code in <code>Button#onClick()</code> event of <code>DatePickerFragment</code> passes a String and Integer.</p> <pre><code>Intent i = new Intent() .putExtra("month", getMonthString()) .putExtra("year", getYearInt()); getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, i); dismiss(); </code></pre> <p>3. Use <code>CalendarFragment</code>'s <code>onActivityResult()</code> method to read the values:</p> <pre><code>@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case DATEPICKER_FRAGMENT: if (resultCode == Activity.RESULT_OK) { Bundle bundle = data.getExtras(); String mMonth = bundle.getString("month", Month); int mYear = bundle.getInt("year"); Log.i("PICKER", "Got year=" + year + " and month=" + month + ", yay!"); } else if (resultCode == Activity.RESULT_CANCELED) { ... } break; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload