同じアクティビティで示される2つの異なるフラグメントを持つテストprojectを作成しました。 1つは風景用で、もう1つは肖像画用です。オリエンテーションを変更すると、異なる断片がロードされませんでした(アンドロイド)
# My unique activity
public class MainActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
# First Fragment
public class LandscapeFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
TextView v = (TextView) inflater.inflate(R.layout.fragment, container, false);
v.setText("LANDSCAPE");
return v;
}
}
# Other Fragment
public class PortraitFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
TextView v = (TextView) inflater.inflate(R.layout.fragment, container, false);
v.setText("PORTRAIT");
return v;
}
}
私は2つのmain.xmlを持っています.1つはレイアウトにあり、もう1つはlayout-land /にあります。各main.xmlは、使用する正しいフラグメントを指しています。
<!-- layout-land/main.xml -->
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment"
android:name="br.luckcheese.test.LandscapeFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp" />
<!-- layout/main.xml -->
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment"
android:name="br.luckcheese.test.PortraitFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp" />
<!-- layout/fragment.xml -->
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="30sp" />
Landscapeでアプリケーションを開くと、LandscapeFragmentが表示され、Portraitで開くとPortraitFragmentが表示されます。ここまでは順調ですね。
ただし、横向きに開いてデバイスを縦向きにすると、横向きの表示が再読み込みされて表示されます。 予測される動作ではありません。 PortraitFragmentが読み込まれているはずです。
デバイスが縦向きで起動し、それを横向きに回転させると、同じことが起こります:LandscapeFragmentが読み込まれる代わりに、PortraitFragmentがリロードされます。
私は間違っていますか?
いいえ、私はデフォルトのマニフェストに追加ユニークなラインアクティビティの 'android:screenOrientation = "sensor"'です – Luckcheese
あなたのアクティビティのonCreate()にブレークポイントを設定して、デバイスを回転させるときにこのメソッドが呼び出されるかどうかを確認しますか? –
はい。いわゆる。私は自分の活動と各断片のログメッセージをonCreateに追加しました。そして、デバイスを回転させると、間違ったフラグメントのonCreateが、アクティビティのonCreateの後に呼び出されます。 – Luckcheese