关于FragmentActivity中调用fragment中方法-创新互联
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方法,通过保存状态的参数来判断
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
文章标题:关于FragmentActivity中调用fragment中方法-创新互联
网页链接:http://scyanting.com/article/jccjg.html