我正在使用這種代碼來處理主要活動中的三個片段...在屏幕方向更改上處理碎片?
FragmentA是固定它一個框架..我更改按鈕上的FragmentB和FragmentC單擊FragmentA。 他的代碼在縱向或橫向視圖中運行良好。這裏是代碼波紋管。
public class MainActivity extends FragmentActivity implements
OnSwitchClickListener {
FragmentManager manager;
FragmentA fragA;
FragmentB fragB;
FragmentC fragC;
boolean fragBSet = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragA = new FragmentA();
fragB = new FragmentB();
fragC = new FragmentC();
manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
ft.add(R.id.a_container, fragA, "frag A");
if (fragBSet) {
ft.add(R.id.bc_container, fragC, "frag C");
} else {
ft.add(R.id.bc_container, fragB, "frag B");
}
fragBSet = true;
ft.commit();
}
@Override
public void onSwitchClick(View v) {
Toast.makeText(getApplicationContext(), "Switch clkick from Activity",
Toast.LENGTH_LONG).show();
FragmentTransaction ft = manager.beginTransaction();
if (fragBSet) {
ft.remove(fragB);
ft.add(R.id.bc_container, fragC, "frag C");
fragBSet = false;
} else {
ft.remove(fragC);
ft.add(R.id.bc_container, fragB, "frag B");
fragBSet = true;
}
ft.commit();
}
本準則的縱向或橫向視圖運行效果良好要麼...但是當過我改變方向的兩個片段覆蓋另一個。
需要解決方案。
試試這個http://stackoverflow.com/questions/13305861/fool-proof-way-to-handle-fragment-on-orientation-change – user2991290