In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces what the traps of Fragment are, which can be used for reference by interested friends. I hope you will gain a lot after reading this article. Let's take a look at it.
A previous project, Fragment nesting Amap, when re-entering the Fragment, there will be a strange phenomenon. The nested map will slide and remain motionless. At first, I thought it was Gaud's bug. After some research, it was finally determined that it was a pit.
First make a brief introduction to Fragment, borrow a paragraph written by a friend on csdn.
Android introduced the concept of fragments in 3.0. the main purpose is to support more dynamic and flexible UI designs for large-screen devices, such as tablets. Tablets have a much larger screen than phones, there is more room for more UI components, and there is more interaction between them. Fragment allows such a design without having to manage the complex changes in viewhierarchy yourself. By spreading the layout of activity into fragment, you can modify the appearance of activity at run time and save those changes in the back stack managed by activity.
Students who have used Fragment know that it is quite simple to use, basically the same as in Activity, but there are a little more lifecycle functions. Today is not about how to use Fragment, but about a pit in a Fragment. Quite a few APP now have the following three design styles.
First, there are usually 3-5 TAB at the bottom. Click on a TAB, and the upper page will also switch, such as QQ, Tencent Weibo, and Sina Weibo. In the early days, Android developers would use TabActivity to implement it, but TabActivity has some problems and does not conform to the principle of Andoid's single window design, so it has been abandoned and is not recommended. If you have business needs, please give priority to using Fragement.
Second, the top TAB is generally about 3-5. Compared with the bottom TAB style, the top TAB generally introduces the implementation of ViewPager + Fragment, which can be switched between left and right, such as Wechat.
Third, the side TAB, that is, SlidingMenu + Fragment or MenuDrawer + Fragment. This style has been out of control since last year. This is the issue of dry information sharing some time ago, and the code is also open source on Github. Interested friends can download and understand it. Turn right when you go out and check the historical news: "A cool project interface to share, full of practical information".
The Fragment used in the above three styles is generally easy to overlook a problem. That is, when the view hierarchy associated with Fragment is being removed, the onDestroy () method is not executed, but onDestroyView () is called. For example, the above style of two viewers + Fragment, by default, when the ViewPager slides to the third page, the Fragment of the * * page will execute onDestroyView, and when it slides to the second page again, the onCreateView of the Fragment of the * * page will re-perform drawing the page. The concomitant problem is that the member variables have to be re-assigned, and the hard and time-consuming pages have to be reloaded again, which adds meaningless pressure to memory and is not very friendly in the user experience. especially in the case of long overhead such as network requests.
Is there any above solution? The answer is yes. Unlike Activity's onDestroy, the execution of onDestroyView does not destroy the current page, so references to all member variables of Fragment are still there. That's easy. When we are in onCreateView, we first determine whether the data to be fetched is empty, such as the root view rootView of Fragment, the data obtained by network requests, and so on. If it is not empty, we do not have to execute it again. In this way, the problems mentioned above can be avoided.
But one thing to note is that if you reuse rootView, be sure to remove rootView from onDestroyView first, because View that already has a parent layout cannot be added to another new parent layout. The code is as follows
This also solves the bug that I encountered before, because each entry will create a MapView, many maps are stacked on top of each other, so there is a strange phenomenon of "slippery".
Thank you for reading this article carefully. I hope the article "what are the traps of Fragment" shared by the editor will be helpful to you. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.