I have the following snippet that is used for the FragmentStatePageAdapter. I have 2 conditions in the when statement. However, I am trying to avoid the use of the else branch. As I don’t think I want to be return the LoginFragment(). However, if I don’t add the else branch I get the exhaustive error. Is there anything I can do to avoid the use of the else branch?
class LoginScreenPagerAdapter(fragmentManager: FragmentManager)
: FragmentStatePagerAdapter(fragmentManager) {
private val MAX_NUMBER_OF_SCREENS = 2
override fun getItem(position: Int): Fragment {
return when (position) {
LOGIN_FRAGMENT.ordinal -> LoginFragment()
SIGNUP_FRAGMENT.ordinal -> SignupFragment()
else -> {
LoginFragment()
}
}
}
override fun getCount() = MAX_NUMBER_OF_SCREENS
enum class FragmentPosition(position: Int) {
LOGIN_FRAGMENT(0),
SIGNUP_FRAGMENT(1)
}
}
I have also tried using the following with sealed classes and I am getting the same issue. Anything with the way I have constructed the when statement with sealed classes?
class LoginScreenPagerAdapter(fragmentManager: FragmentManager)
: FragmentStatePagerAdapter(fragmentManager) {
private val MAX_NUMBER_OF_SCREENS = 2
override fun getItem(position: Int): Fragment {
return when (position) {
FragmentPositionSealed.Login().position -> LoginFragment()
FragmentPositionSealed.SignUp().position -> SignupFragment()
else -> {
return LoginFragment()
}
}
}
override fun getCount() = MAX_NUMBER_OF_SCREENS
sealed class FragmentPositionSealed {
class Login(val position: Int = 0) : FragmentPositionSealed()
class SignUp(val position: Int = 1) : FragmentPositionSealed()
}
}