OnActivityResult() Deprecated:A New Way to StartActivityforResult() to get Result
Previously we are doing something like this to get result from another activity
As we know passing data and getting responses between two activities are the most common thing we do during the development phase for a long time ago. Currently, the only way to get responses back from the next activity is passing Intent and requestCode through startActivityForResult. After that, we have to check the request and result code before parsing responses on onActivityResult. It might look like this sample code:
fun openSomeActivityForResult() {
val intent = Intent(this, SomeActivity::class.java)
startActivityForResult(intent, 123)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (resultCode == RESULT_OK && requestCode == 123) {
doSomeOperations()
}
}
but now its deprecated and they provided another way to do the same.
Activity Results API
Let’s Implement the new way of doing this thing.
By using this There will be no nested if anymore in code and you can remove all request code constant because it will be maintained by ComponentActivity
class.
First we need to add gradle depedancy it required.
implementation 'androidx.activity:activity-ktx:1.2.0-alpha05'
implementation 'androidx.fragment:fragment-ktx:1.3.0-alpha05'
Now where you want to start activity write below code
Step 1:Create object of contract
private val resultAPI=registerForActivityResult(
ActivityResultContracts.StartActivityForResult()) {
if(it.resultCode == Activity.RESULT_OK){
//Here you will recive data from another activity
//e.g
//key data is a key in which you set result binding.textView2.text=it.data?.getStringExtra(Constants.KEY_NAME)+" "+it.data?.getStringExtra(Constants.KEY_PWD)
}
}
Step 2:When you want to an ActivityforResult write below code to launch
binding.textView2.setOnClickListener { //adding launch on click
val intent = Intent(this, GetInputActivity::class.java)
resultAPI.launch(intent)
}
and in next Activity(GetInputActivity) you need to add below code to set result
var username=binidng.editTextTextPersonName.text.toString()
var Pwd=binidng.etPwd.text.toString()
var bundle=Intent()
bundle.putExtra(Constants.KEY_NAME,username)
bundle.putExtra(Constants.KEY_PWD,Pwd)
setResult(Activity.RESULT_OK,bundle)
finish()
And this way you can implement new way to start activity for getting result
Here I am adding full example code for batter understanding.also I have used Viewbinding in code so make sure to enable it.
MainActivity.kt
class MainActivity : AppCompatActivity() {
lateinit var binding:ActivityMainBinding
private val resultAPI=registerForActivityResult(
ActivityResultContracts.StartActivityForResult()) {
if(it.resultCode == Activity.RESULT_OK){
//Here you will recive data from another activity
//e.g
//key data is a key in which you set result
binding.textView2.text=it.data?.getStringExtra(Constants.KEY_NAME)+" "+it.data?.getStringExtra(Constants.KEY_PWD)
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding= ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.textView2.setOnClickListener {
val intent = Intent(this, GetInputActivity::class.java)
resultAPI.launch(intent)
}
}
}
GetInputActivity.kt
class GetInputActivity : AppCompatActivity() {
lateinit var binidng:ActivityGetInputBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binidng= ActivityGetInputBinding.inflate(layoutInflater)
setContentView(binidng.root)
binidng.button.setOnClickListener {
var username=binidng.editTextTextPersonName.text.toString()
var Pwd=binidng.etPwd.text.toString()
var bundle=Intent()
bundle.putExtra(Constants.KEY_NAME,username)
bundle.putExtra(Constants.KEY_PWD,Pwd)
setResult(Activity.RESULT_OK,bundle)
finish()
}
}
}
Constant.kt
object Constants {
val KEY_NAME="key_name"
val KEY_PWD="key_pwd"
}
This is the new way to startactivity for result but there also many contract available that most commonly used.
Pre-built contracts for the rescue
Google provide pre-built contract that for commonly used task such as:
RequestMultiplePermissions()
RequestPermission()
TakePicturePreview()
TakePicture()
TakeVideo()
PickContact()
CreateDocument()
OpenDocumentTree()
OpenMultipleDocuments()
OpenDocument()
GetMultipleContents()
GetContent()
I will cover up the others contracts in separate article.
Happy Coding ;)