I get a weird bug when I implement the top answer to this question. Like I have the following Parcelable to pass between two screens of my Jetpack Compose app:
@Parcelize
data class EdgeParcelable(
val node: NodeParcelable?,
val cursor: String?,
) : Parcelable
And as the accepted Answer says I have implemented a custom NavType:
class EdgeParcelableType : NavType<EdgeParcelable>(isNullableAllowed = false) {
override val name: String
get() = "edge"
override fun get(bundle: Bundle, key: String): EdgeParcelable? {
return bundle.getParcelable(key)
}
override fun parseValue(value: String): EdgeParcelable {
return Gson().fromJson(value, EdgeParcelable::class.java)
}
override fun put(bundle: Bundle, key: String, value: EdgeParcelable) {
bundle.putParcelable(key, value)
}
}
And in my Composable function where I create the NavHost I have:
@Composable
fun MyApp(viewModel: MyViewModel, modifier: Modifier = Modifier) {
val navController = rememberNavController()
Scaffold(
modifier = modifier.fillMaxSize(),
topBar = { MyTopAppBar(
currentScreen=currentScreen,
canNavigateBack = navController.previousBackStackEntry != null,
navigateUp = { navController.navigateUp() }
) }
) { innerPadding ->
NavHost(
navController = navController,
startDestination = "home",
modifier = Modifier.padding(innerPadding)
) {
composable(route = "home") {
val lazyPagingItems = viewModel.Items()
HomeScreen(
lazyPagingItems = lazyPagingItems,
onTownClicked = { edge: EdgeParcelable ->
val json = Uri.encode(Gson().toJson(edgeParcelable))
navController.navigateSingleTopTo("hotspots/$json")
},
modifier = ...
)
}
composable(
route = "hotspots/{edge}",
arguments = listOf(
navArgument("edge") {
type = EdgeParcelableType()
}
)
) {
val edgeParcelable = it.arguments?.getParcelable<EdgeParcelable>("edge")
HotspotsScreen(edgeParcelable)
}
}
}
}
The code above crashes my Application when I add the lines:
val bsEntry by navController.currentBackStackEntryAsState()
val currentScreen = bsEntry?.destination?.route ?: "Home"
Adding the above lines make the Composable become:
@Composable
fun MyApp(viewModel: MyViewModel, modifier: Modifier = Modifier) {
val navController = rememberNavController()
// Adding these causes a problem...
val bsEntry by navController.currentBackStackEntryAsState()
val currentScreen = bsEntry?.destination?.route ?: "Home"
Scaffold(
modifier = modifier.fillMaxSize(),
topBar = { MyTopAppBar(
currentScreen=currentScreen,
canNavigateBack = navController.previousBackStackEntry != null,
navigateUp = { navController.navigateUp() }
) }
) { innerPadding ->
NavHost(
navController = navController,
startDestination = "home",
modifier = Modifier.padding(innerPadding)
) {
composable(route = "home") {
val lazyPagingItems = viewModel.Items()
HomeScreen(
lazyPagingItems = lazyPagingItems,
onTownClicked = { edge: EdgeParcelable ->
val json = Uri.encode(Gson().toJson(edgeParcelable))
navController.navigateSingleTopTo("hotspots/$json")
},
modifier = ...
)
}
composable(
route = "hotspots/{edge}",
arguments = listOf(
navArgument("edge") {
type = EdgeParcelableType()
}
)
) {
val edgeParcelable = it.arguments?.getParcelable<EdgeParcelable>("edge")
HotspotsScreen(edgeParcelable)
}
}
}
}
Passing my Custom NavType with the following line of code:
arguments = listOf(navArgument("edge") { type = EdgeParcelableType() } )
now crashes my app, by rendering it unusable. The app seems to choke on itself, almost like, the Navigation API does not really understand the new Custom EdgeParcleableType()
or perhaps something is missing that remains to be added to make this EdgeParcelableType
work well with the Navigation API.
I was only able to solve the problem by changing the type above to StringType as follows:
arguments = listOf( navArgument("edge") { type = NavType.StringType }
And passing around strings in the rest of the Composable as follows:
@Composable
fun MyApp(viewModel: MyViewModel, modifier: Modifier = Modifier) {
val navController = rememberNavController()
// Using NavType.StringType allows this work...
val bsEntry by navController.currentBackStackEntryAsState()
val currentScreen = bsEntry?.destination?.route ?: "Home"
Scaffold(
modifier = modifier.fillMaxSize(),
topBar = { MyTopAppBar(
currentScreen=currentScreen,
canNavigateBack = navController.previousBackStackEntry != null,
navigateUp = { navController.navigateUp() }
) }
) { innerPadding ->
NavHost(
navController = navController,
startDestination = "home",
modifier = Modifier.padding(innerPadding)
) {
composable(route = "home") {
val lazyPagingItems = viewModel.Items()
HomeScreen(
lazyPagingItems = lazyPagingItems,
onTownClicked = { edge: EdgeParcelable ->
val json = Uri.encode(Gson().toJson(edgeParcelable))
navController.navigateSingleTopTo("hotspots/$json")
},
modifier = ...
)
}
composable(
route = "hotspots/{edge}",
arguments = listOf( navArgument("edge") {
type = NavType.StringType
}
)
) {
val edgeParcelable = Gson().fromJson(Uri.decode(it.arguments?.getString("edge")), EdgeParcelable::class.java)
HotspotsScreen(edgeParcelable)
}
}
}
}
Then my app worked like a Charm. Sorting this took me like 2 days of trial and error and searching so I hope this can help someone out there if faced with a similar issue...