I'm experiencing a weird issue here with Safe Args when trying to pass an argument of a type that's an enum defined inside such fragment class. Some code, for clarification.
This is the enum, as it is defined inside the "EvaluationFragment" kotlin class:
enum class EnrollmentOutcome(vararg labels: String) {
Accepted("AUTO_APPROVED"), Rejected("AUTO_DECLINED"), Reviewing("REVIEW_REQUIRED"), Unknown(
"UNAVAILABLE"
);
private val outcomeLabels: MutableList<String> = ArrayList()
companion object {
fun fromString(label: String): EnrollmentOutcome {
return when {
Accepted.outcomeLabels.contains(label) -> {
Accepted
}
Rejected.outcomeLabels.contains(label) -> {
Rejected
}
Reviewing.outcomeLabels.contains(label) -> {
Reviewing
}
else -> {
Unknown
}
}
}
}
init {
outcomeLabels.addAll(listOf(*labels))
}
}
This is the involved fragment entry as defined in nav_graph.xml file:
<fragment
android:id="@+id/evaluationFragment"
android:name="com.whatever.domain.EvaluationFragment"
android:label="fragment_evaluation"
tools:layout="@layout/fragment_evaluation" >
<argument
android:name="outcome"
app:argType="com.whatever.domain.EvaluationFragment$EnrollmentOutcome"
android:defaultValue="Unknown" />
</fragment>
Then I have this inside the EvaluationFragment class as a private field:
private val args: EvaluationFragmentArgs by navArgs()
And this inside a method in such class too, that's where the error happens:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
when (args.outcome) {
EnrollmentOutcome.Accepted -> {
outcomeTitle.setText(R.string.registration_accepted)
outcomeIcon.setImageDrawable(
ContextCompat.getDrawable(
requireContext(),
R.drawable.ic_check_circle_green_48dp
)
)
outcomeDescription.setText(R.string.registration_description_accepted)
}
EnrollmentOutcome.Rejected -> {
outcomeTitle.setText(R.string.registration_declined)
outcomeDescription.setText(R.string.registration_description_declined)
outcomeIcon.setImageDrawable(
ContextCompat.getDrawable(
requireContext(),
R.drawable.ic_error_red_48dp
)
)
}
EnrollmentOutcome.Reviewing -> {
outcomeTitle.setText(R.string.registration_reviewing)
outcomeDescription.setText(R.string.registration_description_reviewing)
outcomeIcon.setImageDrawable(
ContextCompat.getDrawable(
requireContext(),
R.drawable.ic_info_blue_48dp
)
)
}
EnrollmentOutcome.Unknown -> {
outcomeTitle.setText(R.string.registration_error)
outcomeDescription.setText(R.string.registration_description_error)
outcomeIcon.setImageDrawable(
ContextCompat.getDrawable(
requireContext(), R.drawable.ic_error_red_48dp
)
)
}
}
}
Now the problem is that in each of these when branches the enum item is underlined in red by Android Studio and show this error:
Comparison of incompatible enums '[ERROR : com.whatever.domain.EvaluationFragment$EnrollmentOutcome]' and 'EvaluationFragment.EnrollmentOutcome' is always unsuccessful
In spite of that, the code not only does build perfectly well but it works exactly as expected.
In any case I'd like to know why is Android Studio throwing an error there and how I could get rid of it and still use the enum in safe args (I mean, I could workaround it easily by passing a String instead and doing the required conversions for which I already have a method in the enum itself, but that kinda defeat the point of using Safe Args, doesn't it?).
Thanks a lot in advance, Fran