Mr.P.
Mr.P.

Reputation: 109

Android Backstack with Nested/Child Fragments

I've got a problem with the way backstack of nested Fragments and would be most grateful for any help that is offered.

I have Fragment A and Fragment B. Fragment A contains an other Fragment (ButtonFragment). In the onCreate of the Activity I load Fragment A, than I switch to Fragment B. When I go back to a Fragment A (out of the backstack) I get following Error.

Exception dispatching finished signal.

E/MessageQueue-JNI(6330): Exception in MessageQueue callback: handleReceiveCallback

E/MessageQueue-JNI(6330): android.view.InflateException: Binary XML file line #16: Error inflating class fragment

...

Caused by: java.lang.IllegalArgumentException: Binary XML file line 16: Duplicate id 0x7f080000, tag null, or parent id 0x0 with another fragment for com.example.fragmentnavigation.MainActivity$ButtonFragment

Without the subfragment the navigation works. Maybe I have to add some ChildFragmentManager handling, but I don't know what and where. I hope you can help me.

MainLayout

<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>

Fragment A Layout

<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>

MainAcitivity

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();
}

Fragment 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;
        }
    }

ButtonFragment

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;
        }
    }

Solution special thanks to @arun-kumar

a really good overview about Fragments https://github.com/codepath/android_guides/wiki/Creating-and-Using-Fragments

Fragment A Layout

<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>

onCreate of Fragment A

@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;
        }

Upvotes: 3

Views: 830

Answers (3)

Fahim
Fahim

Reputation: 12358

Caused by: java.lang.IllegalArgumentException: Binary XML file line 16: Duplicate id 0x7f080000, tag null, or parent id 0x0 with another fragment for com.example.fragmentnavigation.MainActivity$ButtonFragment

Two controls in your layout are of same id, you need to change id of any one of the control

Upvotes: 0

user4420136
user4420136

Reputation:

You have to add child fragment at runtime instead of declaring it in the layout in Fragment A Layout file. I got the same problem on implementing google maps.

Upvotes: 5

hoomi
hoomi

Reputation: 1912

I think the reason behind this exception is that you have got two view elements in your view with id=@+id/button

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/app_name" />

and

<fragment 
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:name="com.example.fragmentnavigation.MainActivity$ButtonFragment"/> 

Try renaming one of them to something unique

Upvotes: 0

Related Questions