屏幕方向更改处理片段?

时间:2013-11-25 09:58:47

标签: android tabs fragment

我正在使用这种代码来处理主要活动中的三个片段......

FragmentA固定它一帧..我在Button上更改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();

    }

此代码在纵向或横向视图中运行良好...但是当我改变方向时两个碎片相互覆盖。

需要解决方案。

1 个答案:

答案 0 :(得分: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(Bundle,String,Fragment)放置的引用的当前Fragment实例。

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

希望这有帮助。