Android Development Tips Fragment lazy load _android

Source: Internet
Author: User

Objective

Lazy load, is when the fragment is completely visible when we go to load data, we do application development, one Activity may be viewpager (or other containers) with multiple fragment to use, and if each fragment need to load data, or loaded locally, or loaded from the network, it activity becomes necessary to initialize a large amount of resources at the time of the first creation. Such a result, of course, we will not be satisfied. So, can it be done when switching to this fragment, it is to initialize it?

The answer is in this method in the fragment setUserVisibleHint .

See the API documentation for this method in fragment:

Set a hint to the system about whether this fragment's UI is currently visible to the user. This hint defaults to true and are persistent across fragment instance state save and restore. 
 
An app may set this to false to indicate that fragment's UI is scrolled out of visibility or are otherwise not directly Visible to the user. This May is used by the system to prioritize operations such as fragment lifecycle updates or loader ordering />parameters 

This method is used to tell the system whether the fragment UI is visible. So we just need to inherit fragment and rewrite the method to implement the data loading operation when the 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_contr 
 
Ol_v2.common; 
 
Import android.support.v4.app.Fragment;  /** * AUTHOR:MSDX (645079761@qq.com) * TIME:14-7-17 PM 5:46 * * Public abstract class Lazyfragment extends Fragment 
 {protected Boolean isVisible; 
  /** * Implements slow loading of fragment data here. * @param isvisibletouser */@Override public void Setuservisiblehint (Boolean isvisibletouser) {Super.setuservis 
  Iblehint (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, one that is onVisiable called when fragment is set to be visible, and one is when onInvisible fragment is set to invisible. In addition lazyLoad , an abstract method is written, which is invoked inside the onvisible. You might think, why not getUserVisibleHint just call it in there?

I wrote this for the reuse of code. Because in fragment, we also need to create a view ( onCreateView() method), and may also need to do a small amount of initialization when it is not visible (such as initializing a remote service that needs to be invoked via Aidl), and so on. In the case of a setUserVisibleHint onCreateView previous call, lazyLoad a null pointer exception is used when the view is uninitialized. Instead lazyLoad of pumping out into a method, its subclasses can do this:

public class Openresultfragment extends lazyfragment{ 
 //Flag bit, flag already 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); 
  XXX Initializes the control of view 
 isprepared = true; 
  Lazyload (); 
  return view; 
 } 
 
 @Override 
 protected void Lazyload () { 
  if (!isprepared | |!isvisible) {return 
   ; 
  } 
  Populating data for individual controls 
 } 
 

In the above class, we added a flag bit to isPrepared flag whether the initialization is complete. Then call after the initialization we need to complete, as in the example above, view after initialization, set isPrepared to True, and call the method at the same time lazyLoad() . And in the lazyLoad() midst of it, judgment isPrepared and isVisible as long as one is not true, does not go down. That is, it is only when the initialization is complete, and when it is visible, that it continues to load, which avoids the problem of using the uninitialized finish.

Summarize

Here's my introduction to the lazy load implementation of fragment, so if you're interested, you can delve deeper into it, such as writing a fragment with slow initialization and a feature that refreshes when visible. I hope the content of this article for everyone's study or work can help, if there is doubt you can message exchange.

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.