1

縦向きモードと横向きモードの 2 つの異なるレイアウトがあります。

layout\activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <FrameLayout android:id="@+id/container"
        android:layout_height="match_parent"
        android:layout_width="match_parent"/>

</RelativeLayout>

そしてlayout-land\activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <fragment
        android:id="@+id/fragment1"
        android:name="com.example.fragmentorientationchangetest.Fragment1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <fragment
        android:id="@+id/fragment2"
        android:name="com.example.fragmentorientationchangetest.Fragment2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

</LinearLayout>

onCreate に Fragment1 を追加します。

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (findViewById(R.id.container) != null) {

            if (savedInstanceState != null) {
                return;
            }
            Fragment1 fragment1 = new Fragment1();
            getFragmentManager().beginTransaction()
                    .add(R.id.container, fragment1, "tag1").commit();
        }
    }

Fragment1 には、押されたときにコールバックが Activity に送信されるボタンがあります。MainActivity のコールバックは次のとおりです。

@Override
    public void OnButtonClick() {

        if (getFragmentManager().findFragmentById(R.id.fragment2) == null) {
            Log.d("mytag", "one pane");
        }else{
            Log.d("mytag", "two pane");
        }
    }

プログラムを縦向きで起動してボタンを押すと、logcatに「1つのペイン」が出力されます。

しかし、横向きに回転し、すぐに縦向きに回転してからボタンを押すと、「2つのペイン」がlogcatに出力されます。なぜこれが起こるのですか?

4

0 に答える 0