Android findFragmentByTag切换选项卡上的奇怪行为
我在ActionBar中切换了5个片段:Android findFragmentByTag切换选项卡上的奇怪行为,android,android-fragments,tabs,android-actionbar,fragmenttransaction,Android,Android Fragments,Tabs,Android Actionbar,Fragmenttransaction,我在ActionBar中切换了5个片段: @Override public void onTabSelected(Tab tab, FragmentTransaction ft) { int tag = (Integer) tab.getTag(); fragmentTransaction = fragmentManager.beginTransaction(); switch(tag) { case 0: FragmentBase f
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft)
{
int tag = (Integer) tab.getTag();
fragmentTransaction = fragmentManager.beginTransaction();
switch(tag)
{
case 0:
FragmentBase fragBase = new FragmentBase ();
fragmentTransaction.replace(R.id.frame_order,fragBase,"FragmentBase");
fragmentTransaction.addToBackStack(null);
break;
case 1:
Fragment1 frag1 = new Fragment1();
fragmentTransaction.replace(R.id.frame_order, frag1, "Fragment1");
fragmentTransaction.addToBackStack(null);
break;
case 2:
Fragment2 frag2 = new Fragment2();
fragmentTransaction.replace(R.id.frame_order, frag2, "Fragment2");
fragmentTransaction.addToBackStack(null);
break;
case 3:
Fragment3 frag3 = new Fragment3();
fragmentTransaction.replace(R.id.frame_order, frag3, "Fragment3");
fragmentTransaction.addToBackStack(null);
break;
case 4:
Fragment4 frag4 = new Fragment4();
fragmentTransaction.replace(R.id.frame_order, frag4, "Fragment4");
fragmentTransaction.addToBackStack(null);
break;
}
fragmentTransaction.commit();
}
然后在函数中,我想从片段4中获取一个值:
Fragment4 frag4 = (Fragment4)getSupportFragmentManager().findFragmentByTag("Fragment4");
String stfrag4 = frag4.getFrag4Value();
问题是,当我在标签4上时,这是我的Fragment4内容页,findFragmentByTag
的返回值总是空的,但当我切换到另一个标签,然后找到该片段时,它返回的是真正的片段而不是空的。
为什么会这样?我怎样才能解决这个问题呢?提前感谢。进行测试
改变
int tag = (Integer) tab.getTag();
借
int tag = getActionBar().getSelectedNavigationIndex();