I am starting an Activity for result as
startActivityForResult(new Intent(this,ActivityA.class),REQUEST_CODE)
ActivityA is started. There is a gridview on ActivityA, I want to get the position of selected image in method onActivityResult(int requestCode, int resultCode, Intent data) of caller Activity but I am not getting the way to do that
We use startActivityForResult() to send and receive data between activities, in almost of our android projects. But recently startActivityForResult() method is deprecated in AndroidX. Android came up with ActivityResultCallback (also called Activity Results API) as an alternative for it.
Is startActivityForResult deprecated? onActivityResult , startActivityForResult , requestPermissions , and onRequestPermissionsResult are deprecated on androidx.
The request code identifies the return result when the result arrives. ( You can call startActivityForResult more than once before you get any results. When results arrive, you use the request code to distinguish one result from another.
Yes. You can call startactivityforresult() from adapter. There are two case- 1. Calling adapter from activity and need onActivityResult in activity.
In Activity A,
onItemClick()
of GridView
//create a new intent...
Intent intent = new Intent();
intent.putInt("position",position);
setResult(RESULT_OK,intent);
//close this Activity...
finish();
in Caller Activity,
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
super.onActivityResult(requestCode, resultCode, intent);
Bundle extras = intent.getExtras();
if(extras != null)
int position = extras.getInt("position");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With