Activitygroup window Bad token problem in-depth analysis

Source: Internet
Author: User

if reproduced, please declare the source: The Sands of Time: http://blog.csdn.net/t12x3456


Activitygroup window Bad token problem in-depth analysis

Recently help others debug a WebView page, very strange encountered the following problem:


The H5 page will detect the number of passes in the address, and if it detects a specific parameter exception, it will pop up dialog to prompt

Dialog:android.view.windowmanager$badtokenexception:


But in a separate test project there was no exception, and I noticed that the log included Localactivitymanager content. It was then found that it was only in tabhost that such a situation would occur.


Similarly, suppose we create dialog in Activitygroup or tabactivity if we use the following code

ProgressDialog = new ProgressDialog (xxx.this)

The creation will appear for example the following exception:
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.

Because of the new dialog box, the parameters content is specified as this, which is the content that points to the current child activity.

However, the child activity is created dynamically. There is no guarantee that there will always be.

The content of the parent activity is stable, so there is a workaround


If nested activitygroup in Activitygroup, how many getparent () should be used.

Why use GetParent we are able to understand from the internal mechanisms of Activitygroup:
The parent class of Tabactivity is Activitygroup, and Activitygroup's parent class is activity. So from the AMS point of view. Activitygroup is no different from normal activity, its life cycle includes standard Start,stop,resume,destroy and so on. And the system only agrees to agree with a activitygroup at the same time. But there is an important member variable inside the Activitygroup. Its type is Localactivitymanager, and the most important feature of this class is its ability to access the main class of the application process, the Activitythread class.

AMS initiates an activity or agrees that an activity is performed through the Activitythread class. The Localactivitymanager class means that it can load different activity and control the different states of the activity. Note that this is the load, not the start, which is very important. The so-called startup, usually refers to the creation of a process (assuming that the application often does not exist) to perform the activity, and load simply refers to the activity as a normal class to load, and create a class object, and no matter what the function of the class is not executed.

The process of loading the activity object is completely invisible to AMS, and the embedded activity only contributes to the window form it includes . The different states of the child activity are handled by Movetostate.

So the child activity is not like normal activity. It just provides the window. So when creating dialog, you should use GetParent to get activitygroup real activity, and be able to add dialog to the activity.

Suppose we encounter a similar exception to window Bak token, we can also proceed from this aspect to analyze the solution.


Activitygroup window Bad token problem in-depth analysis

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.