Last active
September 9, 2018 15:17
-
-
Save OrenBochman/b05318b476e6244fcd3b0cac395aa068 to your computer and use it in GitHub Desktop.
Android dialog with host callbacks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends FragmentActivity | |
implements NoticeDialogFragment.NoticeDialogListener{ | |
//... | |
public void showNoticeDialog() { | |
// Create an instance of the dialog fragment and show it | |
DialogFragment dialog = new NoticeDialogFragment(); | |
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 | |
@Override | |
public void onDialogPositiveClick(DialogFragment dialog) { | |
// User touched the dialog's positive button | |
//... | |
} | |
@Override | |
public void onDialogNegativeClick(DialogFragment dialog) { | |
// User touched the dialog's negative button | |
//... | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class NoticeDialogFragment extends DialogFragment { | |
/** This interface (inner to the dialog) must be implemented by | |
* the host activity that calls the dialog, allowing it to receive event callbacks. | |
*/ | |
public interface NoticeDialogListener { | |
public void onDialogPositiveClick(DialogFragment dialog); | |
public void onDialogNegativeClick(DialogFragment dialog); | |
} | |
// Use this instance of the interface to deliver action events | |
NoticeDialogListener mListener; | |
// Override the Fragment.onAttach() method to instantiate the NoticeDialogListener | |
@Override | |
public void onAttach(Activity activity) { | |
super.onAttach(activity); | |
// Verify that the host activity implements the callback interface | |
try { | |
// Instantiate the NoticeDialogListener so we can send events to the host | |
mListener = (NoticeDialogListener) activity; | |
} catch (ClassCastException e) { | |
// The activity doesn't implement the interface, throw exception | |
throw new ClassCastException(activity.toString() | |
+ " must implement NoticeDialogListener"); | |
} | |
} | |
// ... | |
@Override | |
public Dialog onCreateDialog(Bundle savedInstanceState) { | |
// Build the dialog and set up the button click handlers | |
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); | |
builder.setMessage(R.string.dialog_fire_missiles) | |
.setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() { | |
public void onClick(DialogInterface dialog, int id) { | |
// Send the positive button event back to the host activity | |
mListener.onDialogPositiveClick(NoticeDialogFragment.this); | |
} | |
}) | |
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { | |
public void onClick(DialogInterface dialog, int id) { | |
// Send the negative button event back to the host activity | |
mListener.onDialogNegativeClick(NoticeDialogFragment.this); | |
} | |
}); | |
return builder.create(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment