Android Combat tips: Dialog

Source: Internet
Author: User

Ext.: http://blog.csdn.net/hitlion2008/article/details/7567549#t0

Dialog is a control that any system must have, as a secondary window, to display some messages, or to request a user to take a trigger action, and so on.

It is no exception in Android, and the basic use may refer to the documentation.

Precautions for use

1. The back key can cancel the dialog (dismiss), but it will not trigger its Onokey and OnCancel callback interface, so if your dialog box will change some state, it is important to note that there is a third way to cancel the dialog box.

2. Use the modal dialog box (Model dialog) as sparingly as possible, and if dialog.setcancellable (false), it becomes a modal dialog box that, in addition to dismiss inside the program, cannot be canceled by pressing any key. This is a very poor user experience, the dialog itself is a kind of interference, and can not be canceled will make the user crazy. So you can use modal dialogs unless it is particularly necessary to do something that you do not want to be broken.

3. Minimize the use of dialogs, which is a distraction to the user, unless the user is required to do the operation, or make a choice. The usual general notice is enough with toast or notification.

4. Do not use dialog-style activity, which turns activity into a dialog box. Because this is a self-defined layout, the style of dialog with the system may be inconsistent. The most serious is that when the system style changes, the subclass of dialog changes, but the activity-like dialog box does not change. You can find the Activity dialog box in ICS and you'll find that it's OK on the left and the system dialog OK in ICS is on the right.

5. Try to ensure that dialog objects live within the life cycle of the activity, that is, at most between OnCreate () and OnDestroy ().

6. Think about and test the situation where the activity died before its dialog.dismiss (). Because activity must be attached to an activity instance that is being displayed, its activity instance must exist when displayed and canceled, or there will be "illegalargumentexception:view not attached to Window manager.

05-15 02:45:26.320:e/androidruntime (1161): Java.lang.IllegalArgumentException:View not attached to window Manager05-15 02:45:26.320:e/androidruntime (1161): at android.view.WindowManagerImpl.findViewLocked ( windowmanagerimpl.java:355) 05-15 02:45:26.320:e/androidruntime (1161): at     Android.view.WindowManagerImpl.removeView (windowmanagerimpl.java:200) 05-15 02:45:26.320:e/androidruntime (1161):     At Android.view.window$localwindowmanager.removeview (window.java:432) 05-15 02:45:26.320:e/androidruntime (1161): At Android.app.Dialog.dismissDialog (dialog.java:278) 05-15 02:45:26.320:e/androidruntime (1161): at android.app.dialog.access$000 (dialog.java:71) 05-15 02:45:26.320:e/androidruntime (1161): at android.app.Dialog$1. Run (dialog.java:111) 05-15 02:45:26.320:e/androidruntime (1161): at Android.app.Dialog.dismiss (dialog.java:268) 05-15 02:45:26.320:e/androidruntime (1161): at Com.hilton.effectiveandroid.app.dialogdemo$1.handlemessage ( dialogdemo.java:26) 05-15 02:45:26.320:e/androidruntime (1161): at Android.os.Handler.dispatchMessage (handler.java:99) 

7. Dialog.show () must be called in the main thread, but Dialog.dismiss () can be called in any thread.

three ways to use comparison

1. Create a local dialog object directly

The advantage is that variables are locally easy to understand and maintain. The disadvantage is that dialog objects are difficult to control and trigger runtimeexception easily.

2. Turn the dialog object into an activity domain

The advantage is that the dialog object can be reused, and activity can be controlled to ensure that the dialog does not appear outside the activity life cycle. Is the recommended way to use.

3. The Activity Method Oncreatedialog (), ShowDialog () and DismissDialog ()

The advantage is that frameworks will help take care of dialog, which is recommended in most cases. However, for activity to die prematurely, this method must be runtimeexception, and can not be avoided.

Example

public class Dialogdemo extends Activity {private static final int dismiss_dialog = 1;    Private ProgressDialog Mbetterdialog;    Private Handler Mmainhandler = new Handler () {@Overridepublic void Handlemessage (Message msg) {switch (msg.what) {    Case Dismiss_dialog:dialog DIALOG = (DIALOG) Msg.obj;dialog.dismiss ();    Default:break;    }}    }; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.dialog_demo), final Button sucking = (Button) Findviewbyid (r.id.sucking); Sucking.setonclicklistener (new View.onclicklistener () {public void OnClick (View v) {Final activity activity = dialogdemo.this;final ProgressDialog di Alog = new ProgressDialog (activity);d ialog.settitle ("worst dialogging");d ialog.setmessage ("This is the worst Dialogging scheme, never use it. This dialog are easy to "+" run out of its attached activity, yielding windowmanager#badtokenexception if the activity is G One when dismissing ");d ialog.setindeterminate (True);d ialog.setcancelable (TRUE);//You must does the show in main thread anywaydialog.show (); New Thread (New Runnable () {public void run () {systemclock.sleep (10000);/* * Illegalargumentexception:view not Atta  Ched to Window Manager * If the activity showing the dialog is killed before dismiss () out of rotation or locale changed,  * The dialog would gone with activity, if dismiss () yields "illegalargumentexception:view not attached to * window Manager ". * Checking isshowing () won ' t help. * Checking activity.isfinishing () won ' t help, either. * Dismiss it in main thread also won ' t give any help. *///this WON ' t work//if (dialog.isshowing ()) {//Dialog.dismiss ();//}//if (!activity.isfinishing ()) {//Dialog.dism    ISS ();//}message msg = Message.obtain (); msg.what = Dismiss_dialog;msg.obj = Dialog;mmainhandler.sendmessage (msg);    }}). Start (); }}); final Button better = (Button) Findviewbyid (r.id.better); Better.setonclicklistener (New View.onclicklisTener () {public void OnClick (View v) {mbetterdialog = new ProgressDialog (dialogdemo.this); Mbetterdialog.settitle ("Bett ER dialogging "); Mbetterdialog.setmessage (" This dialogging can be used. The dialog object is a field's activity, so activity can ' + ' control it to make sure dialog only lives within Activit Y lifecircle "); Mbetterdialog.setindeterminate (true); Mbetterdialog.setcancelable (true);//You must does the show in main Thread Anywaymbetterdialog.show (); new Thread (new Runnable () {public void run () {systemclock.sleep (10000);/* * This is  Much better, Mbetterdialog is a field's activity, so activity can take care of it in order * to make sure dialog only Live within activity ' s life circle, to avoid any unexpected exceptions. *///This really works if (mbetterdialog! = null && mbetterdialog.isshowing ()) {Mbetterdialog.dismiss ()    ;    }}). Start (); }}); final Button optional = (Button) Findviewbyid (r.id.optional); Optional.setonclicklistener (new View).Onclicklistener () {@SuppressWarnings ("deprecation") public void OnClick (View v) {showDialog (0); new Thread (New Runna BLE () {public void run () {systemclock.sleep (10000);/* * This is the same as works for the most of time, except if activity died Before dismissing, exception must be * thrown: ' Illegalargumentexception:view not ' attached to Window manager '. * Although activity takes care of their belonging dialog, there is no-to-operate it manually any more. * First reference to dialog object and second, no manual operation only interferences * and breaks state Maintained by frameworks.    */dismissdialog (0);    }}). Start ();    }}); } @Override protected Dialog oncreatedialog (int id) {ProgressDialog d = new ProgressDialog (this);d. Settitle ("Optiona L dialogging ");d. Setmessage (" This dialogging scheme works with most times, the dialogs is all taken care of by Activi Tys and Frameworks "+". Except for activity being killed during dialog showing ");d. Setindeterminate (true);d. Setcancelable (True); return D; } @Override protected void OnDestroy () {Super.ondestroy ();//Activity is dying, all it belonging dialogs should be    Dismissed, of course.if (mbetterdialog! = null && mbetterdialog.isshowing ()) {Mbetterdialog.dismiss (); Mbetterdialog = null;} For dialogs showed via ShowDialog (int), no-to-stop it in OnDestroy ()//dismissdialog (0); Cause "Illegalargumentexception:no dialog with id 0 were ever shown via Activity#showdialog"//This is because ACTI Vity have to manage it dialog during OnPause () and Onresume () to restore//dialogs ' state. So if you manually dismiss it in OnDestroy (), it'll cause je.//removedialog (0);//Cause "Illegalargumentexception:no di               Alog with id 0 is ever shown via Activity#showdialog ", when//dismissing in thread. This is because Activity have to manage it dialog during OnPause () and Onresume () to restore/dia Logs ' state. So if you ManuaLly dismiss it in OnDestroy (), it'll cause JE. }}

Android Combat Skills: Dialog (Turn)

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.