Skip to content

Commit e9b533b

Browse files
scottamainAndroid Git Automerger
authored andcommitted
am 82f0ba7: am 4a6b694: am 0732210: revise code sample for callback interface
* commit '82f0ba79e67c154fc12a6cfde2fe1205da166b0d': revise code sample for callback interface
2 parents 5e42147 + 82f0ba7 commit e9b533b

File tree

1 file changed

+28
-33
lines changed

1 file changed

+28
-33
lines changed

docs/html/guide/topics/ui/dialogs.jd

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ onCreateDialog()} callback method.</p>
119119
a {@link android.support.v4.app.DialogFragment}:</p>
120120

121121
<pre>
122-
public class FireMissilesDialog extends DialogFragment {
122+
public class FireMissilesDialogFragment extends DialogFragment {
123123
&#64;Override
124124
public Dialog onCreateDialog(Bundle savedInstanceState) {
125125
// Use the Builder class for convenient dialog construction
@@ -469,7 +469,7 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
469469
})
470470
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
471471
public void onClick(DialogInterface dialog, int id) {
472-
NoticeDialog.this.getDialog().cancel();
472+
LoginDialogFragment.this.getDialog().cancel();
473473
}
474474
});
475475
return builder.create();
@@ -497,15 +497,15 @@ in the <a href="{@docRoot}guide/topics/manifest/activity-element.html">{@code
497497
<p>When the user touches one of the dialog's action buttons or selects an item from its list,
498498
your {@link android.support.v4.app.DialogFragment} might perform the necessary
499499
action itself, but often you'll want to deliver the event to the activity or fragment that
500-
opened the dialog. To do this, define an interface with a method for each type of click event,
501-
then implement that interface in the host component that will
500+
opened the dialog. To do this, define an interface with a method for each type of click event.
501+
Then implement that interface in the host component that will
502502
receive the action events from the dialog.</p>
503503

504504
<p>For example, here's a {@link android.support.v4.app.DialogFragment} that defines an
505505
interface through which it delivers the events back to the host activity:</p>
506506

507507
<pre>
508-
public class NoticeDialog extends DialogFragment {
508+
public class NoticeDialogFragment extends DialogFragment {
509509

510510
/* The activity that creates an instance of this dialog fragment must
511511
* implement this interface in order to receive event callbacks.
@@ -516,48 +516,44 @@ public class NoticeDialog extends DialogFragment {
516516
}
517517

518518
// Use this instance of the interface to deliver action events
519-
static NoticeDialogListener mListener;
520-
521-
/* Call this to instantiate a new NoticeDialog.
522-
* @param activity The activity hosting the dialog, which must implement the
523-
* NoticeDialogListener to receive event callbacks.
524-
* @returns A new instance of NoticeDialog.
525-
* @throws ClassCastException if the host activity does not
526-
* implement NoticeDialogListener
527-
*/
528-
public static NoticeDialog newInstance(Activity activity) {
519+
NoticeDialogListener mListener;
520+
521+
// Override the Fragment.onAttach() method to instantiate the NoticeDialogListener
522+
&#64;Override
523+
public void onAttach(Activity activity) {
524+
super.onAttach(activity);
529525
// Verify that the host activity implements the callback interface
530526
try {
531-
// Instantiate the NoticeDialogListener so we can send events with it
527+
// Instantiate the NoticeDialogListener so we can send events to the host
532528
mListener = (NoticeDialogListener) activity;
533529
} catch (ClassCastException e) {
534530
// The activity doesn't implement the interface, throw exception
535531
throw new ClassCastException(activity.toString()
536532
+ " must implement NoticeDialogListener");
537533
}
538-
NoticeDialog frag = new NoticeDialog();
539-
return frag;
540534
}
541-
542535
...
543536
}
544537
</pre>
545538

546-
<p>The activity hosting the dialog creates and shows an instance of the dialog
547-
by calling {@code NoticeDialog.newInstance()} and receives the dialog's
539+
<p>The activity hosting the dialog creates an instance of the dialog
540+
with the dialog fragment's constructor and receives the dialog's
548541
events through an implementation of the {@code NoticeDialogListener} interface:</p>
549542

550543
<pre>
551544
public class MainActivity extends FragmentActivity
552-
implements NoticeDialog.NoticeDialogListener{
545+
implements NoticeDialogFragment.NoticeDialogListener{
553546
...
554547

555548
public void showNoticeDialog() {
556549
// Create an instance of the dialog fragment and show it
557-
DialogFragment dialog = NoticeDialog.newInstance(this);
558-
dialog.show(getSupportFragmentManager(), "NoticeDialog");
550+
DialogFragment dialog = new NoticeDialogFragment();
551+
dialog.show(getSupportFragmentManager(), "NoticeDialogFragment");
559552
}
560553

554+
// The dialog fragment receives a reference to this Activity through the
555+
// Fragment.onAttach() callback, which it uses to call the following methods
556+
// defined by the NoticeDialogFragment.NoticeDialogListener interface
561557
&#64;Override
562558
public void onDialogPositiveClick(DialogFragment dialog) {
563559
// User touched the dialog's positive button
@@ -573,11 +569,12 @@ public class MainActivity extends FragmentActivity
573569
</pre>
574570

575571
<p>Because the host activity implements the {@code NoticeDialogListener}&mdash;which is
576-
enforced by the {@code newInstance()} method shown above&mdash;the dialog fragment can use the
572+
enforced by the {@link android.support.v4.app.Fragment#onAttach onAttach()}
573+
callback method shown above&mdash;the dialog fragment can use the
577574
interface callback methods to deliver click events to the activity:</p>
578575

579576
<pre>
580-
public class NoticeDialog extends DialogFragment {
577+
public class NoticeDialogFragment extends DialogFragment {
581578
...
582579

583580
&#64;Override
@@ -588,13 +585,13 @@ public class NoticeDialog extends DialogFragment {
588585
.setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() {
589586
public void onClick(DialogInterface dialog, int id) {
590587
// Send the positive button event back to the host activity
591-
mListener.onDialogPositiveClick(NoticeDialog.this);
588+
mListener.onDialogPositiveClick(NoticeDialogFragment.this);
592589
}
593590
})
594591
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
595592
public void onClick(DialogInterface dialog, int id) {
596593
// Send the negative button event back to the host activity
597-
mListener.onDialogPositiveClick(NoticeDialog.this);
594+
mListener.onDialogPositiveClick(NoticeDialogFragment.this);
598595
}
599596
});
600597
return builder.create();
@@ -604,8 +601,6 @@ public class NoticeDialog extends DialogFragment {
604601

605602

606603

607-
608-
609604
<h2 id="ShowingADialog">Showing a Dialog</h2>
610605

611606
<p>When you want to show your dialog, create an instance of your {@link
@@ -621,7 +616,7 @@ android.support.v4.app.Fragment}. For example:</p>
621616

622617
<pre>
623618
public void confirmFireMissiles() {
624-
DialogFragment newFragment = FireMissilesDialog.newInstance(this);
619+
DialogFragment newFragment = new FireMissilesDialogFragment();
625620
newFragment.show(getSupportFragmentManager(), "missiles");
626621
}
627622
</pre>
@@ -653,7 +648,7 @@ onCreateView()} callback.</p>
653648
dialog or an embeddable fragment (using a layout named <code>purchase_items.xml</code>):</p>
654649

655650
<pre>
656-
public class CustomLayoutDialog extends DialogFragment {
651+
public class CustomDialogFragment extends DialogFragment {
657652
/** The system calls this to get the DialogFragment's layout, regardless
658653
of whether it's being displayed as a dialog or an embedded fragment. */
659654
&#64;Override
@@ -683,7 +678,7 @@ or a fullscreen UI, based on the screen size:</p>
683678
<pre>
684679
public void showDialog() {
685680
FragmentManager fragmentManager = getSupportFragmentManager();
686-
CustomLayoutDialog newFragment = new CustomLayoutDialog();
681+
CustomDialogFragment newFragment = new CustomDialogFragment();
687682

688683
if (mIsLargeLayout) {
689684
// The device is using a large layout, so show the fragment as a dialog

0 commit comments

Comments
 (0)