布局更改时的片段管理

时间:2014-07-02 16:34:56

标签: java android android-fragments fragment

我有一个带有单个Activity和两个片段的应用程序。所需的行为是(用蓝色突出显示的用户交互):

纵向

enter image description here

在风景中

enter image description here

我目前的解决方案是:

横向布局

FrameLayout
   ContainerRed(FrameLayout)
   ContainerYellow(FrameLayout)

纵向布局

LinearLayout
   ContainerRed(FrameLayout weight=1)
   ContainerYellow(FrameLayout weight=1)

当用户点击绿色按钮时,我执行以下交易:

  • 在肖像中:删除FragmentA,将FragmentB添加到containerYellow,addToBackstack
  • 在横向:将FragmentB添加到contanerYellow,addToBackstack

这实现了正确的行为,除非您在事务后旋转设备,例如,如果您是肖像,请转到屏幕B1,然后将设备旋转到横向,FragmentA插槽为空。

此外,如果您在屏幕B2中并将屏幕旋转为肖像,则FragmentA将出现在FragmentB的背景中。

我该如何解决?感谢

2 个答案:

答案 0 :(得分:2)

我有一个有效的解决方案:

首先对addToBackstack使用相同的标签(" FOO");

在Activity调用的onCreate方法结束时:

Fragment fragmentB=getFragmentManager().findFragmentByTag("FRAGMENT_B_TAG");
if (fragmentB!=null){
   getFragmentManager().popBackStackImmediate("FOO",
                FragmentManager.POP_BACK_STACK_INCLUSIVE);
   attachFragmentB(fragmentB);
}

void attachFragmentB(Fragment fragmentB){
  //In portrait: Remove FragmentA, add FragmentB to containerYellow, addToBackstack("FOO")
  //In landscape: add FragmentB to contanerYellow, addToBackstack("FOO")
}

答案 1 :(得分:1)

在没有多个布局和Backstack技巧的情况下执行此操作的另一种方法是在onCreate()和onBackStackChanged()中修改容器的可见性。

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (savedInstanceState == null) {
            if (getSupportFragmentManager().findFragmentByTag(AFragment.TAG) == null) {
                Fragment fragment = new AFragment();
                getSupportFragmentManager()
                        .beginTransaction()
                        .replace(R.id.layout_left, fragment,AFragment.TAG)
                        .commit();
            }
        }
        getSupportFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
            @Override
            public void   onBackStackChanged() {
                updateScreen();
            }
        });
        updateScreen();
    }

    //called when A button's is clicked
    public void onClick() {
        if (getSupportFragmentManager().findFragmentByTag(BFragment.TAG) == null) {
            Fragment fragment = new BFragment();
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.layout_right, fragment, BFragment.TAG)
                    .addToBackStack(BFragment.TAG)
                    .commit();
        }
    }

    public void updateScreen(){
        if (getSupportFragmentManager().findFragmentByTag(BFragment.TAG) != null){
            if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
                findViewById(R.id.layout_right).setVisibility(View.VISIBLE);
                findViewById(R.id.layout_left).setVisibility(View.VISIBLE);
            }else{
                findViewById(R.id.layout_right).setVisibility(View.VISIBLE);
                findViewById(R.id.layout_left).setVisibility(View.GONE);
            }
        }else{
            findViewById(R.id.layout_right).setVisibility(View.GONE);
            findViewById(R.id.layout_left).setVisibility(View.VISIBLE);
        }
    }