I am trying to improve my app startup time, which is currently is on roughly 4s+- I am using MVVM clean architecture and compose only app, no fragments.
I tend to think that what cause my app to launch slowly is the view models that I create using Hilt during the onCreate. this is my onCreate method in the MainActivity:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
val themeViewModel: ThemeViewModel = hiltViewModel()
val uiState by themeViewModel.uiState.collectAsState()
val darkTheme = when (uiState.displayTheme) {
ThemeOption.AUTO -> isSystemInDarkTheme()
ThemeOption.LIGHT -> false
ThemeOption.DARK -> true
}
MainAppTheme(darkTheme = darkTheme) {
MainApp(admobCustomService = admobCustomService)
}
}
}
and this is how I initiate the MainApp with all the view models:
@Composable
fun MainApp(
connectableViewModel: ConnectableViewModel= hiltViewModel(),
controlViewModel: ControlViewModel = hiltViewModel(),
screenA_ViewModel: ScreenA_ViewModel= hiltViewModel(),
bluetoothViewModel: BluetoothViewModel= hiltViewModel(),
screenB_ViewModel: ScreenB_ViewModel = hiltViewModel(),
screenC_ViewModel: ScreenC_ViewModel= hiltViewModel(),
screenD_ViewModel: ScreenD_ViewModel= hiltViewModel(),
screenE_ViewModel: ScreenE_ViewModel= hiltViewModel(),
screenF_ViewModel: ScreenF_ViewModel= hiltViewModel(),
screenG_ViewModel: ScreenG_ViewModel= hiltViewModel(),
admobCustomService: AdmobCustomService,
) {
val appState = rememberRemoteControlAppState(
admobCustomService = admobCustomService,
activity = LocalContext.current.findActivity()
)
Scaffold(
modifier = Modifier.fillMaxSize(),
...
}
am I using Hilt correctly? should I inject the view models differently? what if I want to use the same instance of a viewmodel in multiple screens? (this is the main reason I created them as soon as possible).
any other thoughts on how to check what cause the startup time so long?
thanks