Children's shoes, for Android, leaving the activity, basically there is no way to talk about development, then the pro-activity is familiar with the principle of implementation? Anyway, I did not look at the source code before is not familiar with, below, we together according to the same ritual, together to see the source of activity. This article mainly explains how the activity of the dialog box dialog to achieve unified management.
The first thing we see is such a class:
private static class Manageddialog { Dialog mdialog; Bundle Margs; } Private sparsearray<manageddialog> mmanageddialogs;
Obviously, Manageddialog is a data model for managing dialog defined in Android, where the member variables are simple, a dialog variable, a variable that stores the various states of dialog, and the type of bundle Margs. At the same time in the activity, we can also see such a variable, mmanageddialogs, we think of this variable as a hashmap<integer,manageddialog> type, Of course, there is no such data structure using HASHMAP, the document says efficiency is higher than hashmap.
The next thing we need to look at is restoremanageddialogs, which means recovering the state of the managed dialog.
private void Restoremanageddialogs (Bundle savedinstancestate) {final bundle B = Savedinstancestate.getbundle (SAVE D_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 get the bundle object, get an array of the ID of the dialog stored in the bundle object. Creates a collection of Sparsearray<manageddialog> list objects. With the For loop iteration, a new dialog is created through the stored data, which is restored.
The creation of the dialog will be recalled to the following method body:
Private Dialog createdialog (Integer dialogID, bundle State, bundle args) { final Dialog Dialog = Oncreatedialog (Dialog Id, args); if (dialog = = null) { return null; } Dialog.dispatchoncreate (state); return dialog; }
We will find that the actual source of activity, oncreatedialog inside is empty, that is, we need to define their own activity to rewrite the current method, create our dialog, This will then trigger the OnCreate method to dialog.
With the previous recovery, there is bound to be data that now stores the state of the dialog:
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, in the OnDestroy method of activity, all the dialog currently under management will be destroyed.
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, and finally assigns a null pointer to the collection reference of the dialog.
The management of the dialog in the activity is here, I hope you can have something to gain.
How to handle the dialog dialog in the activity source code of Android