Skip to content

Instantly share code, notes, and snippets.

@ivanalvarado
Created June 10, 2019 23:20
Show Gist options
  • Save ivanalvarado/df8094e86e8562925ff9b69d9bbaf92e to your computer and use it in GitHub Desktop.
Save ivanalvarado/df8094e86e8562925ff9b69d9bbaf92e to your computer and use it in GitHub Desktop.
Activity that passes argument to ViewModel via public setter method instead of Constructor.
const val ARGUMENT_USER_ID = "ARGUMENT_USER_ID"
class UserDetailActivity : AppCompatActivity() {
@Inject
internal lateinit var viewModelFactory: ViewModelProvider.Factory
private lateinit var userDetailViewModel: UserDetailViewModel
override fun onCreate(savedInstanceState: Bundle?) {
// ...
userDetailViewModel = ViewModelProviders.of(this, viewModelFactory).get(UserDetailViewModel::class.java)
userDetailViewModel.setUserDetail(intent.getStringExtra(ARGUMENT_USER_ID))
userDetailViewModel.getUserDetail().observe(this, Observer<UserDetailModel>{ userDetail ->
// update UI
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment