Activity专门对dialog提供了自己的管理机制
//私有类ManagedDialog
private static class ManagedDialog {
Dialog mDialog;
Bundle mArgs;
}
//SparseArray是一个升序排列的有序数组 (int - object)
private SparseArray<ManagedDialog> mManagedDialogs;
//私有的一个列表来管理对话框
然后来看看对话框的一些方法:
1.显示
public final boolean showDialog(int id, Bundle args) {
if(mManagedDialogs ==null) {
mManagedDialogs =newSparseArray<ManagedDialog>();
}//如果存放对话框的数组不存在,生成1个
ManagedDialog md = mManagedDialogs.get(id);
if(md ==null) {
md =newManagedDialog();
md.mDialog = createDialog(id,null, args);
if(md.mDialog ==null) {
returnfalse;
}
mManagedDialogs.put(id, md);
}
//在数组中找到id的对话框,如果不存在生成一个新的
md.mArgs = args;
onPrepareDialog(id, md.mDialog, args);//刷新一下对话框的属性
md.mDialog.show();
//最后显示对话框,内容是args
return true;
}
2.产生一个对话框
private Dialog createDialog(Integer dialogId, Bundle state, Bundle args) {
final Dialog dialog = onCreateDialog(dialogId, args);
//onCreateDialog()函数中在一个ID标识的对话框第一次 创建的时候才会被回调
if(dialog ==null) {
return null;
}
dialog.dispatchOnCreate(state);
return dialog;
}
3.隐藏对话框
public final void dismissDialog(int id) {
if(mManagedDialogs ==null) {
throwmissingDialog(id);
}
final ManagedDialog md = mManagedDialogs.get(id);
if(md ==null) {
throwmissingDialog(id);
}
md.mDialog.dismiss();
}
4.从数组中删除一个对话框
public final void removeDialog(int id) {
if(mManagedDialogs ==null) {
return;
}
final ManagedDialog md = mManagedDialogs.get(id);
if(md ==null) {
return;
}
md.mDialog.dismiss();
mManagedDialogs.remove(id);
}
4.当Activity在前后台切换的时候,涉及到保存和恢复状态,这其中当然也要对对话框的状态进行处理,我们先来看一下当Activity切到后台时的状态保存处理
private void saveManagedDialogs(Bundle outState) {
if(mManagedDialogs ==null) {
return;
}
final int numDialogs = mManagedDialogs.size();
if(numDialogs == 0) {
return;
}
Bundle dialogState =newBundle(); //key -value的形式
int[] ids =newint[mManagedDialogs.size()];
// save each dialog's bundle, gather the ids
for(int i = 0; i < numDialogs; i++) {
final int key = mManagedDialogs.keyAt(i);
ids[i] = key;
final ManagedDialog md = mManagedDialogs.valueAt(i);//xxAt(i)
dialogState.putBundle(savedDialogKeyFor(key), md.mDialog.onSaveInstanceState()//Bundle);
if(md.mArgs !=null) {
dialogState.putBundle(savedDialogArgsKeyFor(key), md.mArgs);
}
}
dialogState.putIntArray(SAVED_DIALOG_IDS_KEY, ids);
outState.putBundle(SAVED_DIALOGS_TAG, dialogState);
}
private void restoreManagedDialogs(Bundle savedInstanceState) {
final Bundle b = savedInstanceState.getBundle(SAVED_DIALOGS_TAG);
if(b ==null) {
return;
}
final int[] ids = b.getIntArray(SAVED_DIALOG_IDS_KEY);
final int numDialogs = ids.length;
mManagedDialogs =newSparseArray<ManagedDialog>(numDialogs);
for(int i = 0; i < numDialogs; i++) {
final Integer dialogId = ids[i];
Bundle dialogState = b.getBundle(savedDialogKeyFor(dialogId));
if(dialogState !=null) {
// Calling onRestoreInstanceState() below will invoke dispatchOnCreate
// so tell createDialog() not to do it, otherwise we get an exception
final ManagedDialog md =newManagedDialog();
md.mArgs = b.getBundle(savedDialogArgsKeyFor(dialogId));
md.mDialog = createDialog(dialogId, dialogState, md.mArgs);
if(md.mDialog !=null) {
mManagedDialogs.put(dialogId, md);
onPrepareDialog(dialogId, md.mDialog, md.mArgs);
md.mDialog.onRestoreInstanceState(dialogState);
}
}
}
}
//Activity在onDestory中如何销毁对话框,当然就是销毁那个数组了
protected void onDestroy() {
// dismiss any dialogs we are managing.
if(mManagedDialogs !=null) {
final int numDialogs = mManagedDialogs.size();
for(int i = 0; i < numDialogs; i++) {
final ManagedDialog md = mManagedDialogs.valueAt(i);
if(md.mDialog.isShowing()) {
md.mDialog.dismiss();
}
}
mManagedDialogs =null;
}
……
}
}