Home > Back-end >  How is better to inject a ViewModel object when using Jetpack Compose?
How is better to inject a ViewModel object when using Jetpack Compose?

Time:10-10

I need to use navigation, and I also need in each screen to use an instance of SharedViewModel. Here is what I tried.

class MainActivity : ComponentActivity() {
    private lateinit var navController: NavHostController
    private val viewModel: SharedViewModel by viewModels()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            navController = rememberNavController()
            NavGraph(
                navController = navController,
                sharedViewModel = sharedViewModel
            )
        }
    }
}

As you can see, I pass the navController and the sharedViewModel to the NavGraph.

fun NavGraph(
    navController: NavHostController,
    sharedViewModel: SharedViewModel
) {
    NavHost(
        navController = navController,
        startDestination = HomeScreen.route
    ) {
        composable(
            route = HomeScreen.route
        ) {
            HomeScreen(
                sharedViewModel = sharedViewModel
            )
        }
        composable(
            route = ProfileScreen.route
        ) {
            ProfileScreen(
                sharedViewModel = sharedViewModel
            )
        }
    }
}

To be able to use the SharedViewModel in each screen, I pass an instance to each composable function. This operation works fine. However, I read that we can inject in each composable an instance of the view model directly using:

fun HomeScreen(
    viewModel: SharedViewModel = hiltViewModel()
) {
      //...
}

Which approach is better? Is it better to pass an instance of SharedViewModel to all composable functions as in the first approach? Or it is better to inject it directly as in the second?

CodePudding user response:

fun HomeScreen(
viewModel: SharedViewModel = hiltViewModel()
) {
  //...
}

With this approach The instance is not really shared (if you do not pass the argument from calling point since it can be omitted because you mentioned its default value) . You are using default value argument for viewModel: SharedViewModel So its optional to pass it to the Composable method . if you do not pass it and when it runs it will get initialized by Hilt In that Composable Scope Only So not a shared one. you can check this by logging the ViewModel's instance

You can obviously pass it from the calling point but since its a default named_argument its easy to miss to pass it ..

What you can do is just remove the initialization i.e hiltViewModel() from method argument . Then it will be mandatory and you have to pass it while calling the method. Because having a optional parameter doesn't really make sense in this case.

There is an another way of doing it if you do not want to pass it .. We can make hilt to create ViewModel with Activity's context ..

@Composable
fun mainActivity() = LocalContext.current as MainActivity

fun HomeScreen(viewModel: SharedViewModel = hiltViewModel(mainActivity())) {

 }

This way also you will get same instance of VM hence a shared one . In this case this composable is kind of restricted to a Single Activity . So u gotta watch out for it if u use this in some other Activity it will crash with cast exception for MainActivity . But in Single Activity architecture it will be fine or u can just further add the checks for Activity i guess.

  • Related