Questions about the use of Viewpager and fragment in conjunction with the life cycle.

Source: Internet
Author: User

First of all, this blog post only explores a very small problem.

is the life cycle call problem when Viewpager and fragment are used together.

Let's draw a conclusion:

1. When the screen shows your nth fragment, your first (N-1) and (n+1) fragment will perform their life cycle to the onresume stage.

2. All fragment except for the above three fragment will not be called.


Here is the verification code:

First of all Is my mainactivity ()


public class Mainactivity extends fragmentactivity {Viewpager viewpager;    Arraylist<fragment> fragments;    String[] Strings=new string[]{"A", "B", "C", "D", "E"};        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        Viewpager= (Viewpager) Findviewbyid (R.id.viewpager);        Fragments=new arraylist<fragment> ();        for (String s:strings) {fragments.add (new Fragmenta (s));        } Fragmentmanager Fm=getsupportfragmentmanager (); Fragmentpageradapter fragmentpageradapter=new fragmentpageradapter (FM) {@Override public Fragment ge            TItem (int position) {return fragments.get (position);            } @Override public int getcount () {return fragments.size ();        }        };        Viewpager.setadapter (Fragmentpageradapter);    Viewpager.setcurrentitem (1);}} 

Demo is simple, is dynamically based on the content of string[] to generate a group of fragment, and through the Viewpager to complete the load. Well, we first chose to load the second page, so that we could get a better idea of what fragment was called.


Next is the content of Fragmenta ().


public class Fragmenta extends Fragment {TextView TextView;    String s;        Public Fragmenta (String s) {super ();    This.s=s;        } @Override public void Onattach (context context) {Super.onattach (context);    LOG.E ("Test", "Onattach:" +s);        } @Override public void OnCreate (@Nullable Bundle savedinstancestate) {super.oncreate (savedinstancestate);    LOG.E ("Test", "OnCreate:" +s); } @Override Public View oncreateview (Layoutinflater inflater, @Nullable viewgroup container, @Nullable Bundle Savedi        Nstancestate) {View view=inflater.inflate (r.layout.frag_a,null);        textview= (TextView) View.findviewbyid (r.id.tv_frag_a);        LOG.E ("Test", "Oncreateview:" +s);    return view; } @Override public void onactivitycreated (@Nullable Bundle savedinstancestate) {super.onactivitycreated (save        Dinstancestate);    LOG.E ("Test", "onactivitycreated:" +s); } @Override public void OnStart () {super.onsTart ();    LOG.E ("Test", "OnStart ():" +s);        } @Override public void Onresume () {super.onresume ();        Textview.settext (s);    LOG.E ("Test", "Onresume:" +s); }

Next, let's look at what log will pass to us when the program starts.

05-08 13:43:56.540 14264-14264/com.example.chunya.testviewpager e/test:onattach:b05-08 13:43:56.540 14264-14264/ Com.example.chunya.testviewpager e/test:oncreate:b05-08 13:43:56.541 14264-14264/com.example.chunya.testviewpager e/test:onattach:c05-08 13:43:56.541 14264-14264/com.example.chunya.testviewpager e/test:oncreate:c05-08 13:43:56.544 14264-14264/com.example.chunya.testviewpager e/test:oncreateview:c05-08 13:43:56.545 14264-14264/ Com.example.chunya.testviewpager e/test:onactivitycreated:c05-08 13:43:56.546 14264-14264/ Com.example.chunya.testviewpager e/test:oncreateview:b05-08 13:43:56.546 14264-14264/ Com.example.chunya.testviewpager e/test:onactivitycreated:b05-08 13:43:56.546 14264-14264/ Com.example.chunya.testviewpager E/test:onstart (): b05-08 13:43:56.546 14264-14264/ Com.example.chunya.testviewpager e/test:onresume:b05-08 13:43:56.546 14264-14264/com.example.chunya.testviewpager e/test:onattach:a05-08 13:43:56.546 14264-14264/com.example.chunya.testviewpager E/Test: oncreate:a05-08 13:43:56.547 14264-14264/com.example.chunya.testviewpager e/test:oncreateview:a05-08 13:43:56.547 14264-14264/com.example.chunya.testviewpager e/test:onactivitycreated:a05-08 13:43:56.547 14264-14264/ Com.example.chunya.testviewpager E/test:onstart (): c05-08 13:43:56.548 14264-14264/ Com.example.chunya.testviewpager e/test:onresume:c05-08 13:43:56.548 14264-14264/com.example.chunya.testviewpager E/test:onstart (): a05-08 13:43:56.548 14264-14264/com.example.chunya.testviewpager e/test:onresume:a

Obviously, we chose the second page, then the load is "B", but A and C almost and B have finished loading the content at the same time.

The D and E pages are not loaded, but when the fragment slide, they will be loaded in succession.



Questions about the use of Viewpager and fragment in conjunction with the life cycle.

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.