0
奇怪的行爲,我有5個片段,我在動作條在兩者之間切換:findFragmentByTag上切換標籤
@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();
}
然後在功能我想從Fragment4得到的值:
Fragment4 frag4 = (Fragment4)getSupportFragmentManager().findFragmentByTag("Fragment4");
String stfrag4 = frag4.getFrag4Value();
的問題是當我在標籤4這是我的Fragment4內容頁面,findFragmentByTag
返回總是爲空,但當我切換到另一個標籤,然後找到該片段它返回true片段,而不是空。 爲什麼會發生這種情況?我該如何解決這個問題?提前致謝。