Practical Android skills: Dialog

Source: Internet
Author: User

From: http://blog.csdn.net/hitlion2008/article/details/7567549

Dialog is a control that is required by any system. It is used as an auxiliary window to display some messages or to request the user to take an action.

It is no exception in Android. For more information, see the document.
Precautions for use

1. the back key can cancel the dialog box (dismiss), but it does not trigger its onokey and oncancel callback interfaces. Therefore, if your dialog box changes to some status, you must note that there is a third mode to cancel the dialog box.

2. try to use the modal dialog box (Model DIALOG) as little as possible, if the dialog. setcancellable (false) becomes a modal dialog box, except for dismiss in the program, which cannot be canceled by any key. This is a very poor user experience. The dialog box itself is a type of interference, and it will drive users crazy if they cannot be canceled. Therefore, the modal dialog box can be used unless necessary, that is, when an operation is executed without being broken.

3. Use the dialog box as few as possible. It is a disturbance to the user unless you need to perform operations or make a choice. Generally, toast or notification is enough for general notifications.

4. Do not use a dialog box-style activity, that is, convert the activity into a dialog box. This is a custom layout, which may be different from the system dialog style. The most serious problem is that when the system style changes, the sub-classes of dialog will change, but the activity-Style dialog box will not change. You can find the activity dialog box in ICS, and you will find that its OK is on the left, while the system dialog OK in ICS is on the right.

5. Make sure that the dialog object is within the lifecycle of the activity, that is, at most between oncreate () and ondestroy.

6. Think of and test the situation that the activity died before its dialog. Dismiss. Because the activity must be attached to an active activity instance, the activity instance must exist when it is displayed or canceled; otherwise, "illegalargumentexception: view not attached to Window Manager" is displayed ".

[Plain] view plaincopyprint?
05-15 02:45:26. 320: E/androidruntime (1161): Java. Lang. illegalargumentexception: view not attached to Window Manager
05-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. javastiveandroid. 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.
Comparison of Three usage modes

1. directly create a local dialog object

The advantage is that variables are easy to understand and maintain locally. The disadvantage is that the dialog object is difficult to control and runtimeexception is easily triggered.

2. Change the dialog object to the activity field.

The advantage is that the dialog object can be reused and the activity can be controlled to ensure that the dialog object is not displayed outside the activity lifecycle. Is recommended.

3. Use the oncreatedialog (), showdialog (), and dismissdialog () Methods of the activity ()

The advantage is that frameworks will take care of dialog, which is recommended in most cases. However, if the activity is killed in advance, this method must have a runtimeexception and cannot be avoided.
Instance

[Java] view plaincopyprint?
Public class dialogdemo extends activity {
Private Static final int dismiss_dialog = 1;

Private progressdialog mbetterdialog;

Private handler mmainhandler = new handler (){
@ Override
Public void handlemessage (Message MSG ){
Switch (msg. What ){
Case dismiss_dialog:
Dialog dialog = (DIALOG) msg. OBJ;
Dialog. Dismiss ();
Break;
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 dialog = new progressdialog (activity );
Dialog. settitle ("worst dialogging ");
Dialog. setmessage ("this is the worst dialogging scheme, never use it. This dialog is easy to" +
"Run out of its attached activity, yielding windowmanager # badtokenexception if the activity is gone when dismissing ");
Dialog. setindeterminate (true );
Dialog. setcancelable (true );
// You must do the show in main thread anyway
Dialog. Show ();
New thread (New runnable (){
Public void run (){
Systemclock. Sleep (10000 );
/*
* Illegalargumentexception: view not attached to Window Manager
* If the activity showing the dialog was killed before dismiss () out of rotation or locale changed,
* The dialog will gone with activity, but when dismiss () yields "illegalargumentexception: view not attached
* 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. Dismiss ();
//}
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 ("Better dialogging ");
Mbetterdialog. setmessage ("This dialogging can be used. The dialog object is a field of its activity, so activity can" +
"Control it to make sure dialog only lives within activity lifecircle ");
Mbetterdialog. setindeterminate (true );
Mbetterdialog. setcancelable (true );
// You must do the show in main thread anyway
Mbetterdialog. Show ();
New thread (New runnable (){
Public void run (){
Systemclock. Sleep (10000 );
/*
* This is much better, mbetterdialog is a field of its 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 runnable (){
Public void run (){
Systemclock. Sleep (10000 );
/*
* This way works best for most of time, should T if activity died before dismissing, exception must be
* Thrown: "illegalargumentexception: view not attached to Window Manager ".
* Although activity takes care of its belonging dialog, there is no way to operate it manually any more.
* First you do not have reference to dialog object and second, any 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 ("Optional dialogging ");
D. setmessage ("This dialogging scheme works best for most times, the dialogs are all taken care of by activitys and frameworks" +
". Counter t 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 its belonging dialogs shocould be dismissed, of course.
If (mbetterdialog! = NULL & mbetterdialog. isshowing ()){
Mbetterdialog. Dismiss ();
Mbetterdialog = NULL;
}
// For dialogs showed via showdialog (INT), no way to stop it in ondestroy ()
// Dismissdialog (0); // cause "illegalargumentexception: No dialog with ID 0 was ever shown via activity # showdialog"
// This is because activity has to manage its dialog during onpause () and onresume () to restore
// Dialogs 'State. So if you manually dismiss it in ondestroy (), it will cause je.

// Removedialog (0); // cause "illegalargumentexception: No dialog with ID 0 was ever shown via activity # showdialog", when
// Dismissing in thread.
// This is because activity has to manage its dialog during onpause () and onresume () to restore
// Dialogs 'State. So if you manually dismiss it in ondestroy (), it will cause je.
}
}

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.