0

I am having some trouble with my simple app. The app starts in the MainActivity where you can press a camera icon. This opens an implicit intent for taking a photo. When the photo is taken, another activity DisplayImageActivity is opened. This activity consists of two fragments: one that holds an ImageView for displaying the photo and another one that holds some TextViews that displays some information about the photo (filename, size, location etc.). I use a ViewPager for having horizontal swipe capabilities.

Now to the problem. I should note that this is not a consistent problem. Sometimes the app crashes, sometimes it works just fine. The problem lies in getting the image path from the onActivityResult in MainActivity to the two fragments so I can get the image and info. Here is my onActivityResult method:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 42 && resultCode == RESULT_OK) {
        Log.d(TAG, "MainActivity onActivityResult method called.");

        Intent intentShowPicture = new Intent(this, DisplayImageActivity.class);

        intentShowPicture.putExtra(PICTURE_KEY, imgPath);

        startActivity(intentShowPicture);
    }
}

So I just put the image path I get from taking the picture in the bundle and start the DisplayImageActivity. Then in my DisplayImageActivity, I do this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_image);
    Log.d(MainActivity.TAG, "DisplayImageActivity onCreate called.");

    imgPath = getIntent().getExtras().getString(MainActivity.PICTURE_KEY);

    mAdapter = new FragmentAdapter(getSupportFragmentManager());
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mAdapter);

Then I have a method that just return the straing imgPath:

public String getImgPath() {
    return imgPath;
}

Then inside the fragment (PictureFragment) I try to retrieve the imgPath like this:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    DisplayImageActivity activity = (DisplayImageActivity) getActivity();
    imgPath = activity.getImgPath();

But as I mentioned earlier, sometimes the getImgPath method just returns null and the app crashes when I try to retrieve the photo. But sometimes it works fine. I am kinda lost as to why this is. Is it because the fragment is sometimes constructed before the imgPath variable is assigned in the DisplayImageActivity, so the variable is just null?

I am kinda new to android, so this might not be the best approach. I just did it from the top of my head. Any ideas why this is happening?

St0ffer
  • 161
  • 1
  • 4
  • 17

1 Answers1

0

If you want to pass data from an Activity to a Fragment, you could use this approach:

In the Activity:

    Bundle bundle = new Bundle();
    String imgPath = "path/to/my/image";
    bundle.putString("imgPath", imgPath );
    PictureFragment frag = new PictureFragment();
    frag.setArguments(bundle);
    transaction.replace(R.id.fragment_single, frag);
    transaction.commit();

Fragment:

Reading the value in fragment

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        String myValue = this.getArguments().getString("imgPath");
        ...
        ...
        ...
    }

For more information, take a look at this question How to pass a variable from Activity to Fragment, and pass it back?

Community
  • 1
  • 1
Gabriella Angelova
  • 2,985
  • 1
  • 17
  • 30
  • So the transaction.replace part replaces the current fragment instance or what? I'll try this out and see how it works! – St0ffer May 15 '15 at 15:27
  • the transaction is an animation that opens your Fragment in a View, that is with id fragment_single, but you could skip the `transaction` lines and open the fragment as you always do it :) – Gabriella Angelova May 15 '15 at 15:28