Android您是否可以从链接的startActivityForResult获取活动结果

时间:2013-11-04 16:28:26

标签: android android-intent android-activity start-activity onactivityresult

我有以下活动屏幕

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

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

活动C - 弹出一个对话框,供用户输入签名和完整按钮。

活动A - 包含意图开始启动活动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,定义为结果被取消。

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

从活动C返回时,是否可能从活动B调用finish()?

另外,我需要将Intent数据从Activity C传递给A.如果我在Activity A中有finish(),那么我在活动A中有完成(),然后它不会放入onActivityForResult。

感谢您提供高级帮助:]

2 个答案:

答案 0 :(得分:18)

如果您希望将活动C的结果传回活动A:

从活动A开始活动B:

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

在活动B中调用C:

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

在C:

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

在A:

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

  ...... handle RequestCode here
}

答案 1 :(得分:2)

  

从活动C返回时,是否可能从活动B调用finish()?

是。您需要在

的活动B中将结果设置为OK
case GET_SIGNATURE:
if ( resultCode == RESULT_OK )
{
    // here you need set it to OK before calling finish
    finish();
}