![]() "Only the original thread that created a view hierarchy can touch its views.") Īs you can see from the method reference, the ViewRootImpl#checkThread method is called in almost all view update methods to prevent multi-threaded UI operations. Throw new CalledFromWrongThreadException( 3.2 Where does CalledFromWrongThreadException come fromĪs we all know, when we update the interface element, if it is not executed on the main thread, the system will throw CalledFromWrongThreadException, observe the exception stack, it is not difficult to find that the exception is thrown from the ViewRootImpl#checkThread method. : Relative concept, any other thread is a sub-thread relative to the main thread. : The thread that instantiates ViewRootImpl, and finally executes the thread that involves UI operations such as View's onMeasure/onLayout/onDraw. : The thread that instantiates the ActivityThread, and each Activity instantiates the thread. Next, we analyze the feasibility principle of the second and third options. ![]() Although the "UI thread" can be understood as the "main thread" most of the time, in the strict sense, the "UI thread" has never been defined in the Android source code as the "main thread". : Can the instantiation, display, and interaction of the entire Snackbar be placed in sub-threads for execution?Ĭonclusion: Yes, but there are some constraints. The view operation before attach, strictly speaking, is not a UI operation, but a simple property assignment. : Can the time-consuming operations (such as instantiation, infalte) of the pop-up window be moved to the sub-thread to run, and only executed in the main thread during the display phase (calling the show method)?Ĭonclusion: Yes. : Dynamically set the delayed instantiation and display time of the pop-up window to avoid business dynamic effects.Ĭonclusion: It works, but it's not elegant enough. Through our optimization, we can finally achieve the perfect effect, with smooth motion and non-interference: We know that the Android coding specification requires that child threads cannot manipulate the UI, but must this be the case? Therefore, we need to find a solution to solve the stuttering problem caused by the conflict of motion effects. ![]() The root cause of this problem can be briefly described as: uncontrollable dynamic conflict (business randomness) + main thread time-consuming method that cannot be placed (pop-up window instantiation, view infalte). We simulate the ongoing motion effects of the App (such as page switching) with horizontally moving blocks it can be seen that when the Snackabr pop-up window pops up, the block motion is obviously stuck (moving to about 1/3). When the pop-up window pops up, if the app happens to also have animation effects, the main thread may draw two animation effects at the same time, which will cause a freeze, as shown in the figure below. Due to the requirements of the business scenario, for the App, the popup timing of the SDK's popup window is random. Background and ProblemsĪ certain SDK has PopupWindow and dynamic effects. Author: vivo Internet front-end team - Zhang Xichen 1.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |