标签:
首先,我们这篇博文只探讨一个很小的问题。
就是在ViewPager和Fragment配合使用时的生命周期调用问题。
先给出结论吧:
1、当屏幕显示出你第N个Fragment时,你的第(N-1)和第(N+1)个Fragment会执行其生命周期到Onresume阶段。
2、除以上三个Fragment以外的全部fragment的所有方法都不会被调用。
下面是验证代码:
首先是我的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 getItem(int position) { return fragments.get(position); } @Override public int getCount() { return fragments.size(); } }; viewPager.setAdapter(fragmentPagerAdapter); viewPager.setCurrentItem(1); } }
Demo很简单,就是动态根据String[]的内容生成一群Fragment,并通过ViewPager完成加载。嗯,我们首先选择加载第二页,这样方便我们更清楚的了解FragMent被调用的情况。
接下来是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 savedInstanceState) { 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(savedInstanceState); 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); }
05-08 13:43:56.540 14264-14264/com.example.chunya.testviewpager E/Test: onAttach:b 05-08 13:43:56.540 14264-14264/com.example.chunya.testviewpager E/Test: onCreate:b 05-08 13:43:56.541 14264-14264/com.example.chunya.testviewpager E/Test: onAttach:c 05-08 13:43:56.541 14264-14264/com.example.chunya.testviewpager E/Test: onCreate:c 05-08 13:43:56.544 14264-14264/com.example.chunya.testviewpager E/Test: onCreateView:c 05-08 13:43:56.545 14264-14264/com.example.chunya.testviewpager E/Test: onActivityCreated:c 05-08 13:43:56.546 14264-14264/com.example.chunya.testviewpager E/Test: onCreateView:b 05-08 13:43:56.546 14264-14264/com.example.chunya.testviewpager E/Test: onActivityCreated:b 05-08 13:43:56.546 14264-14264/com.example.chunya.testviewpager E/Test: onStart():b 05-08 13:43:56.546 14264-14264/com.example.chunya.testviewpager E/Test: onResume:b 05-08 13:43:56.546 14264-14264/com.example.chunya.testviewpager E/Test: onAttach:a 05-08 13:43:56.546 14264-14264/com.example.chunya.testviewpager E/Test: onCreate:a 05-08 13:43:56.547 14264-14264/com.example.chunya.testviewpager E/Test: onCreateView:a 05-08 13:43:56.547 14264-14264/com.example.chunya.testviewpager E/Test: onActivityCreated:a 05-08 13:43:56.547 14264-14264/com.example.chunya.testviewpager E/Test: onStart():c 05-08 13:43:56.548 14264-14264/com.example.chunya.testviewpager E/Test: onResume:c 05-08 13:43:56.548 14264-14264/com.example.chunya.testviewpager E/Test: onStart():a 05-08 13:43:56.548 14264-14264/com.example.chunya.testviewpager E/Test: onResume:a
而d、e两页却并未完成加载,但是当fragment滑动时,他们也会被陆续加载出来。
关于ViewPager和Fragment配合使用生命周期的问题。
标签:
原文地址:http://blog.csdn.net/chunge2788/article/details/51345285