Created
May 13, 2020 10:04
-
-
Save furkanaskin/4e86c6d33f72d389f998e57cafa47535 to your computer and use it in GitHub Desktop.
findNavController().getBackStackEntry(R.id.gamesFragment).savedStateHandle.set("myData", "Test")
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment