Thread
#compose
    d

    dagomni

    2 years ago
    Any ETA on official navigation/backstack? 😅
    i

    Ian Lake

    2 years ago
    https://issuetracker.google.com/issues/161472731 is the issue to star for progress updates
    Javier

    Javier

    2 years ago
    @Ian Lake is it available in navigation 2.4 in the androidx snapshot repo?
    i

    Ian Lake

    2 years ago
    It is a separate
    androidx.compose.navigation
    artifact group at the moment
    But yes, there is a snapshot available in pre-dev level of quality
    Javier

    Javier

    2 years ago
    I have a few questions: • Where can I give feedback about improvements for androidx.dev web? (actually, if I want to find
    ui/androidx/compose/navigation
    , I have to click "next page" a few minutes, and in the url I can't put the page number too because it is using a token • I didn't see samples with args, so I guess they are not supported at this moment
    i

    Ian Lake

    2 years ago
    Alexander Karkossa

    Alexander Karkossa

    2 years ago
    Is there a way to use a Crossfade with the current implementation of NavHost?
    i

    Ian Lake

    2 years ago
    No, that's one of the topics we're going to be working on prior to any release
    Alexander Karkossa

    Alexander Karkossa

    2 years ago
    Are there any plans to support Hilt-ViewModelInject for the upcoming navigation library for compose?
    Sergey Y.

    Sergey Y.

    2 years ago
    Injection into a Composable function? Please no
    i

    Ian Lake

    2 years ago
    Navigation in Compose supports ViewModels at the screen level. Navigation doesn't care/control/have anything to do with where you get your ViewModel Factory from, Hilt or not
    Alexander Karkossa

    Alexander Karkossa

    2 years ago
    Hmm ok, maybe I have to check again. Currently he is searching for an empty constructor in my ViewModels. It seems that the HiltViewModelFactory was not used
    i

    Ian Lake

    2 years ago
    Sounds like it isn't using your Hilt provided factory at all. Have you tried the instructions in the Hilt guide? https://developer.android.com/training/dependency-injection/hilt-jetpack#viewmodel-navigation
    Alexander Karkossa

    Alexander Karkossa

    2 years ago
    Jup. ViewModel Injection outside of compose works fine. Don’t know if there is a Problem with NavBackStackEntry within the androidx navigation library. The getDefaultViewModelProviderFactory created the SavedStateVieeModelFactory
    In NavHost compose the ViewModelStoreOwnerAmbient provides the currentNavBackStackEntry. I thought that would cause these problem
    Javier

    Javier

    2 years ago
    @Ian Lake so if I annotate the activity as EntryPoint, hilt should work with compose ootb (using the ViewModel function inside the composable)?
    i

    Ian Lake

    2 years ago
    More official documentation on Hilt+Compose is still to come, but if you use the containing Activity/Fragment's
    defaultViewModelProviderFactory
    and Compose's viewModel function, then it'll certainly use Hilt's factory
    Assuming your containing Activity/Fragment is correctly set up in Hilt land (i.e., as an EntryPoint)
    That's all independent of Navigation
    Alexander Karkossa

    Alexander Karkossa

    2 years ago
    Hmm I did all of this. I will try it tomorrow again.
    @Ian Lake Tried it again.  Test 1 (worked): val viewModel: MyViewModel by viewModels() in the MainActivity injects the ViewModel with Hilt.  Test 2 (worked): val viewModel: MyViewModel = viewModel() within compose Test 3(crash): val viewModel: MyViewModel = viewModel() within NavHost. NewInstanceFactory tries to create an instance. No zero argument constructor.
    i

    Ian Lake

    2 years ago
    You did read the docs I linked above right? You have to pass in the factory when in Navigation
    Hilt only does things at the Activity or Fragment layer, it has no idea about the Navigation layer, so when you create ViewModels that are scoped at the Navigation layer, you need to be explicit that you want to pull a ViewModelProvider.Factory out from your Activity/Fragment
    This is part of what the Hilt team is looking at for best practices for Hilt in Compose
    Alexander Karkossa

    Alexander Karkossa

    2 years ago
    You were right. didn’t read correctly. Worked with using the defaultViewModelProviderFactory from the Activity. But now it crashes, when i enter a subview second time. SavedStateProvider with given key is already registered. Think i am still doing something wrong.
    i

    Ian Lake

    2 years ago
    50/50 chance that it isn't your fault too. There's a reason that Navigation and Hilt isn't part of the current alpha release yet - still more to do 😄
    m

    michaelevans

    1 year ago
    @Alexander Karkossa did you ever solve this?
    Alexander Karkossa

    Alexander Karkossa

    1 year ago
    Hey, this was a while ago, so I’ll try to get it together once. I found a way to use
    Hilt
    ViewModels in
    NavHost
    , but I haven’t checked for a long time to see if it works without that workaround in the meantime.
    class ViewModel(
      private val repository: Repository,
    ) : ViewModel() {
    
    }
    
    class ViewModelFactory(private val context: Context) : ViewModelProvider.Factory {
    
      @EntryPoint
      @InstallIn(ApplicationComponent::class)
      interface ViewModelProviderEntryPoint {
        fun repository(): Repository
      }
    
      @Suppress("UNCHECKED_CAST")
      override fun <T : ViewModel?> create(modelClass: Class<T>): T {
        val provider = EntryPointAccessors.fromApplication(context, ViewModelProviderEntryPoint::class.java)
        return ViewModel(
          repository = provider.repository(),
        ) as T
      }
    }
    
    @Composable
    fun ComposeView(
      modifier: Modifier = Modifier
    ) {
      val viewModel: ViewModel = viewModel(factory = ViewModelFactory(AmbientContext.current))
    }