0

I want to take a picture by pressing an item inside a RecyclerView. If I do ((MainActivity)context).startActivityForResult, onActivityResult in Activity will be called.. How to call it inside the Fragment holding the Adapter?

I know I can just pass the fragment inside the adapter. But I'm concerned about the memory usage.

Irshu
  • 8,248
  • 8
  • 53
  • 65
Kevin Murvie
  • 2,592
  • 1
  • 25
  • 43

1 Answers1

2

If you have a reference to the fragment in the adapter, you can use:

fragment.startActivityForResult(Intent intent, int requestCode);

Then in the fragment, you need the following:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
}
stefana
  • 2,606
  • 3
  • 29
  • 47
  • I do that, the problem is, I have to call it inside the `RecyclerViewAdapter` – Kevin Murvie Apr 26 '16 at 13:45
  • I see. I updated my answer. Note that `startActivityForResult` has different parameters. – stefana Apr 26 '16 at 13:49
  • I can have reference, I did that too and it worked, yes, but I am concerned about memory usage, would it raise my memory usage to a considerable amount by referencing the `Fragment`? I'm still having memory issues too btw – Kevin Murvie Apr 26 '16 at 13:50
  • If you pass the fragment in the constructor of the adapter, I don't see it causing memory problems. – stefana Apr 26 '16 at 13:58
  • Why is that so?? Does the Adapter dies along with the Fragment? – Kevin Murvie Apr 26 '16 at 14:01
  • Yes, since only the fragment has the reference to the adapter. – stefana Apr 26 '16 at 14:05
  • Alrighty! A colleague said this is a bad practice though, I'm actually going to convert the `Fragment` into `Activity`, but your answer is right! Congrats! *clap hands – Kevin Murvie Apr 27 '16 at 02:04