希望页面切换时,一直保留底部导航栏,而底栏含有一个home键,点击无论当前在哪个界面,必须回到首页,Activity无法满足需求,就需要使用Fragment了
那么,最关键的问题,我总结了一下几点:
1.Fragment和Fragment切换逻辑及数据传递
2.Fragment和Activity切换逻辑及数据传递
3.界面返回键逻辑
4.手机返回键逻辑
5.底栏HOME键逻辑
6.Fragment的onActivityResult
MainActivity(继承FragmentActivity)装载管理全部fragment,所以切换的逻辑处理方法就定义在MainActivity中
1.Fragment和Fragment切换逻辑及数据传递
MainActivity#initFragment
public static void initFragment(Fragment f) {
changeFragment(f, true, null);
}
MainActivity的onCreate初始化首页Fragment
EntranceFragment mEntranceFragment = new EntranceFragment();
initFragment(mEntranceFragment);
MainActivity#
changeFragment
public static void changeFragment(Fragment f) {
changeFragment(f, false, null);
}
public static void changeFragment(Fragment f, Bundle bundle) {
changeFragment(f, false, bundle);
}
private static void changeFragment(Fragment f, boolean init, Bundle bundle) {
FragmentTransaction ft = fm.beginTransaction();
if (bundle != null) {
f.setArguments(bundle);
}
// ft.setCustomAnimations(
// R.anim.fragment_slide_right_in, R.anim.fragment_slide_left_out,
// R.anim.fragment_slide_left_in, R.anim.fragment_slide_right_out)
// .replace(R.id.fy_content, f);
ft.replace(R.id.fy_content, f);
if (!init) {
ft.addToBackStack(null);
}
ft.commit();
}
Fragment接收数据
Bundle bundle = getArguments();
serviceItem = bundle.getParcelable("serviceItem");
MainActivity#
intentToFragment
public static void intentToFragment(Fragment fragment, ServiceItem serviceItem){
Bundle bundle = new Bundle();
bundle.putParcelable("serviceItem", serviceItem);
MainActivity.changeFragment(fragment, bundle);
}
Fragment中使用
MainActivity.intentToFragment(new HomeInstrumentActivity(), mServiceItems.get(position));
2.Fragment和Activity切换逻辑及数据传递
MainActivity#intentToActivity
public void intentToActivity(Class<?> clazz, ServiceItem serviceItem) {
Intent i = new Intent(context, clazz);
Bundle b = new Bundle();
b.putParcelable("serviceItem", serviceItem);
i.putExtras(b);
startActivity(i);
}
Fragment中使用
MainActivity.intentToFragment(XXX.class, null);
MainActivity.intentToFragment(XXX.class, mServiceItems.get(position);
3.界面返回键逻辑
MainActivity#backToFragment
public static void backToFragment(){
fm.popBackStack();
}
Fragment中使用
@OnClick(R.id.rl_back)
public void back() {
MainActivity.backToFragment();
}
4.手机返回键逻辑
MainActivity#onKeyDown
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
backToFragment();
if(fm.getBackStackEntryCount()==0){
finish();
}
return true;
}
return super.onKeyDown(keyCode, event);
}
5.底栏HOME键逻辑
@OnClick(R.id.ly_tab_menu_home)
public void tab_menu_home() {
Log.v("JJ", "首页");
setSelected();
tab_menu_home.setSelected(true);
fm.popBackStackImmediate(null, 1);
}
6.Fragment的onActivityResult
Fragment中使用
@OnClick(R.id.tv_hospital_area)
public void hospital_area() {
tv_hostpital.setText("");
Intent intent = new Intent(getActivity(), HospitalAndDepartmentActivity.class);
Bundle b = new Bundle();
b.putString("HD", "Area");
intent.putExtras(b);
this.startActivityForResult(intent, REQUESTCODE);
}
最后,关于fragment返回栈生命周期问题,可参考: http://www.jianshu.com/p/fe16553ca2ca
用于解决点击返回键时,确定fragment加载数据时机
清空Fragment回退栈中某个Fragment之上的所有Fragment,可参考:http://www.cnblogs.com/qixing/p/4015262.html