Fragment知识点代码片段

	public class MyOnCheckedChangeListener implements OnCheckedChangeListener {
		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			FragmentTransaction ft = getFragmentManager().beginTransaction();
			if (mAppsFragment != null) {
				ft.hide(mAppsFragment);
			}
			if (mPhoneFragment != null) {
				ft.hide(mPhoneFragment);
			}
			if (mEmailFragment != null) {
				ft.hide(mEmailFragment);
			}
			if (mSettingsFragment != null) {
				ft.hide(mSettingsFragment);
			}
			switch (checkedId) {
			case R.id.main_tab_apps:
				if (mAppsFragment == null || mAppsFragment.getActivity() == null) {
					mAppsFragment = new AppsFragment();
					ft.add(R.id.realtabcontent, mAppsFragment);
				} else {
					ft.show(mAppsFragment);
				}
				ft.commit();
				break;
			case R.id.main_tab_phone:
				if (mPhoneFragment == null || mPhoneFragment.getActivity() == null) {
					mPhoneFragment = new PhoneFragment();
					ft.add(R.id.realtabcontent, mPhoneFragment);
				} else {
					ft.show(mPhoneFragment);
				}
				ft.commit();
				break;
			case R.id.main_tab_email:
				if (mEmailFragment == null || mEmailFragment.getActivity() == null) {
					mEmailFragment = new EmailFragment();
					ft.add(R.id.realtabcontent, mEmailFragment);
				} else {
					ft.show(mEmailFragment);
				}
				ft.commit();
				break;
			case R.id.main_tab_setting:
				if (mSettingsFragment == null || mSettingsFragment.getActivity() == null) {
					mSettingsFragment = new SettingsFragment();
					ft.add(R.id.realtabcontent, mSettingsFragment);
				} else {
					ft.show(mSettingsFragment);
				}
				ft.commit();
				break;
			}
		}
	}


	private void detachFragments() {
		FragmentTransaction ft = getFragmentManager().beginTransaction();		
		if (mAppsFragment != null) {
			ft.remove(mAppsFragment);
			mAppsFragment = null;
		}
		if (mPhoneFragment != null) {
			ft.remove(mPhoneFragment);
			mPhoneFragment = null;
		}
		if (mEmailFragment != null) {
			ft.remove(mEmailFragment);
			mEmailFragment = null;
		}
		if (mSettingsFragment != null) {
			ft.remove(mSettingsFragment);
			mSettingsFragment = null;
		}
	}

ft.detach(fragment):
onPause, onStop, onDestroyView
ft.remove(fragment):
onPause, onStop, onDestroyView, onDestroy and onDetach
ft.attach(fragment):
onCreateView, onStart and onResume
ft.add(fragment):
onAttach, onCreate, onCreateView, onStart and onResume


描述:

You'll notice that when a Fragment is detached, its onPause, onStop and onDestroyView methods are called only (in that order). On the other hand, when a Fragment is removed, its onPause, onStop, onDestroyView, onDestroy and onDetach methods are called (in that order). Similarly, when attaching, the Fragment's onCreateView, onStart and onResume methods are called only; and when adding, the Fragment's onAttach, onCreate, onCreateView, onStart and onResume methods are called (in that order).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值