In Android, how does one process the Dialog box Dialog in the Activity source code? androiddialog

Source: Internet
Author: User

In Android, how does one process the Dialog box Dialog in the Activity source code? androiddialog

For Android, if you leave the Activity, you will not be able to talk about development. Are you familiar with the Implementation Principles of the Activity? I am not familiar with the source code. Next, let's take a look at the source code of the Activity according to the old rules. This article mainly explains how the Activity manages the Dialog box Dialog in a unified manner.

First, we can see a class like this:

 private static class ManagedDialog {        Dialog mDialog;        Bundle mArgs;    }    private SparseArray<ManagedDialog> mManagedDialogs;

Obviously, ManagedDialog is a data model defined in Android that manages Dialog. The member variables in it are very simple. A Dialog variable and a variable that stores various states of Dialog, bundle type mArgs. At the same time, we can also see this variable in the Activity, mManagedDialogs, which is regarded as a HashMap <Integer, ManagedDialog> type, of course, the reason why HashMap is not used is that the efficiency is higher than HashMap.


Next, we need to look at the restoreManagedDialogs method, that is, restoring the managed Dialog status.

 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 = new SparseArray<ManagedDialog>(numDialogs);        for (int i = 0; i < numDialogs; i++) {            final Integer dialogId = ids[i];            Bundle dialogState = b.getBundle(savedDialogKeyFor(dialogId));            if (dialogState != null) {                final ManagedDialog md = new ManagedDialog();                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);                }            }        }    }
First, obtain the Bundle object and the array of the Dialog id stored in the Bundle object. Create a collection of SparseArray <ManagedDialog> List objects. Through the for loop iteration, create a new Dialog and restore the stored data.

When a Dialog is created, it is called back to the following method body:

private Dialog createDialog(Integer dialogId, Bundle state, Bundle args) {        final Dialog dialog = onCreateDialog(dialogId, args);        if (dialog == null) {            return null;        }        dialog.dispatchOnCreate(state);        return dialog;    }
In actual Activity source code, onCreateDialog is empty. That is to say, we need to re-write the current method in our own defined Activity to create our Dialog, the onCreate method of Dialog is triggered here.


With the previous recovery, the current data storing the Dialog status must exist:

private void saveManagedDialogs(Bundle outState) {        if (mManagedDialogs == null) {            return;        }        final int numDialogs = mManagedDialogs.size();        if (numDialogs == 0) {            return;        }        Bundle dialogState = new Bundle();        int[] ids = new int[mManagedDialogs.size()];        for (int i = 0; i < numDialogs; i++) {            final int key = mManagedDialogs.keyAt(i);            ids[i] = key;            final ManagedDialog md = mManagedDialogs.valueAt(i);            dialogState.putBundle(savedDialogKeyFor(key), md.mDialog.onSaveInstanceState());            if (md.mArgs != null) {                dialogState.putBundle(savedDialogArgsKeyFor(key), md.mArgs);            }        }        dialogState.putIntArray(SAVED_DIALOG_IDS_KEY, ids);        outState.putBundle(SAVED_DIALOGS_TAG, dialogState);    }

At the same time, the onDestroy method of the Activity will destroy all the previously managed Dialog.

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;        }

The for loop iterates into the row, and finally assigns a null pointer to the reference of the Dialog set.

The management of the Dialog in the Activity is mentioned here, and we hope you can gain some benefits.







Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.