12

我正在为结果开始一个活动

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

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

4

2 回答 2

27

在活动 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");
    }
于 2012-07-24T10:01:01.163 回答
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");
          }
       }
}
于 2019-01-02T21:35:11.633 回答