0

I have class product which contain setter and getter methods. In ThirdActivity I have array List of images and I want to retrieve them in fragments.

ThirdActivity

AdapterView.OnItemClickListener onItemClick = new
            AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position,
                                        long id) {
                    //Do any thing when user click to item

                    Intent i= new Intent(getApplicationContext(),FragmentOne.class);
                    Bundle b= new Bundle();

                    b.putInt("Big Image",productList2.get(position).getFragment1());
                    b.putInt("Big Image",productList2.get(position).getFragment2());
                    b.putInt("Big Image",productList2.get(position).getFragment3());
                    b.putInt("Big Image",productList2.get(position).getFragment4());

                    i.putExtras(b);
                    startActivity(i);


                }

            };

FragmentOne

ImageView img2;
Bundle b=getIntent().getExtras();

        int image =b.getInt("Big Image");
        img2.setImageResource(image);

but I get cannot resolve method getIntent() in FragmentOne. I also try Bundle b=getActivity().getIntent().getExtras().getString("Big Image"); but it does not work.

4 Answers4

0

You cannot open new fragments with an Intent, the fragments need to be always hosted by an activity and being inflated when you need it.

Intent just start new Activities, that's why is called:

startActivity() // Start a new Activity

That's why the intent (with bundle) is not being received in your fragment,

Hope it helps,

Crono
  • 2,024
  • 18
  • 16
0

It is better to get intent in your activity that host your fragment and pass whatever data you want to fragment with newInstance function. In your activity:

Bundle b=getIntent().getExtras();
int image =b.getInt("Big Image");
img2.setImageResource(image);
FragmentTransaction transaction = 
getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.flContent, yourFragment.newInstance(image)).commit();
Saber Solooki
  • 1,182
  • 1
  • 15
  • 34
0

Pass the value in newInstance function of fragment.

public static String IMAGE = "imagepath";
public static FragmentOne newInstance(String image) {
        FragmentOne frag = new FragmentOne();
        Bundle bundle = new Bundle();
        bundle.putString(IMAGE, image);
        frag.setArguments(bundle);
        return frag;
}

and in onCreate of fragment get this value.

String image = getArguments().getString(IMAGE);

and in activity

FragmentOne.newInstance("Any string"); //this will create new instance of fragment.
Zohra Khan
  • 5,182
  • 4
  • 25
  • 34
0

First of all even if this would work the FragmentOne activity would get only the last Int as you are overriding the same "Big Image" parameter with new number.

Try to pass a list and then retrieve it in FragmentOne. After that you can create a method which would bind string names to Fragments.

Hope this helps.

zemaitis
  • 203
  • 2
  • 12