Android Development Notes: Dialog's use of detailed _android

Source: Internet
Author: User
Tags advantage
Dialog is a control that any system must have, as a secondary window for displaying messages, or requesting a user to take an action.
Android is no exception, and basic use may refer to documentation.
Considerations when using
1. The back key can cancel out the dialog box (dismiss), but it will not trigger its Onokey and OnCancel callback interface, so if your dialog box will change some state, be sure to note that there is a third way to cancel the dialog box.
2. As little as possible modal dialog box (Model dialog), if the dialog.setcancellable (false), it becomes a modal dialog box, in addition to the program inside the dismiss, or press any key can not be canceled. This is a very bad user experience, the dialog box itself is a kind of interference, and can not be canceled will make the user crazy. So you can use modal dialogs unless you are particularly necessary, that is, if you do not want to be broken when performing an operation.
3. Use the dialog box as little as possible, it is a disturbance to the user, unless you need to do the operation, or make a choice. The usual general notice with toast or notification is sufficient.
4. Do not use dialog-style activity, that is, the activity into a dialog box. Because this is a custom layout, the style of dialog with the system may be inconsistent. Most seriously, when the system style changes, the dialog subclass changes, but the Activity dialog box doesn't change. You can find the activity dialog in ICS and you'll find that the OK is on the left, and the system dialog in ICS is on the right.
5. Try to ensure that the dialog objects live within the life cycle of the activity, and that is, at most, between OnCreate () and OnDestroy ().
6. To think about and test the death of an activity before its dialog.dismiss (). Because the activity must be attached to an activity instance that is being displayed, the activity instance must exist when it is displayed and canceled, otherwise there will be "Illegalargumentexception:view not attached" to Window manager.
Copy Code code as follows:

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.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 invoked on any thread.
Three methods of use comparison
1. Create a local dialog object directly
The advantage is that variables are local and easy to understand and maintain. The disadvantage is that the dialog object is difficult to control and easy to trigger runtimeexception.
2. The domain that turns dialog objects into activity
The advantage is that the dialog object can be reused and the activity can be controlled to ensure that the dialog does not appear outside the activity lifecycle. Is the recommended way to use.
3. By means of activity oncreatedialog (), ShowDialog () and DismissDialog ()
The advantage is that frameworks will help look after dialog, and in most cases this is recommended practice. But for an activity to die prematurely, this method must be runtimeexception and unavoidable.
Instance
Copy Code code as follows:

public class Dialogdemo extends activity {
private static final int dismiss_dialog = 1;

Private ProgressDialog Mbetterdialog;
Private Handler Mmainhandler = new Handler () {
@Override
public void Handlemessage (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 are the worst dialogging scheme, NEVER use it." This dialog are easy to "+
"Run out of it attached activity, yielding windowmanager#badtokenexception if the activity is gone when dismissing");
Dialog.setindeterminate (TRUE);
Dialog.setcancelable (TRUE);
You must doing 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 is killed before dismiss () out of rotation or locale changed,
* The dialog would gone with an activity, but when 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 no 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 it activity, so activity can "+
"Control it to make sure dialog only lives within Activity lifecircle");
Mbetterdialog.setindeterminate (TRUE);
Mbetterdialog.setcancelable (TRUE);
You must doing the show in main thread anyway
Mbetterdialog.show ();
New Thread (New Runnable () {
public void Run () {
Systemclock.sleep (10000);
/*
* This are much better, mbetterdialog are a field of its activity, so activity can take care the 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, except if activity died before dismissing, exception must is
* Thrown: "Illegalargumentexception:view not attached to Window manager".
* Although activity takes care's its belonging dialog, there are no way to operate it manually any more.
* 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 is dialogging scheme works best for most times, the dialogs are all taken care of by Activitys and Framewo Rks "+
". Except for activity being killed during dialog ");
D.setindeterminate (TRUE);
D.setcancelable (TRUE);
return D;
}
@Override
protected void OnDestroy () {
Super.ondestroy ();
The activity are dying, all their belonging dialogs should 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 is ever shown via Activity#showdialog"
This is because activity has to manage it dialog during OnPause () and Onresume () to restore
Dialogs ' state. So if you are manually dismiss it in OnDestroy (), it'll cause JE.

Removedialog (0);//Cause "Illegalargumentexception:no dialog with ID 0 is ever via shown" when
Dismissing in thread.
This is because activity has to manage it dialog during OnPause () and Onresume () to restore
Dialogs ' state. So if you are manually dismiss it in OnDestroy (), it'll cause JE.
}
}

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.