Frage

Is there any difference between these methods? I'd like to create the dialog first so that I have the reference to it when something is clicked in the view to dismiss it. Obviously that's not possible if I'm setting the view with the builder and the alert is not created yet.

War es hilfreich?

Lösung

As per AlertDialog.Builder source it makes no difference.

Andere Tipps

"I'd like to create the dialog first so that I have the reference to it when something is clicked in the view to dismiss it."

The user can't click to dismiss the dialog until to you show it to them, and that is after you have created an instance. See the lifecycle example below:

    View someView = ...;
    View someOtherView= ...;

    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    //sets the view, but doesn't show anything.     
    builder.setView(someView);  

    //now we have an instance of AlertDialog, still not shown
    AlertDialog dialog = builder.create();

    //now we showed it
    dialog.show();

    //...(wait some time)
    //now we changed its view after being shown
    dialog.setView(someOtherView);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top