Search code examples
androidonactivityresult

how to create two onactivityresult for two different class


I am having two different classes. One class is for adding image and uploading it to server and another class for editing the saved image with new image. So for getting image from sdcard, in add class I am using startactivityforresult and onactivityresult. Same thing I am trying to use in edit class because in this class im having an ImageView where new image is added but it is not allowing me to create onactivityresult in edit class. Saying error as onactivityresult is already defined. If someone knows please help me.

code:

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

    if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
        Uri filePath = data.getData();
        try {
            //Getting the Bitmap from Gallery
           bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
            //Setting the Bitmap to ImageView
            imageToUpload.setImageBitmap(bitmap);
        } catch (IOException e) {
           e.printStackTrace();
        }

Solution

  • Use it like this:

    startActivityForResult (intent, int requestCode);
    

    Then in onActivityResult you can use switch case using requestCode. Like:

     @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
         switch(requestCode) {
               //Have your cases here
               }
    }