I cannot get bundle (argument) value in my destination fragment, always return null.
navigation file
Commented.
<fragment
android:id="@+id/nav_home"
android:name="com.my.app.ui.home.HomeFragment"
android:label="@string/menu_home"
tools:layout="@layout/fragment_home" >
<argument android:name="projectSlugArgument" />
<action
android:id="@+id/action_nav_home_to_projectDetailsFragment"
app:destination="@id/projectDetailsFragment" />
<argument android:name="searchArgument" /> // <------- sending argument
<action
android:id="@+id/action_nav_home_to_searchFragment"
app:destination="@id/searchFragment" />
</fragment>
<fragment
android:id="@+id/projectDetailsFragment"
android:name="com.my.app.ui.ProjectDetails.ProjectDetailsFragment"
android:label="fragment_project_details"
tools:layout="@layout/fragment_project_details">
<argument android:name="projectSlugArgumentValue"
app:argType="com.my.app.classes.slugArg" />
/>
</fragment>
<fragment
android:id="@+id/searchFragment"
android:name="com.my.app.ui.search.SearchFragment"
android:label="fragment_search"
tools:layout="@layout/fragment_search">
// <------- receiving argument --------->
<argument android:name="searchArgumentValue"
app:argType="com.my.app.classes.slugArg" />
</fragment>
sending argument
private fun search(input: String) {
val bundle = bundleOf("searchArgument" to input)
navController.navigate(R.id.action_nav_home_to_searchFragment, bundle)
Log.d("eeet", input) // has data in Logcat
}
receiving data
lateinit var searchArgument: String
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
val root = inflater.inflate(R.layout.fragment_search, container, false)
searchArgument = activity?.intent?.extras?.getString("searchArgument").toString()
Log.d("eeee", searchArgument) // retuning null in Logcat
return root
}
You're trying to access the activities arguments here. What you need to do is access the fragments arguments, which can be done with Fragment.getArguments()
.
However I would recommend taking a deeper look into the navigation component. It isn't recommended to handle the arguments manually, but to use directions .
The Navigation component has a Gradle plugin called Safe Args that generates simple object and builder classes for type-safe navigation and access to any associated arguments. Safe Args is strongly recommended for navigating and passing data, because it ensures type-safety.
See the link above for information on how to add Safe Args for Kotlin. After that is done, your code would look like this:
Sending:
private fun search(input: String) {
val action = HomeFragmentDirections.action_nav_home_to_searchFragment(input)
navController.navigate(action)
Log.d("eeet", input)
}
Receiving:
private val args: SearchFragmentArgs by navArgs()
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
val root = inflater.inflate(R.layout.fragment_search, container, false)
Log.d("eeee", args.searchArgumentValue)
return root
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.