您好,登錄后才能下訂單哦!
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(); // 隱藏當(dāng)前的fragment,add下一個(gè)到Activity中 } else { transaction.hide(mContent).show(to).commitAllowingStateLoss(); // 隱藏當(dāng)前的fragment,顯示下一個(gè) } mContent = to; } }
1、上面代碼是多個(gè)fragment之間進(jìn)行切換顯示,未添加到activity中的需要add、commit,已添加的需要hide,show。
在FragmentActivity啟動(dòng)時(shí),會(huì)加載在Fragment1,當(dāng)Fragment1--->Activity2----->Activity3---->FragmentActivity(Fragment1)---->Fragment2,時(shí)此時(shí)Fragmnet屬于未被Add入FragmentActivity中,僅僅通過new創(chuàng)建了實(shí)例,內(nèi)部生命周期不跑,其中g(shù)etActivity == null,isAdded() == false;commit并沒有立即執(zhí)行,而是走正常生命周期,所以不能再activity中不能調(diào)用fragment內(nèi)部方法,ui未加載都為空,不能進(jìn)行ui刷新。當(dāng)add過后commit執(zhí)行完,內(nèi)部方法可以調(diào)用。
2、關(guān)于show,hiden時(shí)fragment在別的activity中fc后返回會(huì)出現(xiàn)fragment顯示show異常
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (savedInstanceState == null) { initView(); initClick(); initFragment(); initFragmentContent(); //版本檢測(cè) // 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)保存狀態(tài),當(dāng)異常重新加載會(huì)走onCreate方法,通過保存狀態(tài)的參數(shù)來判斷
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。