1

I am working with JetpackCompose and I have a lifecycle Observable BatteryBroadcast class which is Injected in MainActivity using Hilt and I want to pass data from BatteryBroadcast to the ViewModel to use that data in Composable Screens. So how can I use the Same ViewModel Instance in all composable screens of activity?. I am using NavHost for composable screens.

class MainActivity : ComponentActivity() {

    @Inject
    lateinit var batteryBroadcast: BatteryBroadcast

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        
        setContent {
            BatteryAlarmGoldTheme {

                val homeViewModel = hiltViewModel<HomeViewModel>()
                lifecycle.addObserver(batteryBroadcast)
                homeViewModel.setBatteryProfileData(batteryBroadcast.dataFlow)

                // A surface container using the 'background' color from the theme
                Surface(color = MaterialTheme.colors.background) {
                    BatteryAlarmGoldApp()
                }
            }
        }
    }
}

My Compose graph.

fun NavGraphBuilder.homeNavGraph(
    navController: NavHostController
) {
    navigation(startDestination = Screen.HomeScreen.route, route = HOME_ROUTE) {
        composable(Screen.HomeScreen.route) {
            val homeBackStackEntry = remember { navController.getBackStackEntry(HOME_ROUTE) }
            val homeViewModel: HomeViewModel = hiltViewModel(homeBackStackEntry)

            HomeScreen(
                navController = navController,
                viewModel = homeViewModel
            )
        }
        composable(Screen.SelectRingtoneScreen.route) {
            val homeBackStackEntry = remember { navController.getBackStackEntry(HOME_ROUTE) }
            val homeViewModel: HomeViewModel = hiltViewModel(homeBackStackEntry)

            RingtoneScreen(
                viewModel = homeViewModel
            )
        }
    }
}

I tried to @Inject BattryBroadcast in Compose function where I also have access to ViewModel so that I can use the same ViewModel instance to set and get data but not able to use @Inject in compose function.

Lokik Soni
  • 602
  • 1
  • 5
  • 25

1 Answers1

0

Why not pass this view model instance down your BatteryAlarmGoldApp composable?

class MainActivity : ComponentActivity() {

    @Inject
    lateinit var batteryBroadcast: BatteryBroadcast

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        
        setContent {
            BatteryAlarmGoldTheme {

                val homeViewModel = hiltViewModel<HomeViewModel>()
                lifecycle.addObserver(batteryBroadcast)
                homeViewModel.setBatteryProfileData(batteryBroadcast.dataFlow)

                // A surface container using the 'background' color from the theme
                Surface(color = MaterialTheme.colors.background) {
                    BatteryAlarmGoldApp(homeViewModel)
                }
            }
        }
    }
}

Then you can use it down in your children composables

Stephen Vinouze
  • 1,815
  • 1
  • 17
  • 28