1 -->onPause 2 -->onStop 3 -->onDestroyView
再由1切换回2或者3时,3界面对应的Fragment的执行流程:
1 -->onCreateView 2 -->onStart 3 -->onResume
可见,界面3对应的Fragment被销毁并重新创建。
mPager .setOffscreenPageLimit(2);
参数:int limit - 缓存当前界面每一侧的界面数
以上述为例,当前界面为1,limit = 2,表示缓存2、3两个界面。如此便避免了界面3被销毁。1 @Override 2 public void onDestroyView() { 3 LogUtils.d(TAG , "-->onDestroyView"); 4 super .onDestroyView(); 5 if (null != FragmentView) { 6 ((ViewGroup) mFragmentView.getParent()).removeView(mFragmentView); 7 } 8 }
步骤2:在onCreateView方法内复用RootView
1 @Override 2 public View onCreateView(LayoutInflater inflater, ViewGroup container, 3 Bundle savedInstanceState) { 4 LogUtils.d (TAG, "-->onCreateView"); 5 if (null == mFragmentView) { 6 mFragmentView = inflater.inflate(R.layout.fragment, container, false); 7 mListView = (ListView) mFragmentView .findViewById(R.id.mm_listview); 8 mListView.setAdapter(mAdapter); 9 mPbar = (ProgressBar) mFragmentView.findViewById(R.id.pbar_mm_loading); 10 mPbar.setVisibility(View.VISIBLE); 11 } 12 13 return mFragmentView ; 14 }