When extracting string resources, the default behavior imports the string using getString(). However, in the android training website I noticed they used stringResource() instead.
My best guess is that they're interchangeable but I can't find any information on when exactly to use one or the other.
Edit:
In the training I found an excerpt saying that some Android Studio versions use getString() and to replace it with stringResource() instead. Still doesn't explain why this happens or why it's important.
Regarding your question:
When exactly to use one or the other?
It depends on the type of project you are working on.
If it is an Android project that uses the old View System, then you will not be able to use stringResource(), because it is part of the Compose integration with activity that comes when you add this dependency inside your build.gradle module file:
implementation("androidx.activity:activity-compose:1.8.1")
But, if you are developing a compose project, then the above dependency will be added by default to your project; that's why you can use either stringResource() or getString() in a compose project.
Note that stringResource() is a @Composable function and composable functions can be called from the setContent() function or from other composable functions, so if you have a case that you need to call a string resource outside the setContent() function or from outside a @Composable function, then you will need to use getString() function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With