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。
在大廠等地區(qū),都構建了全面的區(qū)域性戰(zhàn)略布局,加強發(fā)展的系統(tǒng)性、市場前瞻性、產品創(chuàng)新能力,以專注、極致的服務理念,為客戶提供成都做網(wǎng)站、網(wǎng)站設計 網(wǎng)站設計制作按需開發(fā),公司網(wǎng)站建設,企業(yè)網(wǎng)站建設,成都品牌網(wǎng)站建設,全網(wǎng)營銷推廣,外貿網(wǎng)站建設,大廠網(wǎng)站建設費用合理。在FragmentActivity啟動時,會加載在Fragment1,當Fragment1--->Activity2----->Activity3---->FragmentActivity(Fragment1)---->Fragment2,時此時Fragmnet屬于未被Add入FragmentActivity中,僅僅通過new創(chuàng)建了實例,內部生命周期不跑,其中getActivity == null,isAdded() == false;commit并沒有立即執(zhí)行,而是走正常生命周期,所以不能再activity中不能調用fragment內部方法,ui未加載都為空,不能進行ui刷新。當add過后commit執(zhí)行完,內部方法可以調用。
2、關于show,hiden時fragment在別的activity中fc后返回會出現(xiàn)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)保存狀態(tài),當異常重新加載會走onCreate方法,通過保存狀態(tài)的參數(shù)來判斷
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。