Android fragment real sense of Onresume and OnPause

Source: Internet
Author: User

Fragment although there are onresume and OnPause, but these two methods is the activity of the method, the call timing is the same as the activity, and Viewpager collocation using this method is very chicken, is not the effect you want, here is a way to introduce.

To overwrite the Setuservisiblehint method of fragment, you can:

  

@Override public    void Setuservisiblehint (Boolean isvisibletouser) {        Super.setuservisiblehint ( Isvisibletouser);         if (isvisibletouser) {            // equivalent to fragment Onresume        Else  {            //  Equivalent to fragment's OnPause        }    }

Code Description:

By reading Viewpager and PageAdapter-related code, switching fragment is actually done by setting Setuservisiblehint and Setmenuvisibility, Calling this method does not release fragment (that is, the Ondestoryview is not executed)

========================= the more perfect package should be the following ==============================================

In the fragment Setuservisiblehint this method. See the API documentation for this method in Fragment (domestic mirror address: Fragment API):

1234 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.ParametersisVisibleToUser true if this fragment‘s UI is currently visible to the user (default), false if it is not.
12 该方法用于告诉系统,这个Fragment的UI是否是可见的。所以我们只需要继承Fragment并重写该方法,即可实现在fragment可见时才进行数据加载操作,即Fragment的懒加载。代码如下:
123456789101112131415161718192021222324252627282930313233 /* * 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 下午5:46 */public abstract class LazyFragment extends Fragment {    protected boolean isVisible;    /**     * 在这里实现Fragment数据的缓加载.     * @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, one is onvisiable, which is called when fragment is set to visible, one is oninvisible, that is, fragment is set to not be visible when called. In addition, a Lazyload abstract method is written, which is called in the onvisible. You might think, why not just call it in Getuservisiblehint?

I'm writing this for reuse of code. Because in fragment, we also need to create a view (the Oncreateview () method), and may need to do other small amounts of initialization when it is not visible (such as initializing a remote service that needs to be called through Aidl), and so on. And Setuservisiblehint is called before Oncreateview, then when the view is not initialized, in the lazyload of the use, there will be a null pointer exception. And by pulling the lazyload out into a method, its subclasses can do this:

1234567891011121314151617181920 public class OpenResultFragment extends LazyFragment{    // 标志位,标志已经初始化完成。    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初始化view的各控件    isPrepared = true;        lazyLoad();        return view;    }    @Override    protected void lazyLoad() {        if(!isPrepared || !isVisible) {            return;        }        //填充各控件的数据    }}

In the above class, we have added a flag bit isprepared, which is used to flag whether initialization is complete. It is then called after the initialization operation we need, as in the example above, after initializing the view, set isprepared to True and call the Lazyload () method at the same time. and in Lazyload (), Judge Isprepared and IsVisible as long as there is one not true will not go down execution. That is, only when the initialization is complete, and the visible time to continue to load, which avoids the initialization of the completion of the use of the problem.

This is where my introduction to fragment's lazy loading implementation ends, and if you are interested, you can delve deeper into it, such as writing a fragment with the features of slow initialization and refresh on the visible.

Reference: http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/1021/1813.html

Http://www.cnblogs.com/over140/p/3392164.html

Android fragment real sense of Onresume and OnPause

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.