2

I have a fragment name is HomeNavHostFragment và trong layout của nó tôi có chứa một FragmentContainerView: I have a fragment named HomeNavHostFragment and its layout contains a FragmentContainerView And specifically my code snippets are like below:

Kotlin code:

class HomeNavHostFragment : BaseFragment<FragmentHomeNavHostBinding, HomeNavHostViewModel>() {


    override val layoutResourceId: Int = R.layout.fragment_home_nav_host
    override val classTypeOfViewModel: Class<HomeNavHostViewModel> =
        HomeNavHostViewModel::class.java
    override val nestedNavHostFragmentId: Int = R.id.nested_nav_host_fragment_home
    override val navGraphId: Int = R.navigation.nav_graph_home

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

        val mainNavController =
            Navigation.findNavController(requireActivity(), R.id.nav_host_fragment)

        val nestedNavHostFragment =
            childFragmentManager.findFragmentById(nestedNavHostFragmentId) as? NavHostFragment
        navController = nestedNavHostFragment?.navController


        listenOnBackPressed()

    }
    @SuppressLint("BinaryOperationInTimber")
    override fun initView() {
        super.initView()
    }


}

fragment_navhost_home.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>

    </data>

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

        <androidx.fragment.app.FragmentContainerView
            android:id="@+id/nested_nav_host_fragment_home"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:defaultNavHost="false"
            app:navGraph="@navigation/nav_graph_home" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

nav_graph_home.xml:

<?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/fragment_nav_host_home"
    app:startDestination="@id/home_fragment">


    <fragment
        android:id="@+id/home_fragment"
        android:name="com.apps.ultrawallpapers.view.home.HomeFragment"
        android:label="fragment_home"
        tools:layout="@layout/fragment_home" />

</navigation>

And I am facing a problem that I have a data in HomeNavHostFragment I need to send to HomeFragment but I don't know how to do it, even though I tried the below code inside onViewCreated() of HomeNavHostFragment, but it can't solve the problem there, so can someone help me to solve this problem.

     var bundle = bundleOf("KEY" to "SAMPLE")

     mainNavController.setGraph(R.navigation.nav_graph_home,bundle)

My example is following the instructions from this link: Sample

EDIT

With the help of Arpit Shukla I managed to solve the problem, Partly due to my own lack of it, The way to solve that problem is: remove line app:navGraph="@navigation/nav_graph_home in fragment_navhost_home.xml and inside onViewCreated of HomeNavHostFragment you need to add the following code:

 var bundle = bundleOf("KEY" to "SAMPLE")
 navController?.setGraph(R.navigation.nav_graph_home,bundle)

Thang
  • 409
  • 1
  • 6
  • 17
  • 1
    Are you hosting your `NavHostFragment` inside a fragment? – Arpit Shukla Nov 09 '21 at 15:41
  • Yep, i'm hosting NavHostFragment inside a fragment not an activity – Thang Nov 09 '21 at 15:46
  • And how are you navigating to this initial fragment? Using some transaction? – Arpit Shukla Nov 09 '21 at 15:47
  • i'm got it, I'm navigating to HomeNavHostFragment fragment folow by sample as : https://stackoverflow.com/questions/52540303/android-jetpack-navigation-with-viewpager-and-tablayout#:~:text=when%20(position)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%200%20-%3E-,HomeNavHostFragment,-()%0A%20%20%20%20%20%20%20%20%20%20%20%201%20-%3E%20DashBoardNavHostFragment()%0A%20%20%20%20%20%20%20%20%20%20%20%202 – Thang Nov 09 '21 at 15:52
  • `I'm got it` ? Problem solved? – Arpit Shukla Nov 09 '21 at 15:55
  • ohh sorry, no no, i'm just understand about a your question "And how are you navigating to this initial fragment? Using some transaction?" – Thang Nov 09 '21 at 15:59
  • In the link you shared, NavHostFragment is in the activity. Why you have that in a fragment? – Arpit Shukla Nov 09 '21 at 16:01
  • yep,because i am learning about viewpager with jetpack navigation, that's why i have to push NavHostFragment inside Fragment, Also in activity it also contains a NavHostFragment used to manage viewpager, – Thang Nov 09 '21 at 16:09
  • Also in the link I sent is just a tutorial, the specific project of the person who made that tutorial is in this link, you can see why it's NavHostFragment both in the activity and the fragment: https://github.com/SmartToolFactory/NavigationComponents-Tutorials/tree/master/Tutorial6-2NavigationUI-ViewPager2-NestedNavhost – Thang Nov 09 '21 at 16:10
  • Okay. So you have a fragment (FragmentA) which contains a view pager. In the view pager you have few others fragments (FragmentB, FragmentC etc.) Right? – Arpit Shukla Nov 09 '21 at 16:12
  • Yup. right.I have followed that link and it works perfectly but there is only one problem that I don't know how to send data from fragment which contains NavHostFragemtn to first fragment it contains. – Thang Nov 09 '21 at 16:20
  • That is in this sample: from fragment HomeNavHostFragment I want to send a data to HomeFragment1 fragment: https://github.com/SmartToolFactory/NavigationComponents-Tutorials/tree/master/Tutorial6-2NavigationUI-ViewPager2-NestedNavhost – Thang Nov 09 '21 at 16:23
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/239046/discussion-between-arpit-shukla-and-thang). – Arpit Shukla Nov 09 '21 at 16:25
  • Yep, Hope it gets resolved. – Thang Nov 09 '21 at 16:29
  • 1
    Great. You should post your solution so that others could benefit from it. – Arpit Shukla Nov 09 '21 at 16:36
  • Yes, I will, Thank you, the problem is solved, You are a very enthusiastic person, Once again thank you – Thang Nov 09 '21 at 16:43

0 Answers0