Bottom Sheet Fragment causes error when called the second time

3

I have a class BottomSheetFragment extends BottomSheetDialogFragment, which contains sub-fragments, and I call it from a fragment. The problem is that when I call it the second time, my app crashes with Binary XML file line #69: Duplicate id 0x7f090071, tag null, or parent id 0xffffffff with another fragmenterror.

Bottom sheet class:

public class BottomSheetFragment extends BottomSheetDialogFragment {
    public BottomSheetFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_bottom_sheet, container, false);

        return v;
    }


}

In its XML it contains a sub-fragment public class CreateNotesFragment extends android.support.v4.app.Fragment

The BottomSheetFragment class is called in another fragment on button click like this:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_all_works, container, false);

        createNew = view.findViewById(R.id.add_file_btn);


        createNew.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MainActivity mainActivity = (MainActivity)getActivity();
                mainActivity.showBottomSheetDialogFragment();
            }
        });


        return view;
    }

And here is the method in MainActivity which shows the bottom sheet:

public void showBottomSheetDialogFragment() {
            bottomSheetFragment = new BottomSheetFragment();
            bottomSheetFragment.show(getSupportFragmentManager(), bottomSheetFragment.getTag());
    }

I assume that the CreateNotesFragment no longer exists when I call the BottomSheetFragment for the second time for some reason, but how can I fix it?

Thank you.


Update
Here is the line 69 in fragment_bottom_sheet.xml

<fragment
            android:id="@+id/notes_fragment"
            android:name="com.app.parsec.secondary_fragments.CreateNotesFragment"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/divider9"
            tools:layout="@layout/fragment_create_notes" />

And the whole XML to make you sure that there is no ID duplicates:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".secondary_fragments.BottomSheetFragment"
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
    android:id="@+id/wasd">


    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:id="@+id/notes_btn"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="@color/colorPrimary"
            android:drawableTop="@drawable/ic_note"
            android:foreground="?attr/selectableItemBackgroundBorderless"
            android:padding="10dp"
            android:text="НОТЫ"
            app:layout_constraintBottom_toBottomOf="@+id/text_btn"
            app:layout_constraintEnd_toStartOf="@+id/text_btn"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            android:id="@+id/text_btn"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="@color/colorAccentDark"
            android:drawableTop="@drawable/ic_t"
            android:foreground="?attr/selectableItemBackgroundBorderless"
            android:padding="10dp"
            android:text="ТЕКСТ"
            app:layout_constraintEnd_toStartOf="@+id/project_btn"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toEndOf="@+id/notes_btn"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            android:id="@+id/project_btn"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="@color/colorAccentDark"
            android:drawableTop="@drawable/ic_shape_1"
            android:foreground="?attr/selectableItemBackgroundBorderless"
            android:padding="10dp"
            android:text="ПРОЕКТ"
            app:layout_constraintBottom_toBottomOf="@+id/text_btn"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toEndOf="@+id/text_btn"
            app:layout_constraintTop_toTopOf="parent" />

        <View
            android:id="@+id/divider9"
            android:layout_width="0dp"
            android:layout_height="3dp"
            android:background="@color/colorPrimary"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/text_btn" />

        <fragment
            android:id="@+id/notes_fragment"
            android:name="com.app.parsec.secondary_fragments.CreateNotesFragment"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/divider9"
            tools:layout="@layout/fragment_create_notes" />


    </android.support.constraint.ConstraintLayout>
</FrameLayout>

I also noticed that if I remove this fragment from XML, I can open BottomSheetFragment without any crashes, so the problem must be it the sub-fragment.


Solved
The solution is adding fragments into a fragment dynamically, not through XML. So, my BottomSheetFragment now looks like this:

public class BottomSheetFragment extends BottomSheetDialogFragment {
    public BottomSheetFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_bottom_sheet, container, false);
        getChildFragmentManager().beginTransaction().replace(R.id.fragment_here, new CreateNotesFragment()).commit();
        return v;
    }


}
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".secondary_fragments.BottomSheetFragment"
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
    android:id="@+id/wasd">


    <android.support.constraint.ConstraintLayout
        android:id="@+id/layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:id="@+id/notes_btn"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="@color/colorPrimary"
            android:drawableTop="@drawable/ic_note"
            android:foreground="?attr/selectableItemBackgroundBorderless"
            android:padding="10dp"
            android:text="НОТЫ"
            app:layout_constraintBottom_toBottomOf="@+id/text_btn"
            app:layout_constraintEnd_toStartOf="@+id/text_btn"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            android:id="@+id/text_btn"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="@color/colorAccentDark"
            android:drawableTop="@drawable/ic_t"
            android:foreground="?attr/selectableItemBackgroundBorderless"
            android:padding="10dp"
            android:text="ТЕКСТ"
            app:layout_constraintEnd_toStartOf="@+id/project_btn"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toEndOf="@+id/notes_btn"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            android:id="@+id/project_btn"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="@color/colorAccentDark"
            android:drawableTop="@drawable/ic_shape_1"
            android:foreground="?attr/selectableItemBackgroundBorderless"
            android:padding="10dp"
            android:text="ПРОЕКТ"
            app:layout_constraintBottom_toBottomOf="@+id/text_btn"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toEndOf="@+id/text_btn"
            app:layout_constraintTop_toTopOf="parent" />

        <View
            android:id="@+id/divider9"
            android:layout_width="0dp"
            android:layout_height="3dp"
            android:background="@color/colorPrimary"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/text_btn" />


        <FrameLayout
            android:id="@+id/fragment_here"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/divider9">

        </FrameLayout>


    </android.support.constraint.ConstraintLayout>
</FrameLayout>
java
android
android-fragments
bottom-sheet
asked on Stack Overflow Apr 5, 2019 by Calamity • edited Apr 5, 2019 by Calamity

2 Answers

1

You are trying to open the same fragment. The current fragment should be dismissed. bottomSheetFragment.dismiss(); then bottomSheetFragment.show(getSupportFragmentManager(), tag)

And you can try to give a different tag to fragment.

public void showBottomSheetDialogFragment() {
       bottomSheetFragment = new BottomSheetFragment();
       bottomSheetFragment.show(getSupportFragmentManager(), bottomSheetFragment.getTag());
}

Edit :

This error occurs on Nested Fragments which has been defined in layout, try to remove your Fragment from XML Layout and replace it with a FrameLayout then instantiate your Fragment dynamically in code.

answered on Stack Overflow Apr 5, 2019 by Beyazid • edited Apr 5, 2019 by Beyazid
0

You are calling fragment tag inside XML file of YourFragment, basically, it's a wrong way to add bottom sheet to fragment. Here is the way you can do it:

Call this method on botton Click:

callToGuideShareDialog();

Inflate the bottom Sheet View inside this method

private void callToGuideShareDialog() {
final View viewBottom = DataBindingUtil.inflate(getLayoutInflater(), R.layout.------, null, false).getRoot();
            ImageView imageView = viewBottom.findViewById(R.id.img_size_guide_share);
            TextView textViewMsg = viewBottom.findViewById(R.id.----);
            viewBottom.findViewById(R.id.-----).setOnClickListener(this);
            TextView textViewMeasurement = viewBottom.findViewById(R.id.----);

        sheetDialog = new BottomSheetDialog(context);
        sheetDialog.setContentView(viewBottom);

        viewBottom.post(new Runnable() {
            @Override
            public void run() {
                BottomSheetBehavior mBehavior = BottomSheetBehavior.from((View) viewBottom.getParent());
                mBehavior.setPeekHeight(viewBottom.getHeight());
            }
        });

        if (sheetDialog != null)
            sheetDialog.show();
}
answered on Stack Overflow Apr 5, 2019 by Venky

User contributions licensed under CC BY-SA 3.0