Discussion: Are you really going to use Android dialog? _android

Source: Internet
Author: User

A bug
The last few days such a bug is a runtimeexception, the details are as follows:

Copy Code code as follows:

Java.lang.IllegalArgumentException:View not attached to window manager
At android.view.WindowManagerImpl.findViewLocked (windowmanagerimpl.java:356)
At Android.view.WindowManagerImpl.removeView (windowmanagerimpl.java:201)
At Android.view.window$localwindowmanager.removeview (window.java:400)
At Android.app.Dialog.dismissDialog (dialog.java:268)
At android.app.dialog.access$000 (dialog.java:69)
At Android.app.dialog$1.run (dialog.java:103)
At Android.app.Dialog.dismiss (dialog.java:252)
At Xxx.onpostexecute (xxx$1.java:xxx)

The first is Google, and found that the cause of this is basically the same as the dismiss dialog box, the activity no longer exists. Often this type of exception occurs when a time-consuming threading operation needs to display a ProgressDialog, display a dialog at the beginning of the task, and then dismiss the dialog box when the task is completed. If, during this period, the activity is killed and restarted for some reason, then when the dismiss WindowManager check that the activity that dialog belongs to does not exist, it will report Illegalargumentexception:view not attached to window manager.
One important reason for this type of exception is that both the ProgressDialog creation display and cancellation allow processes in non-UI threads. In Android, non-UI threads are not allowed to manipulate UI-related things, such as adding a remove view, but are you allowed to create a display and Cancel dialog box? It is also possible to cause this exception to result in the application of crash.
To avoid this type of exception, you need to use the dialog box correctly and use the thread correctly.
Correct use of dialog boxes
Do not use dialog boxes to create, display, and Cancel dialog boxes in non-UI threads.
So what about displaying a dialog box for an asynchronous operation? Activity has a corresponding Action dialog box callback such as Oncreatedialog (), ShowDialog (), Dimissdialog (), Removedialog (), and so on. These are all activity methods, so it is more convenient to use, do not have to show the creation and manipulation of dialog objects, everything is controlled by the framework, relatively safe.
It is also important to make the dialog object within the control of the activity and within the lifecycle, such as its member variables, and to allow dialog variables to be active between the OnCreate () and OnDestroy () of the activity.
Proper use of threads
As little as possible with separate threads, unless the real time-consuming operation of the thread, the thread does not use the Java-style anonymous thread, unless it is the simple operation, the operation does not need to do anything else.
Use Android as much as possible to provide analogies such as Asynctask. In addition, if you need to interact with the main thread during threading, it is best to keep the object of a thread and have the most control within the thread, which allows the activity to manipulate the thread better.
If some operations are particularly time-consuming and are frequently done, such as getting data from the network, or reading files from the background, or importing/exporting, restoring/backing up, it is best to put them in the background service, and then give the corresponding progress in the StatusBar.

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.