Home > other >  pass arguments to fragments inside Bottom Navigation using navArgs
pass arguments to fragments inside Bottom Navigation using navArgs

Time:01-05

From Android Studio, I selected Bottom Navigation Activity project. I can compile and successfully run the generated app without any problem. However, I wanted the fragments under the BottomNavigationView to receive arguments, so I added 2 arguments, email and jwt to the starting destination fragment. So here's how my mobile_navigation.xml looks like now:

<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/mobile_navigation"
    app:startDestination="@ id/navigation_home">

    <fragment
        android:id="@ id/navigation_home"
        android:name="com.example.testbottomnavigation.ui.home.HomeFragment"
        android:label="@string/title_home"
        tools:layout="@layout/fragment_home">
        <argument
            android:name="email"
            app:argType="string" />
        <argument
            android:name="jwt"
            app:argType="string" />
    </fragment>

After that I added these codes to my HomeFragment

private val myArgs by navArgs<HomeFragmentArgs>()
override fun onCreateView(){
    val email = myArgs.email
    val jwt = myArgs.jwt
}

and tried to run the app. The app will the crash with the error

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testbottomnavigation/com.example.testbottomnavigation.MainActivity}: android.view.InflateException: Binary XML file line #33 in com.example.testbottomnavigation:layout/activity_main: Binary XML file line #33 in com.example.testbottomnavigation:layout/activity_main: Error inflating class fragment
...
at com.example.testbottomnavigation.MainActivity.onCreate(MainActivity.kt:19)
...
Caused by: java.lang.IllegalArgumentException: Required argument "email" is missing and does not have an android:defaultValue

Basically the app will crash at the line binding = ActivityMainBinding.inflate(layoutInflater) under my MainActivity below

Here's my MainActivity class MainActivity : AppCompatActivity() {

private lateinit var binding: ActivityMainBinding

override fun onCreate(savedInstanceState: Bundle?) {
     super.onCreate(savedInstanceState)

     binding = ActivityMainBinding.inflate(layoutInflater)
     setContentView(binding.root)

     val navView: BottomNavigationView = binding.navView    
     val navController = findNavController(R.id.nav_host_fragment_activity_main)
     val appBarConfiguration = AppBarConfiguration(setOf(
            R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications))
        setupActionBarWithNavController(navController, appBarConfiguration)
        navView.setupWithNavController(navController)
    }
}

So looking at the code above, where and how should I set the arguments that I needed to pass to HomeFragment?

CodePudding user response:

I think you just need to add a default value:

 <argument
    android:name="email"
    app:argType="string"
    android:defaultValue="[email protected]"/>

CodePudding user response:

You didnt tranfer any values,so you should use defultvalue on argument of navigation And You can see below link for transfer data and navigate enter link description here

  • Related