We mentioned in the Creating new activity section that we have passed the Parcelable user object to the MainActivity. To get this object, we need to create a request code. Let's create a request code like the following:
private val requestCode: Int = 1
Now override the onActivityResult() function, where we will retrieve the passed object of the NewUserActivity.
Here is the code of onActivityResult() function:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == this.requestCode && resultCode == Activity.RESULT_OK){
data?.let {
val users: Users = it.getParcelableExtra(getString(R.string.result_replay)) as Users
mMainViewModel.insert(users)
}
}
}
The getParcelableExtra() is used to retrieve a Parcelable object. After then, we call the mMainViewModel.insert(users) to insert the returned User into the database.