Issue
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
}
Solution
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.
Answered By - Henry Twist
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.