Rashedul
Rashedul

Reputation: 121

In onActivityResult i am getting null value from received intent

I have two activities: A and B.

In activity A: On "btn_navSimilarColor" buttonClick - I made a call to B with startActivityForResult. There are already some intents inside A to use camera and gallery and a intent data i am receiving from previous activity.

In activity B: I made a asyncTask call inside onCreate() and in asyncTask's onPostExecute() i am sending back intent extra to Activity A.

Activity 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"));
        }
    }
}
 }

Activity 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();

        }
        });

        }
    }

}

Problem:

Now the problem is that i am getting data in activity B - as i am already checking it with

Toast.makeText(getApplicationContext(), "ID:: "+ str_colorCodeSimilar , Toast.LENGTH_SHORT).show();

But in Activity A's onActivityResult i am not getting the bundle extra data for "intnt_similarColor" which is:

    String stt = extras.getString("intnt_similarColor");

instead i am getting bundle extra for "xtra_selectedColor" which is inside onCreate().

Why is this happening and how am i getting the previous bundle data , not the one that has been passed from activity B?

Upvotes: 0

Views: 1307

Answers (2)

Shohan Ahmed Sijan
Shohan Ahmed Sijan

Reputation: 4531

Get string from data intent that you receive from onActivityResult. You use Bundle extras = getIntent().getExtras(); where getIntent() is actually class A's received intent.

So you have to use:

String stt = data.getStringExtra("intnt_similarColor");

Upvotes: 1

shinil
shinil

Reputation: 1493

change Bundle extras = getIntent().getExtras(); to Bundle extras = data.getExtras();

Upvotes: 2

Related Questions