您好,登录后才能下订单哦!
从fragment-A切换到fragment-B,再从fragment-B切换到fragment-A中。跟踪发现,切换回到fragment-A后,fragment-A的实例变量的值不会被重置,保持和上次相同,但是fragment-A的life cycle方法被重新调用。
第1次创建fragment时,生命周期方法调用顺序是:
11-06 13:56:26.519: D/OrderVideoFragment(2504): ++onCreate++ 11-06 13:56:26.529: D/OrderVideoFragment(2504): ++onCreateView++ 11-06 13:56:26.549: D/OrderVideoFragment(2504): ++onActivityCreated++ 11-06 13:56:26.549: D/OrderVideoFragment(2504): ++onStart++ 11-06 13:56:26.549: D/OrderVideoFragment(2504): ++onResume++
切换到其他fragment时,查看日志
11-06 13:57:44.379: D/OrderVideoFragment(2504): ++onPause++ 11-06 13:57:44.379: D/OrderVideoFragment(2504): ++onStop++
再从其他fragment切换回来,查看日志
11-06 13:58:36.429: D/OrderVideoFragment(2504): ++onCreateView++ 11-06 13:58:36.469: D/OrderVideoFragment(2504): ++onActivityCreated++ 11-06 13:58:36.469: D/OrderVideoFragment(2504): ++onStart++ 11-06 13:58:36.469: D/OrderVideoFragment(2504): ++onResume++
从日志中可以发现,在切换到其他fragment时,并没有调用onDestroy方法,再从其他fragment切换回来时,也没有再次调用onCreate方法。由此可以说明,在fragment来回切换过程中,fragment并没有销毁,所以也没有重新创建,只是按照fragment的设计约定执行了一些生命周期方法。
既然这样,那么在切换回到OrderVideoFragment时,实例变量保存着原来的值就不难理解了,我们在切换回来之后,就可以利用保存在变量中的值来恢复fragment原来的界面。例如在OrderVideoFragment中,第1次创建时要从网络加载数据,那在后来切换回来就可以利用上次从网络加载的数据。
以下说明一下OrderVideoFragment类中的重要代码片段和重要方法
1. 重写onCreateView方法,创建fragment界面(界面其实表现为View组件)。
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Logger.i(TAG, "++onCreateView++"); this.inflater = inflater; this.context = getActivity(); setTagId(); setTitleName(); setBRTL(); View view = inflater.inflate(R.layout.weiyu_fragment, null); // 文字标题 TextView titleView = (TextView) view.findViewById(R.id.txtTitleName); if (!TextUtils.isEmpty(titleName)) { titleView.setVisibility(View.VISIBLE); titleView.setText(titleName); } else { titleView.setVisibility(View.INVISIBLE); } // 图片标题 ImageView titleImageView = ((ImageView) view.findViewById(R.id.titleImage)); if (titleImageResId != 0) { titleImageView.setVisibility(View.VISIBLE); titleImageView.setImageResource(titleImageResId); } else { titleImageView.setVisibility(View.INVISIBLE); } // 菜单按钮 view.findViewById(R.id.btnMenu).setOnClickListener(this); // 搜索按钮 view.findViewById(R.id.btnSearch).setOnClickListener(this); pullToRefreshView = (PullToRefreshView) view.findViewById(R.id.pullView); pullToRefreshView.setFootLocked(true); pullToRefreshView.setOnHeaderRefreshListener(this); mLayout = ((LinearLayout) view.findViewById(R.id.videoGroupsLayout)); return view; }
2. 重新onStart方法。在onStart方法中加载数据。
@Override public void onStart() { super.onStart(); loadData(); }
3. 实现加载数据方法。
private void loadData() { if(mResult == null) loadDataFromNetWork(); else loadDataFromCache(); }
4. 加载数据要分两种情况:
(1)从实例变量中加载数据。
private void loadDataFromCache() { new AsyncTask<Void, Void, Void>() { @Override protected void onPreExecute() { super.onPreExecute(); hideRefreshView(); hideWaitView(); mViewList.clear(); } @Override protected Void doInVoid... params) { for (VideoGroupData dataItem : mData) { mViewList.add(buildVideoGroupView(dataItem)); } return null; } @Override protected void onPostExecute(Void result) { super.onPostExecute(result); mLayout.removeAllViews(); for (View view : mViewList) { mLayout.addView(view); } } }.execute(); }
(2)从网络加载数据。
private void loadDataFromNetWork() { IVideoFetcher.CallBack callBack = new IVideoFetcher.CallBackAdapter() { @Override public void onComplete(IVideoResult result) { hideWaitView(); mResult = result; // 加载数据成功 if (result.isSuccess()) { mData = ((WeiyuVideoResult) result).getData(); mViewList.clear(); for (VideoGroupData dataItem : mData) { mViewList.add(buildVideoGroupView(dataItem)); } mLayout.removeAllViews(); for (View view : mViewList) { mLayout.addView(view); } } // 加载数据失败 else { if (!isPullDownRefresh) { showRefreshView(); } UIUtils.showToast(context, getString(R.string.load_fail_tray_again_later)); } if (isPullDownRefresh) { pullToRefreshView.onHeaderRefreshComplete(); isPullDownRefresh = false; } } @Override public void onBegin() { hideRefreshView(); if (!isPullDownRefresh) showWaitView(); } }; HashMap<String, String> params = new HashMap<String, String>(); params.put("q", null); IVideoFetcher.doGet(WeiyuVideoResult.class, callBack, IVideoURL.getCloudVideoURL(), params); }
5. 根据加载到的数据构建View界面。
private View buildVideoGroupView(VideoGroupData videoGroupData) { int resid = bRTL ? R.layout.video_group2_rtl : R.layout.video_group2; View view = (RelativeLayout) inflater.inflate(resid, null); TextView titleView = ((TextView) view.findViewById(R.id.groupTitleName)); titleView.setText(videoGroupData.getTitle()); View titleLayout = view.findViewById(R.id.groupTitleLayout); titleLayout.setOnClickListener(this); titleLayout.setTag(cidTagId, videoGroupData.getId()); titleLayout.setTag(cnameTagId, videoGroupData.getTitle()); List<VideoItemData> items = videoGroupData.getData(); int n = items.size() / mColumns * mColumns; // 取偶数个item items = CommonUtils.getFirstNItems(items, n); VideoGridAdapter videoAdapter = new VideoGridAdapter(context, items, false); videoAdapter.setBRTL(bRTL); FixedGridLayout fixGridLayout = (FixedGridLayout) view.findViewById(R.id.fixGridLayout); int cellWidth = UIUtils.getScreenWidthPixels(context) / mColumns; fixGridLayout.setRowParams(cellWidth, mColumns); fixGridLayout.setAdapter(videoAdapter); return view; }
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。