I can't find the fragment in the navHostFragment

94 Views Asked by At

I have a Patient activity with 3 fragments in a fragmentcontainerview with navgraph and I have Speciality activity that is about a listview of specialities I want to send and intent from the speciality activity to the first fragment in the main activity

Patient XML:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
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=".PatientHome">

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/bottomNavigationView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#fff"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:menu="@menu/patient_bottom_menu"
    app:itemTextColor="@color/bottom_nav_color"
    app:itemIconTint="@color/bottom_nav_color"/>

<androidx.fragment.app.FragmentContainerView
    android:id="@+id/patientfragmentContainerView"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:defaultNavHost="true"
    android:tag="customTag"
    app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:navGraph="@navigation/patient_nav" />
</androidx.constraintlayout.widget.ConstraintLayout>

The Navgraph:

<?xml version="1.0" encoding="utf-8"?>
<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/patient_nav"
app:startDestination="@id/firstPatientFragment">

<fragment
    android:id="@+id/firstPatientFragment"
    android:name="com.topaholic.hptracker.FirstPatientFragment"
    android:label="fragment_first_patient"
    tools:layout="@layout/fragment_first_patient" />
<fragment
    android:id="@+id/secondPatientFragment"
    android:name="com.topaholic.hptracker.SecondPatientFragment"
    android:label="fragment_second_patient"
    tools:layout="@layout/fragment_second_patient" />
<fragment
    android:id="@+id/thirdPatientFragment"
    android:name="com.topaholic.hptracker.ThirdPatientFragment"
    android:label="fragment_third_patient"
    tools:layout="@layout/fragment_third_patient" />
</navigation>

So from the activity speciality I'm trying to intent to Patient activity then to the fragment activity

Speciality Activity:

Intent intent = new Intent(SpecialityActivity.this, PatientHome.class);
            Bundle bundle = new Bundle();
            bundle.putString("specialities", 
            getResources().getStringArray(R.array.specialities)[i]);
            intent.putExtras(bundle);
            Log.d("Speciality", "Bundle sent: " + bundle);
            startActivity(intent);

Patient Activity:

Bundle bundle = getIntent().getExtras();
    if (bundle != null) {
        String value = bundle.getString("specialities");
        // pass the data to the fragment using the fragment's arguments
        FragmentManager fragmentManager = getSupportFragmentManager();
        Fragment fragment = fragmentManager.findFragmentById(R.id.firstPatientFragment);
        Log.d("PatientActivity", "Fragment found: " + fragment);
        if (fragment != null) {
            fragment.getArguments().putAll(bundle);
        }
    }

FirstFragment in Patient Activity:

Bundle bundle = getArguments();
    Log.d("MyFragment", "Bundle received: " + bundle);
    if (bundle != null) {
        String value = bundle.getString("specialities");
        specbtn.setText(value);
    }

Here are the Logs I get: Speciality Activity: Bundle sent: Bundle[{specialities=Diagnostic Radiology}] Patient Activity: Fragment found: null FirstFragment: Bundle received: null

So idk it is giving that fragment in null in the FragmentContainerView

1

There are 1 best solutions below

0
On BEST ANSWER

You forgot to initialize FragmantContainerView with NavGraph when accessing the fragment in patient activity.