Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 82f0ba79 authored by Scott Main's avatar Scott Main Committed by Android Git Automerger
Browse files

am 4a6b6948: am 07322102: revise code sample for callback interface

* commit '4a6b6948':
  revise code sample for callback interface
parents 64b3de29 4a6b6948
Loading
Loading
Loading
Loading
+28 −33
Original line number Original line Diff line number Diff line
@@ -119,7 +119,7 @@ onCreateDialog()} callback method.</p>
a {@link android.support.v4.app.DialogFragment}:</p>
a {@link android.support.v4.app.DialogFragment}:</p>


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


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


<pre>
<pre>
public class NoticeDialog extends DialogFragment {
public class NoticeDialogFragment extends DialogFragment {
    
    
    /* The activity that creates an instance of this dialog fragment must
    /* The activity that creates an instance of this dialog fragment must
     * implement this interface in order to receive event callbacks.
     * implement this interface in order to receive event callbacks.
@@ -516,48 +516,44 @@ public class NoticeDialog extends DialogFragment {
    }
    }
    
    
    // Use this instance of the interface to deliver action events
    // Use this instance of the interface to deliver action events
    static NoticeDialogListener mListener;
    NoticeDialogListener mListener;
        
    
    /* Call this to instantiate a new NoticeDialog.
    // Override the Fragment.onAttach() method to instantiate the NoticeDialogListener
     * @param activity  The activity hosting the dialog, which must implement the
    &#64;Override
     *                  NoticeDialogListener to receive event callbacks.
    public void onAttach(Activity activity) {
     * @returns A new instance of NoticeDialog.
        super.onAttach(activity);
     * @throws  ClassCastException if the host activity does not
     *          implement NoticeDialogListener
     */
    public static NoticeDialog newInstance(Activity activity) {
        // Verify that the host activity implements the callback interface
        // Verify that the host activity implements the callback interface
        try {
        try {
            // Instantiate the NoticeDialogListener so we can send events with it
            // Instantiate the NoticeDialogListener so we can send events to the host
            mListener = (NoticeDialogListener) activity;
            mListener = (NoticeDialogListener) activity;
        } catch (ClassCastException e) {
        } catch (ClassCastException e) {
            // The activity doesn't implement the interface, throw exception
            // The activity doesn't implement the interface, throw exception
            throw new ClassCastException(activity.toString()
            throw new ClassCastException(activity.toString()
                    + " must implement NoticeDialogListener");
                    + " must implement NoticeDialogListener");
        }
        }
        NoticeDialog frag = new NoticeDialog();
        return frag;
    }
    }
    
    ...
    ...
}
}
</pre>
</pre>


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


<pre>
<pre>
public class MainActivity extends FragmentActivity
public class MainActivity extends FragmentActivity
                          implements NoticeDialog.NoticeDialogListener{
                          implements NoticeDialogFragment.NoticeDialogListener{
    ...
    ...
    
    
    public void showNoticeDialog() {
    public void showNoticeDialog() {
        // Create an instance of the dialog fragment and show it
        // Create an instance of the dialog fragment and show it
        DialogFragment dialog = NoticeDialog.newInstance(this);
        DialogFragment dialog = new NoticeDialogFragment();
        dialog.show(getSupportFragmentManager(), "NoticeDialog");
        dialog.show(getSupportFragmentManager(), "NoticeDialogFragment");
    }
    }


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


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


<pre>
<pre>
public class NoticeDialog extends DialogFragment {
public class NoticeDialogFragment extends DialogFragment {
    ...
    ...


    &#64;Override
    &#64;Override
@@ -588,13 +585,13 @@ public class NoticeDialog extends DialogFragment {
               .setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() {
               .setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                   public void onClick(DialogInterface dialog, int id) {
                       // Send the positive button event back to the host activity
                       // Send the positive button event back to the host activity
                       mListener.onDialogPositiveClick(NoticeDialog.this);
                       mListener.onDialogPositiveClick(NoticeDialogFragment.this);
                   }
                   }
               })
               })
               .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
               .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                   public void onClick(DialogInterface dialog, int id) {
                       // Send the negative button event back to the host activity
                       // Send the negative button event back to the host activity
                       mListener.onDialogPositiveClick(NoticeDialog.this);
                       mListener.onDialogPositiveClick(NoticeDialogFragment.this);
                   }
                   }
               });
               });
        return builder.create();
        return builder.create();
@@ -604,8 +601,6 @@ public class NoticeDialog extends DialogFragment {








<h2 id="ShowingADialog">Showing a Dialog</h2>
<h2 id="ShowingADialog">Showing a Dialog</h2>


<p>When you want to show your dialog, create an instance of your {@link
<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>


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


<pre>
<pre>
public class CustomLayoutDialog extends DialogFragment {
public class CustomDialogFragment extends DialogFragment {
    /** The system calls this to get the DialogFragment's layout, regardless
    /** The system calls this to get the DialogFragment's layout, regardless
        of whether it's being displayed as a dialog or an embedded fragment. */
        of whether it's being displayed as a dialog or an embedded fragment. */
    &#64;Override
    &#64;Override
@@ -683,7 +678,7 @@ or a fullscreen UI, based on the screen size:</p>
<pre>
<pre>
public void showDialog() {
public void showDialog() {
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentManager fragmentManager = getSupportFragmentManager();
    CustomLayoutDialog newFragment = new CustomLayoutDialog();
    CustomDialogFragment newFragment = new CustomDialogFragment();
    
    
    if (mIsLargeLayout) {
    if (mIsLargeLayout) {
        // The device is using a large layout, so show the fragment as a dialog
        // The device is using a large layout, so show the fragment as a dialog