OiO.lk Blog Android AndroidTest IllegalStateException: Given component holder class androidx.activity.ComponentActivity does not implement interface dagger.hilt
Android

AndroidTest IllegalStateException: Given component holder class androidx.activity.ComponentActivity does not implement interface dagger.hilt


I’ve seen a few things now where you are trying to inject a viewModel using hilt navigation and it is giving errors.

This question is the most similar.

I have an abstract viewModel which is being inherited by 5 different viewmodels and the viewModel is scoped to the backstack. Everything works fine on the emulator and an real device, just not in UI tests.

Here is the abstract viewModel:

abstract class ProposeNewGoalViewModel : ViewModel() {

    private val proposedGoalUiDataInit = ProposedGoalUiData(
        targetAmount = null,
        startYear = LocalDateTime.now().year.toString(),
        singleOrRecurring = SingleOrRecurring.SINGLE,
        goalName = null,
        wantWishOrNeed = WantWishOrNeed.NEED,
        yearCareProvidedStart = LocalDateTime.now().year.toString()
    )



 open fun resetState() { 
    ......
 }


//methods
.......

}

Here is one of the viewModels:

@HiltViewModel
class Category1OneTimeOrRecurringViewModel @Inject constructor() : ProposeNewGoalViewModel() {

    override fun resetState() {
        super.resetState()
        showRecurrenceFormFields.update { false }
        onTargetAmountChanged("")
        onStartYearChanged("")
        onFrequencyChanged("")
        onNumberOfOccurrencesChanged("")
    }
}

So nothing earth shattering. Here’s where I get the error in my androidTest:

@AndroidEntryPoint
class GoalsFragment() { 

@AndroidEntryPoint
class GoalsFragment : BaseFragment() {

.......

override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View {
    return ComposeView(requireActivity()).apply {
        setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)
        setContent {
            GoalsNav(
                goalsViewModel = goalsViewModel,
                proposeNewGoalViewModel = proposeNewGoalScreenViewModel,
                popBackStack = { activity?.finish() },
                navigateToAccounts = {
                    landingViewModel.performNavigationSubject.onNext(R.id.accounts)
                },
                navigateToMyTeamMessage = { messageType ->
                    findNavController().navigateSafe(
                        R.id.action_goalsLanding_to_myTeamNewMessage,
                        MyTeamNewMessageFragmentArgs(
                            messageType
                        ).toBundle()
                    )
                },
                fragmentNavController = findNavController()
            )
        }
    }
}
}

@SuppressLint("NavigateSafe")
@Composable
fun GoalsNav(
    goalsViewModel: GoalsViewModel,
    proposeNewGoalViewModel: ProposeNewGoalScreenViewModel,
    navController: NavHostController = rememberNavController(),
    popBackStack: () -> Unit,
    navigateToAccounts: () -> Unit,
    navigateToMyTeamMessage: (MyTeamMessageType) -> Unit,
    fragmentNavController: NavController
) {
    EJTheme {
        NavHost(
            navController = navController,
            startDestination = GOALS_ROUTE
        ) {

        ////////////////////THIS WORKS FINE IN TEST    
        composable(route = PROPOSE_NEW_GOAL_ROUTE) { backStackEntry ->
            ProposeNewGoalScreen(
                viewModel = hiltViewModel<ProposeNewGoalScreenViewModel>(backStackEntry),
                navigateBack = { navController.popBackStack() },
                onNavigateToGoalType = {
                    navController.navigate(
                        "$FILL_OUT_PROPOSED_NEW_GOAL_ROUTE/${it.goalType}"
                    )
                },
            )}
        
        /////////////////ILLEGAL STATE
        composable(route = "$FILL_OUT_PROPOSED_NEW_GOAL_ROUTE/{$PROPOSE_GOAL_TYPE_ARG}") { backStackEntry ->

            val goalTypeArg =
                backStackEntry.arguments?.getString(PROPOSE_GOAL_TYPE_ARG)?.toInt()
            val selectedGoalTypeEnum = goalTypeArg?.let { GoalType.getGoal(it) }
            val proposeNewGoal = selectedGoalTypeEnum?.let { ProposeNewGoal.getGoalBasedOnGoalType(it) }
            val proposeNewGoalViewModelForForm = when (proposeNewGoal?.category) {
                Category.CATEGORY_1_SINGLE_OR_REOCCURRING -> {
                    hiltViewModel(backStackEntry) as Category1OneTimeOrRecurringViewModel. ///////error happens here
                }
                Category.CATEGORY_2_EDUCATION ->  {
                    hiltViewModel(backStackEntry) as Category2EducationViewModel
                }
                Category.CATEGORY_3_SINGLE_OCCURRENCE -> {
                    hiltViewModel(backStackEntry) as Category3SingleTimeViewModel
                }
                Category.CATEGORY_4_PROVIDE_CARE -> {
                    hiltViewModel(backStackEntry) as Category4ProvideCareViewModel
                }
                Category.CATEGORY_5_BEQUEST -> {
                    hiltViewModel(backStackEntry) as Category5LeaveBequestViewModel
                }
                null -> hiltViewModel(backStackEntry) as Category1OneTimeOrRecurringViewModel
            }
            ProposeNewGoalForm(
                viewModel = proposeNewGoalViewModelForForm,
                navigateBack = { navController.navigate(PROPOSE_NEW_GOAL_ROUTE) { popUpTo(PROPOSE_NEW_GOAL_ROUTE) { inclusive = true } } },
                onNavigateBackToGoals = navigateToGoals,
                validateForm = {
                    proposeNewGoalViewModelForForm.validateForm()
                },
                selectedGoalType = goalTypeArg,
                onNavigateToConfirmation = { navController.navigate("$FILL_OUT_PROPOSED_NEW_GOAL_ROUTE_CONFIRMATION/$goalTypeArg") },
            )
        }
}
}

I tried making the abstract class an open class with HiltViewModel and no dice. I think it’s a bug.



You need to sign in to view this answers

Exit mobile version