2013-11-25 112 views
0

我正在使用這種代碼來處理主要活動中的三個片段...在屏幕方向更改上處理碎片?

FragmentA是固定它一個框架..我更改按鈕上的FragmentB和FragmentC單擊FragmentA。 他的代碼在縱向或橫向視圖中運行良好。這裏是代碼波紋管。 enter image description here

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(); 

    } 

本準則的縱向或橫向視圖運行效果良好要麼...但是當過我改變方向的兩個片段覆蓋另一個。

需要解決方案。

+0

試試這個http://stackoverflow.com/questions/13305861/fool-proof-way-to-handle-fragment-on-orientation-change – user2991290

回答

2

當你的方向改變時,你的Activity被重新創建。

"In the scenario where a user rotates their device, Android will destroy your 
application’s activity(s). Before destroying them it calls, onSaveInstanceState, 
allowing developers to persist data. Once the activity is recreated post 
rotation, the OS will call onRestoreInstanceState giving developers a chance to 
restore the application state pre-rotation." 

您應該嘗試保存您的Fragments State

putFragment將引用放入Bundle中的片段。這個Bundle可以作爲保存狀態持久化,當以後恢復時getFragment(Bundle,String)將返回同一片段的當前實例。

@Override 
protected void onSaveInstanceState(Bundle outState) { 
    FragmentManager manager = getFragmentManager(); 
    manager.putFragment(outState, MyFragment.TAG, mMyFragment); 
} 

getFragment檢索當前片段實例先前放置與putFragment(捆綁,字符串,片段)的引用。

private void instantiateFragments(Bundle inState) { 
    FragmentManager manager = getFragmentManager(); 
    FragmentTransaction transaction = manager.beginTransaction(); 

    if (inState != null) { 
     mMyFragment = (MyFragment) manager.getFragment(inState, MyFragment.TAG); 
    } else { 
     mMyFragment = new MyFragment(); 
     transaction.add(R.id.fragment, mMyFragment, MyFragment.TAG); 
     transaction.commit(); 
    } 
} 

restoreFragment

@Override 
protected void onRestoreInstanceState(Bundle inState) { 
    instantiateFragments(inState); 
} 

希望這有助於。

+0

對我來說,當我旋轉設備它去在instantiateFragments()if(inState != null)條件並顯示空白屏幕。 – PankajAndroid