private void initFragment() { mRecommendFragment = new RecommendFragment(); mInvestFragment = new InvestFragment(); mCla***oomFragment = new Cla***oomFragment(); mMineFragment = new MineFragment(); mBaseFragments = new BaseFragment[] { mRecommendFragment, mInvestFragment, mCla***oomFragment, mMineFragment }; } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.llrecommend: btStatuas(mRecommendFragment, 0); switchContent(mRecommendFragment); if(mRecommendFragment.getActivity() != null){ mRecommendFragment.NetFragment(); } break; case R.id.llinvest: btStatuas(mInvestFragment, 1); switchContent(mInvestFragment); break; case R.id.llcla***oom: btStatuas(mCla***oomFragment, 2); switchContent(mCla***oomFragment); break; case R.id.llmine: if (MyApplication.getInstance().getLoginInfo() != null) { btStatuas(mMineFragment, 3); switchContent(mMineFragment); if(mMineFragment.getActivity() != null){ mMineFragment.refreshTitleData(); } } else { Intent intent = new Intent(MainActivity.this, LoginActivity.class); intent.putExtra("fromact", Constant.FROM_ACT_MAIN); startActivityForResult(intent, 0); } break; } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); switch (resultCode) { case 1: if (MyApplication.getInstance().getLoginInfo() == null) { btStatuas(mCurrentContent, mCurrentLockIndex); switchContent(mCurrentContent); } else { btStatuas(mMineFragment, 3); switchContent(mMineFragment); } break; case 2: if (MyApplication.getInstance().getLoginInfo() == null) { btStatuas(mRecommendFragment, 0); switchContent(mRecommendFragment); } break; } } public void switchContent(Fragment to) { if (mContent != null && mContent != to) { FragmentTransaction transaction = fragmentManager .beginTransaction().setCustomAnimations( android.R.anim.fade_in, android.R.anim.fade_out); if (!to.isAdded()) { // 先判断是否被add过 transaction.hide(mContent).add(R.id.llfrag, to).commitAllowingStateLoss(); // 隐藏当前的fragment,add下一个到Activity中 } else { transaction.hide(mContent).show(to).commitAllowingStateLoss(); // 隐藏当前的fragment,显示下一个 } mContent = to; } }
1、上面代码是多个fragment之间进行切换显示,未添加到activity中的需要add、commit,已添加的需要hide,show。
在FragmentActivity启动时,会加载在Fragment1,当Fragment1--->Activity2----->Activity3---->FragmentActivity(Fragment1)---->Fragment2,时此时Fragmnet属于未被Add入FragmentActivity中,仅仅通过new创建了实例,内部生命周期不跑,其中getActivity == null,isAdded() == false;commit并没有立即执行,而是走正常生命周期,所以不能再activity中不能调用fragment内部方法,ui未加载都为空,不能进行ui刷新。当add过后commit执行完,内部方法可以调用。
2、关于show,hiden时fragment在别的activity中fc后返回会出现fragment显示show异常
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (savedInstanceState == null) { initView(); initClick(); initFragment(); initFragmentContent(); //版本检测 // initData(); } else { initView(); initClick(); initFragment(); fragmentManager = getSupportFragmentManager(); FragmentTransaction beginTransaction = fragmentManager .beginTransaction(); btStatuas(mRecommendFragment, 0); // findViewById(R.id.llfrag) beginTransaction.replace(R.id.llfrag, mRecommendFragment); // beginTransaction.add(R.id.llfrag, mRecommendFragment); mContent = mBaseFragments[0]; beginTransaction.commitAllowingStateLoss(); // Toast.makeText(MainActivity.this, "", Toast.LENGTH_SHORT).show(); } @Override protected void onSaveInstanceState(Bundle outState) { // TODO Auto-generated method stub super.onSaveInstanceState(outState); outState.putInt("index", 0); }
onSaveInstanceState(Bundle outState)保存状态,当异常重新加载会走onCreate方法,通过保存状态的参数来判断
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。