4

Android onActivityResult is Deprecated.

But I am trying to build an App in Java with a button which will give two option to select photo , either through gallery or through Camera. In onActivityResult, it used to have request codes. So earlier we can get two different request codes for camera and gallery and according using if statement we can handle them using onActivityResult. Now how should we implement it ?

Below is the older way which I mentioned:

''''

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode == CAPTURE_IMAGE){

        if(resultCode == RESULT_OK){

            if(mImageUri != null){

                //REST OF CODE
                
                     


     


            }

        }

    }


    if(requestCode == GALLARY_PICK){

        if(resultCode == RESULT_OK){


            Uri uri = data.getData();
            if(uri != null) {

                    
                 //REST OF CODE

            }

        }


    }






}

''''

Polly
  • 63
  • 9

1 Answers1

1

From the documentation:

The Activity Result APIs provide components for registering for a result, launching the result, and handling the result once it is dispatched by the system.

Also, you can read here why they deprecated the onActivityResult() callback.

When starting an activity for a result, it is possible (and, in cases of memory-intensive operations such as camera usage, almost certain) that your process and your activity will be destroyed due to low memory.

For this reason, the Activity Result APIs decouple the result callback from the place in your code where you launch the other activity. As the result callback needs to be available when your process and activity are recreated, the callback must be unconditionally registered every time your activity is created, even if the logic of launching the other activity only happens based on user input or other business logic.

For camera, use Take Picture contract

For Gallery, use Get Content contract

Update:

You can create your custom contract something like this:

class DsPhotoEditorContract : ActivityResultContract<Uri, Uri?>() {
    override fun createIntent(context: Context, input: Uri) =
        Intent(context, DsPhotoEditorActivity::class).apply {
            data = input
            putExtra(DsPhotoEditorConstants.DS_PHOTO_EDITOR_OUTPUT_DIRECTORY, "YOUR_OUTPUT_IMAGE_FOLDER")
        }

    override fun parseResult(resultCode: Int, intent: Intent?) : Uri? {
        if (resultCode != Activity.RESULT_OK) {
            return null
        }
        return intent?.data;
    }
}
Chintan Soni
  • 24,761
  • 25
  • 106
  • 174
  • Thank you....But is there a way to get multiple requestCodes....because I am also adding other things like the DS Photo Editor sdk with requestcode = 200 within the same onActivityResult. – Polly Feb 09 '22 at 10:47
  • @Polly In that case, you will need to create your own Contract. I am updating the answer. – Chintan Soni Feb 09 '22 at 10:55