这个MyFragmentPagerAdapter在被set给viewPager后,会调用getItem(),实例化Framgment
@Override public Fragment getItem(int position) { MyFragment fragment = MyFragment.newInstance(); fragment.setmCid(mCID[position]); return fragment; }
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if(mCid == null) mCid = savedInstanceState.getString("CurrentCID"); } @Override public void onSaveInstanceState(Bundle savedInstanceState) { savedInstanceState.putString("CurrentCID", mCid); super.onSaveInstanceState(savedInstanceState); }故此得出结论:在横竖屏切换时,Android系统不再重新构建fragement实例,而是复用之前的fragment,只不过状态(数据)全部清零。如果你想要保存之前的状态(数据),使用onSaveInstanceState,道理和Activity横竖屏切换一样。