Skip to content

Instantly share code, notes, and snippets.

@manuelvicnt
Created May 1, 2021 12:35
Show Gist options
  • Save manuelvicnt/cde90b59eaa59bfb8382740fe7b313f9 to your computer and use it in GitHub Desktop.
Save manuelvicnt/cde90b59eaa59bfb8382740fe7b313f9 to your computer and use it in GitHub Desktop.
@HiltAndroidApp // Setup Hilt in your app
class MyApplication : Application() { ... }
// Make Hilt aware of this ViewModel
@HiltViewModel
class LoginViewModel @Inject constructor(
private val savedStateHandle: SavedStateHandle,
/* ... Other dependencies Hilt takes care of ... */
) : ViewModel() { ... }
// Make the activity use the right ViewModel factory and
// inject other dependencies
@AndroidEntryPoint
class LoginActivity : AppCompatActivity() {
private val loginViewModel: LoginViewModel by viewModels()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// loginViewModel is ready to be used
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment