Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to navigate from bottomsheetdialog to fragment using navigation?

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>
like image 340
Liam Avatar asked Jan 11 '20 16:01

Liam


1 Answers

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);
}
like image 130
ianhanniballake Avatar answered Sep 28 '22 04:09

ianhanniballake