我有两个活动:A和B.
在活动A:On" btn_navSimilarColor" buttonClick - 我使用startActivityForResult调用了B。 A中已有一些意图使用相机和图库以及我从之前的活动中收到的意图数据。
在活动B中:我在onCreate()和asyncTask的onPostExecute()中进行了asyncTask调用,我向活动A发回了额外的意图。
活动A:
public class A extends Activity
{
...
@Override
public void onCreate(Bundle savedInstanceState)
{
...
Bundle extras = getIntent().getExtras();
if (extras != null) {
edtTxtColorCode.setText(extras.getString("xtra_selectedColor"));
} else {
Toast.makeText(this, "There was a problem in the response!", Toast.LENGTH_SHORT).show();
}
}
public void buttonOnClick(View view)
{
switch (view.getId())
{
case R.id.btnCamera:
startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE), FLAG_CAMERA);
break;
case R.id.btnGallery:
startActivityForResult(
new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI), FLAG_GALLERY);
break;
case R.id.btn_navSimilarColor:
Intent intnt_similar = new Intent(A.this, B.class);
intnt_similar.putExtra("xtraColor", edtTxtColorCode.getText().toString());
startActivityForResult(intnt_similar, FLAG_navSimilarColorAct);
break;
default:
break;
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.v("resultCode","="+resultCode);
if (resultCode == Activity.RESULT_OK)
{
mCursor = null;
if (requestCode == FLAG_GALLERY)
onSelectFromGalleryResult(data);
else if (requestCode == FLAG_CAMERA)
onCaptureImageResult(data);
else if(requestCode == FLAG_navSimilarColorAct)
{ Bundle extras = getIntent().getExtras();
String stt = extras.getString("intnt_similarColor");
if (extras != null)
edtTxtColorCode.setText(extras.getString("intnt_similarColor"));
}
}
}
}
活动B:
public class B extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
....
receiveIntent();
new AsyncConver().execute();
}
private void receiveIntent() {
Bundle extras = getIntent().getExtras();
if (extras != null)
strIntentrecvdColor = extras.getString("xtraColor");
else
Toast.makeText(this, "There was a problem in the response!", Toast.LENGTH_SHORT).show();
}
class AsyncConvert extends AsyncTask<String, Integer, String>
{
...
@Override
protected void onPostExecute(String s)
{
super.onPostExecute(s);
Custom_SimilarColorListAdapter gridAdapter = new Custom_SimilarColorListAdapter(SimilarColors.this, list_SimilarColors);
grdVw.setAdapter(gridAdapter);
grdVw.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
String str_colorCodeSimilar = ((TextView) v.findViewById(R.id.listrow_similar_code)).getText().toString();
Toast.makeText(getApplicationContext(), "ID:: "+ str_colorCodeSimilar , Toast.LENGTH_SHORT).show();
Intent retrnIntnt = new Intent();
retrnIntnt.putExtra("intnt_similarColor", str_colorCodeSimilar);
setResult(RESULT_OK, retrnIntnt);
finish();
}
});
}
}
}
问题:
现在问题是我在活动B中获取数据 - 因为我已经用
检查了它Toast.makeText(getApplicationContext(), "ID:: "+ str_colorCodeSimilar , Toast.LENGTH_SHORT).show();
但是在活动A的onActivityResult中,我没有获得&#34; intnt_similarColor&#34;的捆绑额外数据。这是:
String stt = extras.getString("intnt_similarColor");
相反,我正在获得&#34; xtra_selectedColor&#34;它位于onCreate()。
为什么会发生这种情况以及如何获取以前的数据包数据,而不是从活动B传递的数据?
答案 0 :(得分:2)
将Bundle extras = getIntent().getExtras();
更改为Bundle extras = data.getExtras();
答案 1 :(得分:1)
从onActivityResult收到的数据意图中获取字符串。您使用Bundle extras = getIntent().getExtras();
,其中getIntent()实际上是A类的接收意图。
所以你必须使用:
String stt = data.getStringExtra("intnt_similarColor");