Recently I faced the onActivtyResult
is deprecated. what should we do for handle it?
any alternative introduced for that?
In KOTLIN I changed my code
startActivityForResult(intent, Constants.MY_CODE_REQUEST)
and
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == Activity.RESULT_OK) {
when (requestCode) {
Constants.MY_CODE_REQUEST -> {
...
}
FOR
registerForActivityResult(StartActivityForResult()) { result ->
onActivityResult(Constants.MY_CODE_REQUEST, result)
}.launch(intent)
and
private fun onActivityResult(requestCode: Int, result: ActivityResult) {
if(result.resultCode == Activity.RESULT_OK) {
val intent = result.data
when (requestCode) {
Constants.MY_CODE_REQUEST -> {
...
I hope it works for you. :D
From now, startActivityForResult()
has been deprecated so use new method instead of that.
Kotlin Example
fun openActivityForResult() {
startForResult.launch(Intent(this, AnotherActivity::class.java))
}
val startForResult = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
result: ActivityResult ->
if (result.resultCode == Activity.RESULT_OK) {
val intent = result.data
// Handle the Intent
//do stuff here
}
}
onActivityResult
, startActivityForResult
, requestPermissions
, and onRequestPermissionsResult
are deprecated on androidx.fragment
from 1.3.0-alpha04
, not on android.app.Activity
.
Instead, you can use Activity Result APIs with registerForActivityResult.
It seems that onActivityResult
is deprecated in the super class but you did not mention the super class name and compileSdkVersion
here in your question.
In Java and Kotlin every class or method could be marked as deprecated simply by adding @Deprecated
to it so check your super class you may extend a wrong class.
When a class is deprecated all of its methods are deprecated too.
To see a quick solution click on deprecated method and press Ctrl+Q
in Android studio to see documentation of method there should be a solution for it.
In my project using androidx
and API 29 as compileSdkVersion
, this method is NOT deprecated in activities and fragments
A basic training is available at developer.android.com.
Here is an example on how to convert the existing code with the new one:
The old way:
public void openSomeActivityForResult() {
Intent intent = new Intent(this, SomeActivity.class);
startActivityForResult(intent, 123);
}
@Override
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK && requestCode == 123) {
doSomeOperations();
}
}
The new way (Java):
// You can do the assignment inside onAttach or onCreate, i.e, before the activity is displayed
ActivityResultLauncher<Intent> someActivityResultLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode() == Activity.RESULT_OK) {
// There are no request codes
Intent data = result.getData();
doSomeOperations();
}
}
});
public void openSomeActivityForResult() {
Intent intent = new Intent(this, SomeActivity.class);
someActivityResultLauncher.launch(intent);
}
The new way (Kotlin):
var resultLauncher = registerForActivityResult(StartActivityForResult()) { result ->
if (result.resultCode === Activity.RESULT_OK) {
// There are no request codes
val data: Intent? = result.data
doSomeOperations()
}
}
fun openSomeActivityForResult() {
val intent = Intent(this, SomeActivity::class.java)
resultLauncher.launch(intent)
}