TheQ
TheQ

Reputation: 2019

How to use Intent in onResume?

I'm trying to send an image path from one activity to another. I'm catching the intent in onResume, but the string path is always null. I don't know what I'm doing wrong. Hopefully you guys can help me with this problem.

Here's my activity where I grab the image path and send an intent.

private Intent testImage = new Intent(this, MyActivity.class);

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.card_create_layout);
    testImage = new Intent(this, MyActivity.class);
}


private void grabImage()
{
    Intent imageGetter = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(imageGetter, RESULT_LOAD_IMAGE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data)
    {
        Uri selectedImage = data.getData();
        String[] filePathColumn = {MediaStore.Images.Media.DATA};//Array size of 1, and we put in a string
        Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        user_image_path = cursor.getString(columnIndex);//here we have our image path.
        cursor.close();
        ImageView imageView = (ImageView) findViewById(R.id.imageView);
        imageView.setImageBitmap(BitmapFactory.decodeFile(user_image_path));
    }

    testImage.putExtra("the_image", user_image_path);

}

@Override
public void onClick(View v)
{
    switch (v.getId())
    {
        case R.id.theCreateButton:
            grabImage();
            break;
        case R.id.theDesButton:
            startActivity(sendInformation);
    }
}

@Override
public void onBackPressed()
{
    super.onBackPressed();
    MyActivity.checkCard();
    setResult(Activity.RESULT_OK, getIntent());
    finish();
}

Now in my other activity, when I grab the image and press back

@Override
public void onResume()
{
    super.onResume();
    String ImagePath = getIntent().getStringExtra("the_image");
    if(ImagePath == null)
    {
        Toast.makeText(this,"hello everyone",Toast.LENGTH_LONG).show();
    }
}

It keeps on showing the toast message "hello everyone", which means ImagePath is continuously null. How do I fix this?

Upvotes: 3

Views: 8016

Answers (2)

ginsengtang
ginsengtang

Reputation: 751

I don't see a

 startActivity(testImage);

If you aren't using that intent to start the activity, then there is no extra called 'the_image' and the getStringExtra function will effectively return a null.

@Override
public void onBackPressed() {

Intent intent = new Intent();
intent.putExtra("the_image", user_image_path);
setResult(RESULT_OK, intent);
super.onBackPressed();
} 

The above is how to correctly do it, if you have started an activity for result.

Upvotes: 1

Sergey Shustikov
Sergey Shustikov

Reputation: 15831

Pass mechanism between activities is available in three ways :

  • via DI(Dependency Injection)
  • via Bundle mechanism
  • via Singletone class which play a role a bridge or data holder between activities.

To avoid duplicating answer - please search any way(i recommend easiest - via Bundle) in stackoverflow.

Quick guide :

  1. You put your string into bundle via intent.putExtra(String name, String value) in Activity A
  2. Start this intent with startActivity(intent);
  3. In B activity read value view getIntent().getStringExtra(String name) in OnCreate method.

name value is need the same in activity A and B. This is a key.

Upvotes: 1

Related Questions