0

I have 1 activity, 2 layouts for both portrait and landscape and 2 fragments.

in portrait layout there is only 1 fragment, in landscape there are 2 fragments stand together.

when I run the activity as portrait mode at the beginning, fragmentmanager says there is only 1 fragment which is ok. Then I change the orientation and fragmentmanager says there are 2 fragments anymore which is also ok. But even though I change the orientation from landscape to portrait fragmentmanager still says there are 2 fragments. The question is that does it need to remove all fragments it has with every new oncreate? How to provide there should just 1 fragment everytime if activity creates with portrait layout?

4

1 に答える 1

0

問題を解決しました。

向きが横向きから縦向きに変化している間は、2 番目のフラグメントのみを削除します。

ここに例があります:

@Override
public void onSaveInstanceState(Bundle state)
{
    MySecondFragment secondFragment = (MySecondFragment) getFragmentManager().findFragmentById(R.id.secondFragment);
    if (secondFragment!=null)
    {
        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.remove(secondFragment);
        ft.commit();
    }
    super.onSaveInstanceState(state);

}
于 2014-06-11T12:09:44.783 に答える