无法从以前的活动中获取数据

时间:2013-03-13 03:07:27

标签: android android-intent android-activity bundle

我希望从第二个活动中获取两个String值,然后返回第二个活动finish();之后的第一个活动。我尝试使用下面的代码,没有错误,我也无法获得返回值。

活动A

Intent intent = new Intent(activityA.this, activityB.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivityForResult(intent, 100);


@Override
protected void onActivityResult(int requestCode,
                                 int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Log.i(TAG, "onActivityResult");
    if(resultCode == 100){
        Bundle res = data.getExtras();
         String contentid = res.getString("contentID");
         String contenttype = res.getString("contentType");
         Log.i(TAG, "contentid:" +contentid);
         Log.i(TAG, "contenttype:" +contenttype);
    }
}

活动B

 @Override
 public void onDestroy(){
     super.onDestroy();
     helper.close();
     Intent intent = new Intent();
     intent.putExtra("contentID", contentID);
     intent.putExtra("contentType", contentType);
     setResult(100, intent);
     Log.i(TAG, "SETRESULT.................." +intent);
     finish();
 }

任何评论或回答都将不胜感激。

1 个答案:

答案 0 :(得分:0)

我意识到我的代码出了什么问题。我不应SETRESULT onDestroy()。如果我把它放在别处,它就有用了。对不起,谢谢你的时间