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 ([email protected]) * 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 = 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.