0

I am trying to replace a fragment with another fragment in my Bottom Navigation Activity. I want to achieve this on the click of a button in my fragment.

but I am getting the following error:

java.lang.IllegalArgumentException: No view found for id 0x7f090074 for fragment

here's my replace function:

private fun replaceFragment(fragment: Fragment) {
        val fragmentManager = childFragmentManager
        fragmentManager.commit {
            setReorderingAllowed(true)
            replace(R.id.content, fragment)
        }
    }

XML code for Bottom Nav:

<androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true">

            <fragment
                android:id="@+id/home_nav_host"
                android:name="androidx.navigation.fragment.NavHostFragment"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                app:defaultNavHost="true"
                app:layout_constraintBottom_toTopOf="@+id/home_bottom_nav"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:navGraph="@navigation/home_nav" />

            <com.google.android.material.bottomnavigation.BottomNavigationView
                android:id="@+id/home_bottom_nav"
                android:layout_width="match_parent"
                android:layout_height="66dp"
                android:background="#fff"
                android:textAlignment="center"
                app:itemIconTint="@drawable/home_bottom_nav_colour_selector"
                app:itemTextColor="@drawable/home_bottom_nav_colour_selector"
                app:labelVisibilityMode="labeled"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:menu="@menu/bottom_nav_menu" />
        </androidx.constraintlayout.widget.ConstraintLayout>

FirstFragment is to be replaced by SecondFragment and FirstFragment is currently displayed by the BottomNavigation

FirstFragment() code:

class ExpenseFragment : Fragment() {

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

button.setOnClickListener {
            fragment = SecondFragment()
            replaceFragment(fragment)
        }

private fun replaceFragment(fragment: Fragment) {
        val fragmentManager = childFragmentManager
        fragmentManager.commit {
            setReorderingAllowed(true)
            replace(R.id.content, fragment)
        }
    }
}

I've tried all the solutions to similar questions but my problem is still not solved

Levi
  • 187
  • 2
  • 17
  • 2
    have you tried [this](https://stackoverflow.com/a/21029470/14679969)? – Bugs - not a bug Feb 17 '21 at 15:46
  • in the method u suggested what should be the container_id? Is it the same container that i m using for my Bottom Navigation? – Levi Feb 17 '21 at 15:55
  • In your Bottom Navigation Activity you have container, Which is a view where your fragment is displayed. It is usually a FrameLayout or Fragment in your xml. Please paste your xml and java file code and show what you have tried in your questions. – Bugs - not a bug Feb 17 '21 at 15:58
  • here's my xml and kotlin code – Levi Feb 17 '21 at 16:06
  • 1
    fragment tag in your xml with id "home_nav_host" is your container. replace R.id.content in your replaceFragment function with this fragment id – Bugs - not a bug Feb 17 '21 at 16:11
  • thanks!! it worked just fine – Levi Feb 17 '21 at 16:17
  • You're Welcome :) – Bugs - not a bug Feb 17 '21 at 16:20
  • @Bugs-notabug Hey I've got a better solution to this problem i.e. using Navigation Component. As I m using Navigation Component for Bottom Navigation, so simply we can add the fragments we want to open in our navigation graph and can easily navigate from one fragment to another within the same Bottom Navigation – Levi Feb 18 '21 at 07:00

1 Answers1

-1

Ideally you should use activity class to replace fragments in a activity.

fragmentManager.commit {
    setReorderingAllowed(true)
    // Replace whatever is in the fragment_container view with this fragment
    replace<ExampleFragment>(R.id.fragment_container)
}

Add this in a function in activity class and call from the fragment using interface or activity object from the fragment.

Ran94
  • 139
  • 11
  • but we never create an object of activity – Levi Feb 17 '21 at 15:23
  • in my activity i have a BottomNav and from one the fragments in BottomNav I am trying to a open a fragment – Levi Feb 17 '21 at 15:25
  • (-1) it is no where written that replacing fragments within an activity from same activity is ideal. Fragments can be replaced within an activity from other fragments. – Bugs - not a bug Feb 17 '21 at 16:34