Using Assisted Injection to inject savedStateHandle in ViewModel returning null argument value

208 Views Asked by At

I try to inject savedStateHandle using Dagger Assisted Injection following this link , I used backstack entry as the "SavedStateRegistryOwner" but somehow my argument in view model class returning as null, but when i print the same value from backstack entry it's having correct value,

class PlannerActivity : ComponentActivity() {

    @Inject
    lateinit var assistedFactory: AvailabilityCheckViewModelAssistedFactory


    @OptIn(ExperimentalAnimationApi::class)
    override fun onCreate(savedInstanceState: Bundle?) {
        initialiseComponent()
        super.onCreate(savedInstanceState)


        setContent {
    ...
         composable(
                            route = "${AvailabilityCheckScreen.route}/{selectedLocations}",
                            arguments = listOf(
                                navArgument("selectedLocations") {
                                    type = SelectedJourneyLocationsDTONavType()
                                }),
                        ) { backStackEntry ->
            
                            val selectedLocations =
                                
    
    //NOTE: this print the correct values
    backStackEntry.arguments?.getParcelable<SelectedJourneyLocationsDTO>("selectedLocations")
            
                            val availabilityCheckViewModel = viewModel(
                                modelClass = AvailabilityCheckViewModel::class.java,
                                factory = assistedFactory.create(backStackEntry)
                            )
            
                            val uiState by availabilityCheckViewModel.uiState.collectAsStateWithLifecycle()
            
                            //availabilityCheckViewModel.setSelectedLocations(selectedLocations)

      }

    }  
}  
        --------------
        class AvailabilityCheckViewModel @Inject constructor(
        
            private val savedStateHandle: SavedStateHandle,
        ) :
            ViewModel() {
        
    // NOTE: Here printing the null values
            init {
                val argument = savedStateHandle.get<SelectedJourneyLocationsDTO>("selectedLocations")
                Timber.d("argument $argument")
            }
        
        }
        
        --------------
        class AvailabilityCheckViewModelFactory @AssistedInject constructor(
            @Assisted owner: SavedStateRegistryOwner
        ) : AbstractSavedStateViewModelFactory(owner, null) {
        
        
            override fun <T : ViewModel> create(
                key: String,
                modelClass: Class<T>,
                handle: SavedStateHandle
            ): T =
                AvailabilityCheckViewModel(
                    handle
                ) as T
        
        }
        
          --------------
        @AssistedFactory
        interface AvailabilityCheckViewModelAssistedFactory {
            fun create(owner: SavedStateRegistryOwner): AvailabilityCheckViewModelFactory
        }
0

There are 0 best solutions below