HomeAndroidView Mannequin Creation in Jetpack Compose

View Mannequin Creation in Jetpack Compose


In Jetpack Compose, you should use both by viewModels or viewModel() composable operate to create view mannequin.

by viewModels

class MainActivity : ComponentActivity() {

    non-public val viewModel by viewModels<MainViewModel>()

    override enjoyable onCreate(savedInstanceState: Bundle?) {
        tremendous.onCreate(savedInstanceState)

        setContent {
            MainScreen(viewModel)
        }
    }
}

will be simply changed with

viewModel() composable

class MainActivity : ComponentActivity() {

    override enjoyable onCreate(savedInstanceState: Bundle?) {
        tremendous.onCreate(savedInstanceState)

        setContent {
            MainScreen(viewModel())
        }
    }
}

Supply code instance (Easy Relaxation API Demo App): MainActivity.kt.

If that does not work, you wish to explicity embody this library in your construct.gradle file.

implementation 'androidx.lifecycle:lifecycle-viewmodel-compose:2.4.0'

by ViewModels (customized constructor)

For view mannequin creation that has customized constructor, by viewModels code appears to be like like this:

non-public val viewModel by viewModels<MainViewModel> {
    MainViewModelFactory(repository)
}

viewModel() composable (customized constructor)

To transform it to viewModel() composable operate, it appears to be like like this

viewModel(manufacturing facility = MainViewModelFactory(repository))

You cross within the customized view mannequin manufacturing facility (i.e. MainViewModelFactory) as parameter of viewModel() composable operate.

Be aware: viewModel() is a composable operate, it’s essential name it inside a composable operate.

Supply code instance (Android Information app): MainActivity.kt.

HiltViewModel() – DI Framework

One other solution to create view mannequin is to make use of Hilt dependency injection(DI) framework. It is extremely just like viewModel() composable operate. You simply exchange it with hiltViewModel() which can be a composable operate.

@AndroidEntryPoint
class MainActivity : ComponentActivity() {

    override enjoyable onCreate(savedInstanceState: Bundle?) {
        tremendous.onCreate(savedInstanceState)
        setContent {
            MainScreen(
                viewModel = hiltViewModel(), 
                useSystemUIController = true
            )
        }
    }
}

Supply code instance (Android Information app – Hilt department): MainActivity.kt.

For tips on how to use the Hilt dependency injection, seek advice from this text:

Conclusion

I wasn’t conscious of this viewModel() composable operate. So I had been utilizing by viewModel. I believe it’s higher to make use of viewModel() composable operate in case your app is Jetpack Compose. You too can name viewModel() many instances and just one occasion of view mannequin is created.

One factor to remember when utilizing viewModel() composable operate is the view mannequin lifecycle is scoped to composable operate that’s nonetheless within the again stack. If the composable operate is faraway from the again state, the view mannequin is destroyed. For particulars, seek advice from this text.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments