Android Can you get an activity result from a chai

2019-03-16 21:48发布

问题:

I have the following activity screens

Activity A - contains a button that links to Activity B

Activity B - contains a confirmation of order and then a Next button that opens up a Activity C (to capture signature)

Activity C - a dialog box that pops up for the user to enter their signature and a complete button.

Activity A - contains intent start to start Activity B and implements onActivityForResult

Intent intent = new Intent( this, ConfirmLines.class );
startActivityForResult( intent, GET_SIGNATURE );

protected void onActivityResult( int requestCode, int resultCode, Intent intent )
  {
    super.onActivityResult( requestCode, resultCode, intent );
    switch ( requestCode )
    {
      case GET_SIGNATURE:
        if ( resultCode == RESULT_OK )
        {
          getIntent().putExtra( SIGNATURE_DATA, intent.getStringExtra( SignatureCapture.SIGNATURE_RESULT ) );
          getIntent().putExtra( SIGNATURE_TIME, "34552655544" ); // todo - remove hardcoded signature time
          showDialog( PRINT_NAME );
        }
        else
        {
          //reset data after a cancel/back from signature screen
          getIntent().putExtra( SignatureCapture.SIGNATURE_RESULT, "" );
        }
        break;
    }
  }

Activity B - contains code to start intent for signature capture and also onActivityForResult which goes back to Activity A.

final Intent intent = new Intent( this, SignatureCapture.class );
          startActivityForResult( intent, GET_SIGNATURE );
  @Override
  protected void onActivityResult( int requestCode, int resultCode, Intent intent )
  {
    super.onActivityResult( requestCode, resultCode, intent );

    switch ( requestCode )
    {
      case GET_SIGNATURE:
        if ( resultCode == RESULT_OK )
        {
          finish();
        }
    }
  }

Activity C - contains the code for signature capture and a complete button

public void onClick( View view )
  {
    switch ( view.getId() )
    {
      case R.id.button_cancel:
        dismiss();
        nameValue.setText( "" );
        notesValue.setText( "" );
        imageView_button.setImageBitmap( null );
        break;
      case R.id.button_confirm:
        final String printedText = String.valueOf( nameValue.getText() );
        if ( printedText.isEmpty() )
        {
          Toast.makeText( getContext(), "Please enter a name", Toast.LENGTH_SHORT ).show();
        }
        else
        {
          if ( mDialogResult != null )
          {
            mDialogResult.finish( String.valueOf( nameValue.getText() ), String.valueOf( notesValue.getText() ) );
          }
          nameValue.setText( "" );
          notesValue.setText( "" );
          dismiss();
        }
        break;
    }
  }

I am getting stuck when I get returned back to Activity A, the resultCode equals 0, which is defined as the result being cancelled.

It is picking up the correct requestCode as started originally from Activity A but it's just this resultCode that seems to be the problem, if anyone can see why?

Could it possibly be to do with calling finish() from Activity B when it is returned from Activity C?

Also, I am needing to pass Intent data from Activity C to A. Where I have finish() in Activity B if I startActivity for Activity A it then does not drop into onActivityForResult.

Thanks for help in advanced :]

回答1:

If you would like to get the result from Activity C passed back to Activity A:

Start Activity B from Activity A:

Intent showB = new Intent(ActivityA, ActivityB); 
startActivityForResult(showB, RequestCode); 

In Activity B call C:

Intent showC = new Intent(ActivityC);
showC.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
startActivity(showC); 
finish(); //Close Activity B

In C:

//set the result code and close the activity
Intent result = new Intent();
setResult(resultCode, result);//like RESULT_OK
finish();

In A:

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

  ...... handle RequestCode here
}


回答2:

Could it possibly be to do with calling finish() from Activity B when it is returned from Activity C?

Yes. You need to set the result to OK in Activity B at

case GET_SIGNATURE:
if ( resultCode == RESULT_OK )
{
    // here you need set it to OK before calling finish
    finish();
}

as well.