WRONG onActivityResult() being called

浪子不回头ぞ 提交于 2020-01-17 12:46:05

问题


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:


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



来源:https://stackoverflow.com/questions/24479770/wrong-onactivityresult-being-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!