Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Navigate with args
class DataSenderFragment : Fragment() {
// some stuff..
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// Set data
findNavController().getBackStackEntry(R.id.gamesFragment).savedStateHandle.set("myData", "Test")
}
}
class GamesFragment : BaseBottomTabFragment() {
// some stuff...
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// some stuff...
val navController = findNavController();
val navBackStackEntry = navController.currentBackStackEntry!!
navBackStackEntry.lifecycle.addObserver(LifecycleEventObserver { _, event ->
if (event == Lifecycle.Event.ON_RESUME
&& navBackStackEntry.savedStateHandle.contains("myData")) {
val result = navBackStackEntry.savedStateHandle.get<String>("myData");
Toast.makeText(requireContext(), result, Toast.LENGTH_SHORT).show()
}
})
}
// some stuff...
}
<fragment
android:id="@+id/dynamicTitleFragment"
android:name="com.faskn.trickynavigationsample.fragments.DynamicTitleFragment"
android:label="{title}"
tools:layout="@layout/fragment_dynamic_title">
<argument
android:name="title"
android:defaultValue="Title"
app:argType="string" />
<action
android:id="@+id/action_dynamicTitleFragment_to_dataSenderFragment"
app:destination="@id/dataSenderFragment"
app:popUpTo="@id/gamesFragment" />
</fragment>
<fragment
android:id="@+id/dataSenderFragment"
android:name="com.faskn.trickynavigationsample.fragments.DataSenderFragment"
android:label="fragment_data_sender"
tools:layout="@layout/fragment_data_sender" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment