0

I have 2 activities A, B and both are using onActivityResult() in them.

The process:

  1. onActivityResult() in A works fines.
  2. I have two onActivityResult()(shown in code below) in Activity B .First one as a clickable TextView which is also working fine. The other as Button.

The problem I'm facing is in the Button which is suppose to bring a Bitmap from sub-activity of B and display in a ImageView in B. . When I click the Button it takes me to the onActivityResult() of the Activity A.

startActivityForResult() in Button of Activity B:

int capSig = arg0.getId();
if(capSig == R.id.capSig)      //Button  which takes me to sub-activity of B
{
    Intent goToCapSignatures = new Intent(this, CaptureSignature.class);
    startActivityForResult(goToCapSignatures, GET_SIG);

}

how i'm changing ByteArray to Bitmap and sending the Bitmap to Activity B:

Bitmap returnedBitmap = Bitmap.createBitmap(mContent.getWidth(),
                 mContent.getHeight(), Bitmap.Config.ARGB_8888);
         Canvas canvas = new Canvas(returnedBitmap);
         Drawable bgDrawable = mContent.getBackground();
         if (bgDrawable != null)
             bgDrawable.draw(canvas);
         else
             canvas.drawColor(Color.WHITE);
         mContent.draw(canvas);

         ByteArrayOutputStream bs = new ByteArrayOutputStream();
         returnedBitmap.compress(Bitmap.CompressFormat.PNG, 50, bs);
         byte[] byteArray = bs.toByteArray();
         Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray , 0, byteArray.length);



         //send the captured signature to Check and Operations page
         Intent returncapSigIntent = new Intent();
         returncapSigIntent.putExtra("signature",bitmap);
        setResult(RESULT_OK, returncapSigIntent);
        finish();

onActivityResult() in Activity B:

protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
      if (requestCode == GET_NOTES)        // For textview (working fine)
      {
        if(resultCode == RESULT_OK)
        {
            if (data.hasExtra("notes ready")) 
        {
            String readyNotes = data.getExtras().getString("notes ready");
            showNotesFromNotesClass.setText(readyNotes);
        }
      }

      if (requestCode == GET_SIG)         // for Button - this isn't being  
                                                      called instead 
      {
          if(resultCode == RESULT_OK)
          {
            if (data.hasExtra("signature")) 

                //display Bitmap in an ImageView

                capturedSigImageFromCapSigclass = (Bitmap) data.getExtras().get("signature");
                imgSig.setImageBitmap(capturedSigImageFromCapSigclass);
            }
          }

    }
      }
}

onActivityResult() in Activity A:(This in being called instead.

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

      if (resultCode == RESULT_OK && requestCode == REQUEST_APPLIANCE) {
        if (data.hasExtra("selected appliance")) 
        {
            String selectedAppType = data.getExtras().getString("selected appliance");
            tvApplianceType.setText(selectedAppType);
          Toast.makeText(this, data.getExtras().getString("selected appliance"),
            Toast.LENGTH_SHORT).show();
        }
      }
    }

I'm sure I'm doing something work. please give me some pointers or guidance where i'm going wrong.It will be much appreciated as I have already spend days on this logic and not getting anywhere. Thanks

1 Answers1

0

You can simply change the requestCode value on each activity to make sure that only one is called for sure.

requestsCode are simply int constants, they're only representatives!

private static final int GET_NOTES = 0;

...

private static final int GET_SIG = 1;
...

private static final int REQUEST_APPLIANCE = 2;

This should be enough to diversify the results

Angelo Cassano
  • 180
  • 1
  • 4
  • 16
  • Thanks for replying mate. Do you mean change the `requestCode` in my code `onActivityResult()` like ` if (resultCode == RESULT_OK && requestCode == 2)`? – user3677683 Jun 29 '14 at 20:15
  • No, you have to change it where the variables GET_NOTES, GET_SIG and REQUEST_APPLIANCE are declared, just make sure that they represent different numbers among them (they have to be the same in the activity caller and in the activity who gets the results) – Angelo Cassano Jun 29 '14 at 20:18
  • I have done as u suggested however still not working. Please bear in mind that REQUEST_APPLIANCE is in one class and GET_NOTES AND GET SIG both in a different class. is that not working because i have them in two different classes ? – user3677683 Jun 29 '14 at 20:27
  • If you call activity B from class A, it will always return codes to the class A That's the same if you call D from C. The only way you can let it work is to use a centralized activity which start more activities and controls the returning codes – Angelo Cassano Jun 29 '14 at 20:32
  • Sorry, I can't understand you. Would you elaborating it please. – user3677683 Jun 29 '14 at 20:36
  • If you start an Activity B from an activity A, when the activity B returns the infos with setResult, it will set the results in the Activity A, and only the onActivityResult of the Activity A will get the messages. If you have an Activity C with an onActivityResult, it will not listen to more messages which are coming from class B simply because the relationship between the Activity C and the others does not exist – Angelo Cassano Jun 29 '14 at 20:44
  • But if the `requestCodes` are different it shouldn't still call the same one though :/ – user3677683 Jun 29 '14 at 20:52