Can there be two onActivityResult in the same Android class?

2

I have to upload two images (they do not have to be uploaded at the same time) to two ImageView , each one selected separately from the gallery. I managed to get one of them, but I duplicated the data in the two ImageView because of the onActivityResult and I wondered how I could divide the code so that, or appear two onActivityResult or in it, or call it from another side to call two different methods.

The code I have is the following: private void openGallery(){ Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI); startActivityForResult(gallery, PICK_IMAGE); }
protected void onActivityResult(int RequestCode, int ResultCode, Intent data) { //super.onActivityResult(RequestCode, ResultCode, data); if(ResultCode==RESULT_OK && RequestCode==PICK_IMAGE){ imaginiUri=data.getData(); imagenInicio.setImageURI(imaginiUri); imagfinUri=data.getData(); imagenFin.setImageURI(imagfinUri); } }

Where imagenFin is ImageView

    
asked by OniNeit 28.07.2017 в 14:56
source

2 answers

2

Use a requestCode different depending on the ImageView you want to update

private void openGallery(int requestCode){
    Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
        startActivityForResult(gallery, requestCode);
}

And then in onActivityResult()

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(resultCode==RESULT_OK){
        if (requestCode==PICK_IMAGE_INI){
            imaginiUri=data.getData();
            imagenInicio.setImageURI(imaginiUri);
            return;
        }else if (requestCode==PICK_IMAGE_FIN){
            imagfinUri=data.getData();
            imagenFin.setImageURI(imagfinUri);
            return;
        }
    } 

    super.onActivityResult(RequestCode, ResultCode, data);
}

No. You can not have two onActivityResult() since it is a legacy method of Activity . When the secondary activity ends, the system calls Activity.onActivityResult , and this in turn to the method marked with @Override in your implementation, in case it exists.

    
answered by 28.07.2017 / 15:50
source
2
  

Can there be two onActivityResult in the same Android class?

NO!

If you overwrite the method repeatedly, which one would you have to access?

Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
...
...
...
}

What is done is that the requestCode and resultCode have different Id in order to identify the actions.

Regarding:

  

"I managed to make one of them appear, but I duplicated the data in   the two ImageView because of the onActivityResult "

You're actually getting the same data for imaginiUri and imagfinUri

    imaginiUri=data.getData();
    imagenInicio.setImageURI(imaginiUri);
    imagfinUri=data.getData();
    imagenFin.setImageURI(imagfinUri);
    
answered by 28.07.2017 в 21:39