How do I display consecutive AlertDialogs in Android?
I'm trying to port one of my iPhone apps over to the Android. It was all going along开发者_运维技巧 swimmingly until I came to AlertDialogs. In the iPhone app, sometimes there will be more than one alert to pass to the user. When this happens, the first alert dialog will come up, and when they click it away the next one will come up. I can't seem to get more than one dialog box to come up like that in Android. Is it possible to display back to back AlertDialogs where a second one pops up as soon as the first is finished?
You execute 2 consecutive call to 'showDialog()' after eachother and the second will show after the 1st was dismissed:
showDialog(FIRST_DIALOG_ID);
showDialog(SECOND_DIALOG_ID);
Ofcourse you also have to implement onCreateDialog().
If you feel that you will be having multiple dialogs, one after another, you could create a custom class that holds all of the information for the alert, such as the title, text, icon, etc. From there, create an arraylist to store the custom class objects. When you are done with your first dialog, remove it from the arraylist, then check to see if there are any remaining dialogs that need to be presented.
The only issue you'll run into is that it will be much more difficult if you want to have different conditions in your Confirm and Cancel options.
public class DialogObject(){
String title;
String body;
String iconName; // or just an Image asset
}
ArrayList<DialogObject> dialogList = new ArrayList<>();
When a dialog is required, add it to the list if there is a dialog already on screen
dialogList.add(new DialogObject(param1, param2, param3));
This may not be the best way, but it is an option. The ArrayList will need to be in a separate class itself so you don't lose the data when moving from screen to screen.
For example - Note the "static" keyword.
public class DialogHolder(){
public static ArrayList<DialogObject> = new ArrayList<>();
}
精彩评论