将自定义Activity作为startActivityForResult调用

时间:2012-07-24 09:58:49

标签: android android-activity

我正在为结果启动一个活动

startActivityForResult(new Intent(this,ActivityA.class),REQUEST_CODE)

ActivityA已启动。在ActivityA上有一个gridview,我想在调用者Activity的方法onActivityResult(int requestCode,int resultCode,Intent data)中获取所选图像的位置但是我没有办法做到这一点

2 个答案:

答案 0 :(得分:23)

在活动A中,

onItemClick()

GridView

//create a new intent...
Intent intent = new Intent();
intent.putInt("position",position);
setResult(RESULT_OK,intent);
//close this Activity...
finish();

在来电活动中,

  @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent){
        super.onActivityResult(requestCode, resultCode, intent);
        Bundle extras = intent.getExtras();
        if(extras != null)
        int position = extras.getInt("position");
    }

答案 1 :(得分:0)

要唯一标识您的startActivityForResult()回调,请检查Activity A的相似性和成功之处。请按照以下步骤操作:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
    super.onActivityResult(requestCode, resultCode, intent);
    if(requestCode==REQUEST_CODE)
      {
        if(resultCode==RESULT_OK)
          {
              Bundle extras = intent.getExtras();
              if(extras != null)
              int position = extras.getInt("position");
          }
       }
}