On the documentation they say this:
Basic usage Implement ListDetailPaneScaffold as follows:
Use a class that represents the content to be selected. This class should be Parcelable to support saving and restoring the selected list item. Use the kotlin-parcelize plugin to generate the code for you.
@Parcelize
class MyItem(val id: Int) : Parcelable
And later, they use that this way:
val navigator = rememberListDetailPaneScaffoldNavigator<MyItem>()
Why they ask you to use a custom class? Why not simply using an Int representing the current index clicked? Simply this:
val navigator = rememberListDetailPaneScaffoldNavigator<Int>()
Am I losing something? I tested and it works. It even survives configuration changes without having to deal with Parcelable. It simplifies everything. Why adding the complexity of a custom Parcelable Class instead of simply using a Int index?
That is just an example. Of course you could use rememberListDetailPaneScaffoldNavigator<Int>()
instead when the data class only contains an Int. But it shows how you can handle more complex objects as well.
In a classic, non-adaptive layout you would pass a MyItem
object from your list composable to the details composable. MyItem
would be a part of the ui state, containing the details of the currently selected list item. Usually that will be much more than just a simple Int. Imagine a list of persons where the user can select one person to display the person's details. Then you would need a custom object with many more properties than just an id, like a first name, last name, date of birth, country of origin, and so on.
By making that object parcelable it can also be used in the ListDetailPaneScaffold where it needs to be placed on the navigation's backstack (in a serialized form) and saved in the file system as a Bundle to survive configuration changes and system-initiated process death. That are the same requirements for saving an object with rememberSaveable
.
If you do not need to pass anything to the details composable you can use the rememberListDetailPaneScaffoldNavigator
overload without a type parameter.