Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
findNavController().getBackStackEntry(R.id.gamesFragment).savedStateHandle.set("myData", "Test")
class BottomSheetSampleFragment : BaseBottomSheetFragment() {
private var _binding: FragmentBottomSheetSampleBinding? = null
private val binding get() = _binding!!
val timerTotal = 600000L // 10 Min
lateinit var timer: CountDownTimer
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog = super.onCreateDialog(savedInstanceState) as BottomSheetDialog
dialog.setOnShowListener {
val bottomSheet =
(it as BottomSheetDialog).findViewById<View>(com.google.android.material.R.id.design_bottom_sheet) as FrameLayout?
val behavior = BottomSheetBehavior.from(bottomSheet!!)
behavior.state = BottomSheetBehavior.STATE_EXPANDED
behavior.isDraggable = false
}
return dialog
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
_binding = FragmentBottomSheetSampleBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
timer = object : CountDownTimer(timerTotal, 1000) {
override fun onFinish() {
}
override fun onTick(millisUntilFinished: Long) {
binding.textViewCounter.text =
TimerFormatter(
millisUntilFinished
).getRemainingTimeText()
}
}.start()
binding.buttonAddAnotherFragment.setOnClickListener {
findNavController().navigate(R.id.action_bottomSheetSampleFragment_self)
}
findNavController().getBackStackEntry(R.id.gamesFragment).savedStateHandle.set("myData", "Test")
}
override fun onDestroyView() {
super.onDestroyView()
timer.cancel()
_binding = null
}
}
class GamesFragment : BaseBottomTabFragment() {
private var _binding: FragmentGamesBinding? = null
private val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
_binding = FragmentGamesBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.buttonReplaceFragment.setOnClickListener {
navigateWithAction(
GamesFragmentDirections.actionGamesFragmentToDynamicTitleFragment(
"Replace"
)
)
}
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()
}
})
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}
<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"
app:argType="string"
android:defaultValue="Title" />
<action
android:id="@+id/action_dynamicTitleFragment_to_bottomSheetSampleFragment"
app:destination="@id/bottomSheetSampleFragment"
app:popUpTo="@id/gamesFragment"/>
</fragment>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment