0

I want to pass image uri from my array images[] at 0 position to other fragment. This is my fragment from which i want to pass in onclick method at postion==0.

   public class OrganicAcidFragment extends Fragment {
        String[] product_name={"Formic Acid","Acetic Acid","Propionic Acid",};
        String[] product_cn={"CH2O2","CH3COOH","C3H602"};

        int[] images={R.drawable.formicacid,R.drawable.aceticacidjpg,R.drawable.propionicacid};
        ListView list;
        String quantity;
        String price;
        FragmentManager fm;
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
            View rootview = inflater.inflate(R.layout.i_fragment_organic_acid, container, false);
            fm=getFragmentManager();


            MyAdapter adapter=new MyAdapter(getActivity(),product_name,images,product_cn,quantity,price);
            list.setAdapter(adapter);
            list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    if(position==0){

                        PurchaseFragment ldf = new PurchaseFragment ();
                        Bundle args = new Bundle();
                        args.putString("product_name", product_name[0]);
                        args.putString("product_cn", product_cn[0]);
                        ldf.setArguments(args);
                        getFragmentManager().beginTransaction().add(R.id.content_frame, ldf).commit();
                    }
                    if(position==1){
                        fm.beginTransaction().replace(R.id.content_frame,new OrganicBaseFragment()).addToBackStack("chemicalorganicfragment").commit();
                    }
                    if(position==2){
                        fm.beginTransaction().replace(R.id.content_frame,new OrganicBaseFragment()).addToBackStack("chemicalorganicfragment").commit();
                    }


                }
        });
        return rootview;
    }
}

In this fragment i want to get that data:

  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
            View rootview = inflater.inflate(R.layout.i_fragment_purchase, container, false);

            String product_names = getArguments().getString("product_name");
           String product_cns = getArguments().getString("product_cn");


            imagee=(ImageView)rootview.findViewById(R.id.productimage);
            cancel= (Button) rootview.findViewById(R.id.cancel);
            address= (EditText) rootview.findViewById(R.id.address);

            name.setText(product_names);
            cn.setText("("+product_cns+")");




            return rootview;
        }

Both fragments are in same activity.

How can i do it?

dev
  • 69
  • 8

2 Answers2

0

I think best and easiest way to send data between running activities and fragments is LocalBroadcast.

Intent intent = new Intent("some_name");
intent.putExtra("some_data_key", "some_data_value");
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent);

This is how to send local broadcast.

For listen local broadcast you must register that broadcast in onCreate() (If listener is Activity) or onCreateView() (If listener is fragment) or whenever you want to receive that broadcast:

LocalBroadcastManager.getInstance(this/*here is context*/).registerReceiver(someReceiver,
            new IntentFilter("some_name"));

This is broadcast receiver to receive data:

BroadcastReceiver someReceiver= new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String data= intent.getStringExtra("some_data_key");
        //Here do whatever you want with data
        //It isn't must be a string. If you want to send a object you must make object class implement Parcelable properly.
    }
};

In the end, don't forget to unregister this broadcast receiver. Do it in onDestroy() or whereever you want.

LocalBroadcastManager.getInstance(this).unregisterReceiver(someReceiver);
Efe AYDIN
  • 193
  • 12
  • Here i am using fragments..Is it applicable for sending from fragments to fragments? – dev May 13 '17 at 23:07
  • Yes, it is. Declare BroadcastReceiver (someReceiver) in receiver fragment then register it in onCreateView() thats all. After that send your local broadcast from whereever you want. Sending local broadcast from anywhere is okay but I think you can only receive them in Activity and Fragment. Maybe you can receive in service but I am not sure. This example is for activity but it is also valid for fragment: http://stackoverflow.com/questions/8802157/how-to-use-localbroadcastmanager – Efe AYDIN May 13 '17 at 23:16
  • what do i write instead getApplicationContext() in fragment? – dev May 14 '17 at 00:02
  • Use getContext() in fragment. – Efe AYDIN May 14 '17 at 01:54
0

You can use bundle to send data from one fragment to another.

//Put the value
YourNewFragment ldf = new YourNewFragment ();
Bundle args = new Bundle();
args.putString("YourKey", "YourValue");
ldf.setArguments(args);

//Inflate the fragment
getFragmentManager().beginTransaction().add(R.id.container, ldf).commit();

In onCreateView() of another fragment:

//Retrieve the value
String value = getArguments().getString("YourKey");
Lokesh Desai
  • 2,607
  • 16
  • 28