ネストされたフラグメントのバックスタックの方法に問題があり、提供されたヘルプに最も感謝しています。
フラグメント A とフラグメント B があります。フラグメント A には別のフラグメント (ButtonFragment) が含まれています。アクティビティの onCreate で、フラグメント A をロードし、フラグメント B に切り替えます。(バックスタックから) フラグメント A に戻ると、次のエラーが発生します。
例外ディスパッチ終了シグナル。
E/MessageQueue-JNI(6330): MessageQueue コールバックの例外: handleReceiveCallback
E/MessageQueue-JNI(6330): android.view.InflateException: バイナリ XML ファイルの 16 行目: クラス フラグメントの拡張中にエラーが発生しました
...
原因: java.lang.IllegalArgumentException: バイナリ XML ファイル 16 行目: ID 0x7f080000、タグ null、または親 ID 0x0 が com.example.fragmentnavigation.MainActivity$ButtonFragment の別のフラグメントと重複しています
サブフラグメントがなければ、ナビゲーションは機能します。ChildFragmentManager の処理を追加する必要があるかもしれませんが、何をどこで処理するのかわかりません。あなたが私を助けてくれることを願っています。
メインレイアウト
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name" />
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="com.example.fragmentnavigation.MainActivity"
tools:ignore="MergeRootFrame" />
</LinearLayout>
フラグメント A レイアウト
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.fragmentnavigation.MainActivity$FragmentA" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<fragment
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.example.fragmentnavigation.MainActivity$ButtonFragment"/>
</LinearLayout>
主な活動
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new FragmentA()).commit();
}
}
...
private void switchToB() {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.container, new BFragment());
ft.addToBackStack(null);
ft.commit();
}
フラグメントA
public static class FragmentA extends Fragment {
public FragmentA() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
ボタンフラグメント
public static class ButtonFragment extends Fragment {
public ButtonFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.buttonfragment, container,
false);
return rootView;
}
}
@arun-kumar に感謝する特別なソリューション
フラグメントに関する非常に優れた概要 https://github.com/codepath/android_guides/wiki/Creating-and-Using-Fragments
フラグメント A レイアウト
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.fragmentnavigation.MainActivity$FragmentA" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<FrameLayout
android:id="@+id/child_fragment_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
フラグメント A の onCreate
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
Fragment childFragment = new ButtonFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.replace(R.id.child_fragment_container, childFragment).commit();
return rootView;
}