Bradleycorn
01/01/2021, 11:47 PMsealed class Screen(val route: String, val title: String, val isTopLevel: Boolean = false) {
object Category: Screen("category/{postType}", "Category") {
// here I define a NavType.EnumType for my enum that is used for an argument.
val postTypeArgType = NavType.EnumType(PostType::class.java)
fun buildRoute(postType: PostType): String {
return "category/${postType.name}"
}
}
// ... more screen routes defined here..
}
Then I use the Category to build the composable() in the NavGraph Builder. And I use the postTypeArgType
which is a NavType.EnumType
from the Category object above to get the postType argument. Is this the right approach?
composable(Screen.Category.route,
arguments = listOf(
navArgument("postType") { type = Screen.Category.postTypeArgType },
navArgument("title") { type = NavType.StringType }
)
) { backStackEntry ->
val vm: CategoryScreenViewModel = navViewModel()
// Is this the right way to get the `postType` argument from the backstack entry?
val postType = Screen.Category.postTypeArgType.get(backStackEntry.arguments?: Bundle(), "postType") ?: PostType.OTHER
CategoryScreen(vm, postType)
}
Ian Lake
01/02/2021, 4:30 AMgetSerializable()
method