How to navigate from bottomsheetdialog to fragment using navigation?
Asked Answered
T

1

5

I'm currently making an app with bottomsheet feature which when the user taps an icon(imageview) inside the dialog the fragment will show up. my problem is I cant navigate to fragment b and it's returning me this errorenter image description here

this is my home fragment the one that starts first when the activity was launched

public class HomeFrag extends Fragment {

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_home, container, false);
    ImageView buttonOpenSheet = view.findViewById(R.id.buttonopensheet);

    buttonOpenSheet.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            BottomSheetDialog bottomsheet = new BottomSheetDialog();
            bottomsheet.show(getFragmentManager(), "bottomsheet");
        }
    });

    return view;
}}

bottomsheetdialog.java

public class BottomSheetDialog extends BottomSheetDialogFragment {

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.bottom_sheet, container, false);

    //Views
    ImageView usual = view.findViewById(R.id.usual);
    final NavController navigator = 
Navigation.findNavController(getActivity(), R.id.fragment_host);

    usual.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            navigator.navigate(R.id.action_bottomSheetDialog_to_usualFrag);
            Log.d(TAG, "user clicked" );
        }
    });
    return view;
}}

this is my navigation

<navigation 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:id="@+id/app_nav"
app:startDestination="@id/homeFrag">

<fragment
    android:id="@+id/homeFrag"
    android:name="com.bw.f22920.HomeFrag"
    android:label="fragment_home"
    tools:layout="@layout/fragment_home" />

<dialog
    android:id="@+id/bottomSheetDialog"
    android:name="com.bw.f22920.BottomSheetDialog"
    android:label="bottom_sheet"
    tools:layout="@layout/bottom_sheet">
    <action
        android:id="@+id/action_bottomSheetDialog_to_usualFrag"
        app:destination="@id/usualFrag" />
</dialog>
<fragment
    android:id="@+id/usualFrag"
    android:name="com.bw.f22920.UsualFrag"
    android:label="fragment_usual"
    tools:layout="@layout/fragment_usual" />


</navigation>
Toothless answered 11/1, 2020 at 16:34 Comment(0)
S
10

You're using

@Override
public void onClick(View v) {
    BottomSheetDialog bottomsheet = new BottomSheetDialog();
    bottomsheet.show(getFragmentManager(), "bottomsheet");
}

Which means the NavController has no idea your current destination has changed to the bottom sheet dialog.

You need to call navigate():

@Override
public void onClick(View v) {
    Navigation.findNavController(v).navigate(R.id.bottomSheetDialog);
}
Shiite answered 11/1, 2020 at 22:22 Comment(2)
How to dismiss this bottom sheet while it's showing with nav controller?Kehoe
navController.popBackStack() will dismiss a dialog destination.Shiite

© 2022 - 2024 — McMap. All rights reserved.