Yes/No dialog and the lifecycle
I'm sure this is a fundamental question, but my research yields nothing useful. My new application needs to use a Yes/No dialog under a few circumstances, and I'm not getting how dialogs fit into the application lifecycle. For example, I would like to create a method to support this type of construct:
if (yesNoAlert("Title", "Do you want to try again?") == true) {
action1();
} else {
action2();
}
The method would look something like this:
private boolean yesNoAlert(String title, String message) {
final boolean returnValue;
DialogInterface.OnClickListener dialogClickListener = new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which){
case DialogInterface.BUTTON_POSITIVE:
returnValue = true;
break;
case DialogInterface.BUTTON_NEGATIVE:
returnValue=false;
break;
}
}
};
alertbox = new AlertDialog.Builder(this);
alertbox.setMessage(message)
.setTitle(title)
.setPositiveButton("Yes", dialogClickListener)
.setNegativeButton("No", dialogClickListener)
.show();
}
... but as you can see, it is not finished - there are a number of things not quiet correct about it. The piece I'm missing is how to know that the dialog has finished... what method exists that can be utilized s开发者_运维技巧o that the application can pick up on the fact that the button has been pressed? Of course, the BUTTON_POSITIVE and BUTTON_NEGATIVE actions respond to that, but my question is how to return with an indicator, so that the code that's waiting for a response will pick up again at action1() or action2(), depending upon response.
At present, I do not see any way for my application to determine this, - nor even a valid way to make a method/function from that code. so I'm missing some vital piece from the lifecycle.
Where might I read up on this? Of course, there are volumes of information available on internet about this, but for me as a relative newbie it's like trying drink from a fire hose.
This will make the action that needs to be taken dynamic:
private Thread actionToDo;
private void yesNoAlert(String title, String message)
{
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
switch (which)
{
case DialogInterface.BUTTON_POSITIVE:
actionToDo.start();
break;
case DialogInterface.BUTTON_NEGATIVE:
break;
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(message).setPositiveButton("Yes", dialogClickListener).setNegativeButton("No", dialogClickListener).setTitle(title).show();
}
You could do like this
private boolean yesNoAlert(String title, String message) {
new AlertDialog.Builder(this).setMessage(message)
.setTitle(title)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) { action1(); }
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) { action2(); }
})
.show();
}
you can use a listener to achieve this. Like said in android documentation:
Define a interface with the actions you need to support (
onDialogPositiveClick
andonDialogNegativeClick
).public class NoticeDialogFragment extends DialogFragment {
/* The activity that creates an instance of this dialog fragment must * implement this interface in order to receive event callbacks. * Each method passes the DialogFragment in case the host needs to query it. */ 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"); } } ...
}
Make the class that displays the dialog implements your interface.
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 ... }
}
Make your dialog invoke these methods on the correct moment (when detect
setPositiveButton
orsetNegativeButton
click).public class NoticeDialogFragment extends DialogFragment { ...
@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(); }
}
Ref http://developer.android.com/guide/topics/ui/dialogs.html#PassingEvents
精彩评论