项目中我们有时会遇到这样一种场景,首页中加载数据要弹出一个加载对话框,加载完数据之后可能要弹出一个定位城市的选择确认框,或者个人喜好设置对话框,或者感兴趣的栏目订阅选择对话框。为了便于管理dialog,取消前一个对话框后再显示下一个对话框,我们可以用FIFO队列对dialog进行排队。
private Queque<Dialog> dialogQueue = new LinkedList<>();
private Dialog currentDialog = null; // 当前显示的对话框
private void showDialog(Dialog dialog) {
if (dialog != null) {
dialogQueue.offer(dialog);
}
if (currentDialog == null) {
currentDialog = dialogQueue.poll();
if (currentDialog != null) {
currentDialog.show();
currentDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
currentDialog = null;
showDialog(null);
}
});
}
}
}
/**
* Inserts the specified element into this queue if it is possible to do
* so immediately without violating capacity restrictions.
* When using a capacity-restricted queue, this method is generally
* preferable to {@link #add}, which can fail to insert an element only
* by throwing an exception.
*
* @param e the element to add
* @return <tt>true</tt> if the element was added to this queue, else
* <tt>false</tt>
* @throws ClassCastException if the class of the specified element
* prevents it from being added to this queue
* @throws NullPointerException if the specified element is null and
* this queue does not permit null elements
* @throws IllegalArgumentException if some property of this element
* prevents it from being added to this queue
*/
boolean offer(E e);
/**
* Retrieves and removes the head of this queue,
* or returns <tt>null</tt> if this queue is empty.
*
* @return the head of this queue, or <tt>null</tt> if this queue is empty
*/
E poll();
原文:http://www.cnblogs.com/warmwei818/p/5351850.html