A bug
The first few days such a bug is a runtimeexception, the details are this:
Copy CodeThe code is 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 consistent with the dismiss dialog box, activity no longer exists. Often this kind of exception situation is, there is a time-consuming thread operation, need to display a progressdialog, at the beginning of the task to display a dialog box, and then when the task is completed and then dismiss dialog box, If during this period if the activity was killed for some reason and restarted, then when the dismiss WindowManager check that dialog belongs to the activity no longer exists, it will be reportedIllegalargumentexception:view not attached to window manager.
An important reason for this type of exception is that the ProgressDialog creation display and cancellation are all allowed in non-UI threads in the process. In Android, a non-UI thread is not allowed to manipulate UI-related things, such as adding a remove view, etc., but is it possible to create a display and Cancel dialog box? It is also possible to trigger this exception to cause application crash.
To avoid such exception, use the dialog box correctly and use the thread correctly.
Correct use of dialog boxes
do not use dialog boxes in non-UI threads to create, display, and cancel dialogs.
What about displaying a dialog box for an asynchronous operation? The activity has a corresponding action dialog callback such as Oncreatedialog (), ShowDialog (), Dimissdialog (), Removedialog (), and so on. These are all activity methods, so it is more convenient to use, do not show the creation and manipulation of dialog objects, everything is controlled by the framework, relatively safe.
It is also important to have the dialog object within the control of the activity and within the life cycle, for example, if it is a member variable, and to make the dialog variable active between the activity's OnCreate () and OnDestroy ().
Correct use of threads
try to minimize the use of separate threads, unless it is a real time-consuming operation to use threads, and threads do not directly use Java-style anonymous threads, unless it is a simple operation that does not need to do anything else after the operation is complete.
use Android to provide as many analogies as Asynctask. In addition, if you need to interact with the main thread during the threading process, it is a good idea to save the object of a thread and have the most control inside the thread, allowing the activity to manipulate the thread better.
If some operations are particularly time-consuming and frequent, 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.