Fragment Traps in Android

Source: Internet
Author: User

Previously done a project, fragment nesting High German map, when again into the fragment, there will be strange phenomenon. Nested maps will appear slippery situation, at first thought it was a gold bug, after a study, finally determined that this is a pit.

First to fragment do a simple introduction, borrow csdn on a piece of content written by friends.

Android introduced the concept of fragments in 3.0, with the main purpose being to use on large-screen devices, such as tablets, to support more dynamic and flexible UI design. The screen of a tablet computer is much larger than that of a mobile phone, and there is more room for more UI components, and there is more interaction between these components. Fragment allows such a design without the need for you to manage Viewhierarchy's complex changes in person. By dispersing the layout of the activity into fragment, you can modify the appearance of the activity at run time and save those changes in the back stack managed by the activity.

The students who have used fragment know that it is quite simple to use, basically the same as in the activity, the life cycle function is a little bit more. Today is not about how fragment is used, but about a pit in a fragment. Quite a few apps now have the following three design styles.

One, the bottom tab, General 3-5, click on a tab, the top page also with the switch, such as QQ, Tencent Weibo, Sina micro-blog. Early on, Android developers will use Tabactivity to achieve, but tabactivity have some problems, also does not conform to the andoid of the Single Window design principles, so has been abandoned, not recommended to continue to use. If you have business needs, please give priority to using Fragement.

second, the top of the tab, is generally about 3-5, relative to the bottom tab style, the Top tab will generally introduce Viewpager + fragment implementation, so you can switch around, such as micro-letter.

Third, the side tab, that is, Slidingmenu + Fragment or Menudrawer + Fragment. This style since last year, swept out of control, a period of time before dry goods sharing is this, the code is also open source in the GitHub, interested friends can download to understand under. Go out to the right, view the historical news "a cool project interface sharing, dry goods."

These three styles used in the fragment, generally very easy to be overlooked by everyone a problem. That is, when the view hierarchy associated with fragment is being removed, the OnDestroy () method is not executed, but Ondestroyview () is invoked. such as the above style two Viewpager + Fragment, by default when Viewpager sliding to the third page, the first page of the Fragment will be executed Ondestroyview, when sliding again to the second page, The first page of the fragment Oncreateview again executes the drawing page. The accompanying problem is that the member variable has to be assigned again, and the hard time to load the page to reload again, so that the memory will add no meaning to the pressure, the user experience is also not friendly, especially in the case of a network request, such as the long time overhead.

Is there any solution to the above? The answer is yes. The execution of Ondestroyview is not the same as the ondestroy of the activity, and the current page is not destroyed, so references to all the member variables of fragment are still in. That's good to do, we in the Oncreateview, the first to determine whether the data is empty, such as the root view of the fragment Rootview, network requests to obtain data, etc., if not empty will not be executed again. This also avoids the existence of the above mentioned problems.

Back to the column page: http://www.bianceng.cnhttp://www.bianceng.cn/OS/extra/

But the point to note is that if you reuse Rootview, be sure to remember to remove the Rootview in Ondestroyview, because a view that already has a parent layout cannot be added to another new parent layout. The code is as follows

This also solves the most before I encountered the bug, because each entry will create a mapview, many maps stacked together, so there is a "slippery" strange phenomenon.

Sharing today is only an optimization strategy, I hope to help you.

Author: cnblogs Jackcho

Related Article

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.