Skip to content

Instantly share code, notes, and snippets.

@evilthreads669966
Last active February 26, 2022 18:02
Show Gist options
  • Save evilthreads669966/0a9d443b7255c0285744a709eb792a18 to your computer and use it in GitHub Desktop.
Save evilthreads669966/0a9d443b7255c0285744a709eb792a18 to your computer and use it in GitHub Desktop.
type safe way for passing objects when performing a NavHostController's navigation function. saving the arguments to the navcontroller instead.
fun <T: Parcelable> NavHostController.putParcelables(key: String, parcelables: ArrayList<T>) = currentBackStackEntry?.arguments?.putParcelableArrayList(key, parcelables)
fun <T: Parcelable> NavHostController.getParcelables(key: String) = previousBackStackEntry?.arguments?.getParcelableArrayList<T>(key)
fun <T: Parcelable> NavHostController.putParcelable(key: String, parcelable: T) = currentBackStackEntry?.arguments?.putParcelable(key, parcelable)
fun <T: Parcelable> NavHostController.getParcelable(key: String) = previousBackStackEntry?.arguments?.getParcelable<T>(key)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment