Android basic knowledge 30: Relationship between dialog02 -- showdialog (), onpreparedialog () and oncreatedialog ()

Source: Internet
Author: User

Activities provides a convenient dialog box mechanism for management, such as oncreatedialog (INT), onpreparedialog (INT, DIALOG), showdialog (INT), and dismissdialog (INT) if you use these methods, the activity returns the dialog box managed by the activity through the getowneractivity () method ).

Oncreatedialog (INT): When you use this callback function, the android system will effectively set this activity as the owner of each dialog box, so as to automatically manage the status of each dialog box and link it to the activity. In this way, each dialog box inherits the specific attributes of this activity. For example, when a dialog box is opened, the menu key displays the option menu defined for the activity, and the volume key modifies the audio stream used by the activity.

Showdialog (INT): When you want to display a dialog box, call the showdialog (int id) method and pass an integer that uniquely identifies the dialog box. When the dialog box is requested for the first time, Android calls oncreatedialog (int id) from your activity. You should initialize the dialog box dialog here. This callback method is passed to the same ID as showdialog (int id. After you create this dialog box, return this object at the end of the activity.

Onpreparedialog (INT, DIALOG): Before the dialog box is displayed, Android also calls the optional callback function onpreparedialog (int id, DIALOG ). if you want to change any of its attributes when each dialog box is opened, you can define this method. This method is called each time a dialog box is opened, while oncreatedialog (INT) is called only when the dialog box is opened for the first time. If you do not define onpreparedialog (), this dialog box will remain the same as the previous one. This method is also passed with the dialog box ID and the dialog box object created in oncreatedialog.

Dismissdialog (INT): when you are about to close the dialog box, you can call dismiss () to delete it. If needed, you can also call the dismissdialog (int id) method from this activity, which will actually call the dismiss () method for this dialog box. If you want to use the oncreatedialog (int id) method to manage the status of your dialog box (as discussed in the previous chapter), and each time your dialog box is cleared, the status of the dialog box objects will be retained by the activity. If you decide that you no longer need this object or clear the state, you should call removedialog (int
ID ). This will delete any internal object reference. If this dialog box is displayed, it will be removed.

----> Showdialog () calls createdialog () and onpreparedialog (). createdialog () calls oncreatedialog ().
Project source code:

Package COM. bn. ex2s; import android. app. activity; import android. app. dialog; import android. app. progressdialog; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. util. log; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; public class sample2_19_activity extends activity {final int progress_dialog = 0; Final int increase = 0; Final int max_counter = 100; progressdialog PD; handler HD; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); button Bok = (button) This. findviewbyid (R. id. button01); Bok. setonclicklistener (New onclicklistener () {@ override public void onclick (view v) {showdialog (progress_dialog) ;}}); Hd = new handler () {@ override public void handlemessage (Message MSG) // This method must be rewritten to receive data {super. handlemessage (MSG); Switch (MSG. what) {Case increase: PD. incrementprogressby (1); // Add 1 If (PD. getprogress ()> = max_counter) {PD. dismiss () ;}break ;}};}@ override public dialog oncreatedialog (int id) {Switch (ID) {Case progress_dialog: Pd = new progressdialog (this); PD. setmax (max_counter); // sets the maximum value of PD. setprogressstyle (progressdialog. style_horizontal); PD. settitle (R. string. title); // set the title PD. setcancelable (false); // you cannot use the rollback button to close the log in the set progress dialog box. D ("KKK", "KKK"); break;} return PD ;} @ override // The method public void onpreparedialog (int id, dialog DIALOG) {super. onpreparedialog (ID, DIALOG); Switch (ID) {Case progress_dialog: PD. incrementprogressby (-PD. getprogress (); // The Progress of the dialog box is cleared. New thread () {public void run () {While (true) {HD. sendemptymessage (Increase); If (PD. getprogress ()> = max_counter) {break;} Try {thread. sleep (40);} catch (exception e) {e. printstacktrace ();}}}}. start (); break ;}}}

Android SDK source code:

public final void showDialog(int id) {        showDialog(id, null);}public final boolean showDialog(int id, Bundle args) {        if (mManagedDialogs == null) {            mManagedDialogs = new SparseArray<ManagedDialog>();        }        ManagedDialog md = mManagedDialogs.get(id);        if (md == null) {            md = new ManagedDialog();            md.mDialog = createDialog(id, null, args);            if (md.mDialog == null) {                return false;            }            mManagedDialogs.put(id, md);        }               md.mArgs = args;        onPrepareDialog(id, md.mDialog, args);        md.mDialog.show();        return true;} 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;}

References:
Showdialog (), onpreparedialog (), and oncreatedialog ()

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.