Andriod development skills-Fragment lazy loading and andriod development skills

Source: Internet
Author: User

Andriod development skills-Fragment lazy loading and andriod development skills

During application development, a single Activity may be used in combination with viewpager (or other containers) and multiple Fragment. If each fragment needs to load data, if the activity is loaded locally or from the network, a large amount of resources need to be initialized when the activity is created. Of course we are not satisfied with this result. So, can we initialize it only when switching to this fragment?

The answer is in the setUserVisibleHint method in Fragment. See the API documentation for this method in Fragment (domestic image address: http://dd.ma/nvSZLKjU ):

Set a hint to the system about whether this fragment's UI is currently visible to the user. This hint defaults to true and is persistent across fragment instance state save and restore.An app may set this to false to indicate that the fragment's UI is scrolled out of visibility or is otherwise not directly visible to the user. This may be used by the system to prioritize operations such as fragment lifecycle updates or loader ordering behavior.ParametersisVisibleToUsertrue if this fragment's UI is currently visible to the user (default), false if it is not.

This method is used to tell the system whether the Fragment UI is visible. Therefore, we only need to inherit Fragment and override this method to load data when fragment is visible, that is, Fragment lazy loading.

The Code is as follows:

/** Date: 14-7-17 * Project: Access-Control-V2 */package cn. irains. access_control_v2.common; import android. support. v4.app. fragment;/*** Author: msdx (645079761@qq.com) * Time: 14-7-17 */public abstract class LazyFragment extends Fragment {protected boolean isVisible; /*** implements Fragment data loading. * @ param isVisibleToUser */@ Override public void setUserVisibleHint (boolean isVisibleToUser) {super. setUserVisibleHint (isVisibleToUser); if (getUserVisibleHint () {isVisible = true; onVisible () ;}else {isVisible = false; onInvisible () ;}} protected void onVisible () {lazyLoad ();} protected abstract void lazyLoad (); protected void onInvisible (){}}

In LazyFragment, I added three methods: onVisiable, called when fragment is set to visible, and onInvisible, that is, call when fragment is set to invisible. In addition, a lazyLoad abstract method is written, which is called in onVisible. You may wonder why it is called directly instead of getUserVisibleHint?

I write this for code reuse. Because in fragment, we also need to create a view (onCreateView () method ), you may also need to perform other small initialization operations (such as initializing remote services that need to be called through AIDL) when it is invisible. The setUserVisibleHint is called before onCreateView, so when the view is not initialized and used in lazyLoad, there will be a null pointer exception. To extract lazyLoad from a method, its subclass can do this:

Public class OpenResultFragment extends LazyFragment {// flag. The flag has been initialized. Private boolean isPrepared; @ Override public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {Log. d (LOG_TAG, "onCreateView"); View view = inflater. inflate (R. layout. fragment_open_result, container, false); // The isPrepared = true; lazyLoad (); return view ;}@ Override protected void lazyLoad () {if (! IsPrepared |! IsVisible) {return;} // fill in the data of each control }}

In the above class, an isPrepared flag is added to indicate whether the initialization is complete. Then call after the required initialization operation is complete. In the above example, after initializing the view, set isPrepared to true and call the lazyLoad () method. In lazyLoad (), if one of the isPrepared and isVisible values is not true, it is not executed. That is to say, loading continues only when the initialization is complete and visible. This avoids the problems caused by the use of initialization.

Here I will introduce fragment's lazy loading implementation so far. If you are interested, you can further explore it based on this, for example, write a Fragment with the feature of refreshing during initialization and visibility.


This article is original. For more information, see http://blog.csdn.net/maosidiaoxian/article/details/38300627. Add the QQ Group on the left for the discussion.



The fragment loading layout in android is minor.

This is true.

View view = inflater. inflate (R. layout. lrctext, container, false );

How to disable data reload when switching between android fragment

For how to switch, if you use ViewPager to switch, there is a setOffScreenxxx or something.

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.