4

我有以下活动屏幕

活动 A - 包含一个链接到活动 B 的按钮

活动 B - 包含订单确认,然后是一个打开活动 C 的下一步按钮(以获取签名)

Activity C - 弹出一个对话框供用户输入他们的签名和一个完成按钮。

Activity A - 包含启动 Activity B 的意图并实现 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;
    }
  }

活动 B - 包含用于启动签名捕获意图的代码以及返回到活动 A 的 onActivityForResult。

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();
        }
    }
  }

活动 C - 包含签名捕获的代码和一个完整的按钮

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;
    }
  }

当我返回到活动 A 时,我卡住了,resultCode 等于 0,这被定义为结果被取消

它正在拾取最初从 Activity A 开始的正确 requestCode 但似乎只是这个 resultCode 问题,如果有人能明白为什么?

从Activity C返回时,它可能与从Activity B调用finish()有关吗?

此外,我需要将 Intent 数据从 Activity C 传递到 A。如果我在 Activity B 中有完成(),如果我为 Activity A 启动Activity,那么它不会进入 onActivityForResult。

感谢您的帮助:]

4

2 回答 2

23

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
}
于 2013-12-14T16:27:13.823 回答
2

从Activity C返回时,它可能与从Activity B调用finish()有关吗?

是的。您需要在活动 B 中将结果设置为 OK

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

也是。

于 2013-11-04T16:39:50.897 回答