Recently to help others debug a WebView page, it is strange to encounter the following problems:
The parameters passed in the address are detected in the H5 page, and if a specific parameter exception is detected, the dialog will be prompted
Dialog:android.view.windowmanager$badtokenexception:
But in a separate test project there was no exception, and I noticed that the log contained Localactivitymanager content, and then found that it was only in tabhost that the situation would occur.
In a similar situation, if we create dialog in Activitygroup or tabactivity, use the following code
ProgressDialog = new ProgressDialog (xxx.this)
The following exception will appear when creating:
05-24 12:34:42.236:error/androidruntime (6362): android.view.windowmanager$badtokenexception:unable to add Window-- token [email protected] is not valid; Is your activity running?
The main reason for this problem is that the context selection error occurs when the UI interface is displayed.
Since the new dialog box, the content of the parameter is specified as this, which is the content that points to the current child activity. However, the child activity is created dynamically and cannot be guaranteed to persist. The content of the parent activity is stable, so there are the following solutions
If nested activitygroup in Activitygroup, how many getparent () should be used.
Why use GetParent we can understand from the internal mechanism of Activitygroup:
The parent class of Tabactivity is Activitygroup, and Activitygroup's parent class is activity. So from the AMS point of view, activitygroup and ordinary activity is no different, its life cycle includes the standard Start,stop,resume,destroy and so on, Moreover, only one activitygroup is allowed in the system. But there is an important member variable inside the Activitygroup, which is of type Localactivitymanager, and the greatest feature of this class is that it can access the main class of the application process. That is, the Activitythread class. When AMs starts an activity or endorses an activity that is performed through the Activitythread class, the Localactivitymanager class means that it can be used to load different activity. and control the different states of the activity. Note that this is the load, not the start, which is important. The so-called startup, generally refers to the creation of a process (if the application is often not present) to run the activity, and the load simply refers to the activity as a normal class to load, and create a class of the object, and any function of the class is not run. The process of loading the activity object is completely invisible to AMS, and the embedded activity only contributes to the window windows it contains . The different states of the child activity are handled by Movetostate.
So the child activity is not like normal activity, it just provides window, so when creating dialog, you should use GetParent to get activitygroup real activity, Before adding dialog to the activity.
If you encounter a similar window Bak token exception, we can also proceed from this aspect to analyze the solution.