关于FragmentActivity中调用fragment中方法
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于FragmentActivity中调用fragment中方法相关的知识,希望对你有一定的参考价值。
private void initFragment() { mRecommendFragment = new RecommendFragment(); mInvestFragment = new InvestFragment(); mClassRoomFragment = new ClassRoomFragment(); mMineFragment = new MineFragment(); mBaseFragments = new BaseFragment[] { mRecommendFragment, mInvestFragment, mClassRoomFragment, 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.llclassroom: btStatuas(mClassRoomFragment, 2); switchContent(mClassRoomFragment); 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方法,通过保存状态的参数来判断
本文出自 “绝对领域” 博客,请务必保留此出处http://lyp634959848.blog.51cto.com/7750498/1754297
以上是关于关于FragmentActivity中调用fragment中方法的主要内容,如果未能解决你的问题,请参考以下文章
FragmentActivity onSaveInstanceState 没有被调用
Android 关于Toolbar和FragmentActivity的问题
在 FragmentActivity 中永远不会调用 onRequestPermissionsResult()
onCreateOptionsMenu()没有在FragmentActivity上调用