java - Passing arraylist from one activity to fragment of another -
i trying pass arraylist 1 activity activity's fragment. searched lot solution couldn't working answer.
this passing arraylist.
intent intent = new intent(sizeactivity.this,cartactivity.class); intent.putextra("printlist", photoprintslist); startactivity(intent);
accepting in activity fragment.
public class exampleactivity extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_cart); if (savedinstancestate == null) { fragment newfragment = new cartactivityfragment(); newfragment.setarguments(getintent().getextras()); fragmenttransaction ft = getfragmentmanager().begintransaction(); ft.commit(); } }
and fragment. how pass data 1 activity fragment.
private arraylist<photoprints> printsarraylist = new arraylist<>(); private string tag = "cartactivityfragment"; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.fragment_cart, container, false); return view; } @override public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); bundle b = getarguments(); if(b != null) { log.d(tag,b.size() + ""); printsarraylist = b.getparcelablearraylist("printlist"); log.d(tag,"got arraylist"); } else { log.d(tag,"null"); } }
edit:
i found oncreateview of fragment being called before oncreate of holding activity. way not able relay data activity fragment. have @ log.
09-21 17:21:32.539 22672-22672/in.reduxpress d/cartactivityfragment﹕ null checker 09-21 17:21:32.539 22672-22672/in.reduxpress d/cartactivityfragment﹕ null 09-21 17:21:32.539 22672-22672/in.reduxpress d/cartactivity﹕ received arraylist 09-21 17:21:32.539 22672-22672/in.reduxpress d/cartactivity﹕ 0 /storage/emulated/0/whatsapp/media/whatsapp images/sent/img-20150913-wa0014.jpg 09-21 17:21:32.539 22672-22672/in.reduxpress d/cartactivity﹕ 0 4 x 6 09-21 17:21:32.539 22672-22672/in.reduxpress d/cartactivity﹕ 0 glossy 09-21 17:21:32.539 22672-22672/in.reduxpress d/cartactivity﹕ 0 economy 09-21 17:21:32.539 22672-22672/in.reduxpress d/cartactivity﹕ 0 6 09-21 17:21:32.539 22672-22672/in.reduxpress d/cartactivity﹕ 1 /storage/emulated/0/whatsapp/media/whatsapp images/sent/img-20150913-wa0015.jpg 09-21 17:21:32.539 22672-22672/in.reduxpress d/cartactivity﹕ 1 4 x 6 09-21 17:21:32.539 22672-22672/in.reduxpress d/cartactivity﹕ 1 glossy 09-21 17:21:32.539 22672-22672/in.reduxpress d/cartactivity﹕ 1 economy 09-21 17:21:32.539 22672-22672/in.reduxpress d/cartactivity﹕ 1 6 09-21 17:21:32.539 22672-22672/in.reduxpress d/cartactivity﹕ 2 /storage/emulated/0/whatsapp/media/whatsapp images/sent/img-20150915-wa0003.jpg 09-21 17:21:32.539 22672-22672/in.reduxpress d/cartactivity﹕ 2 4 x 6 09-21 17:21:32.539 22672-22672/in.reduxpress d/cartactivity﹕ 2 glossy 09-21 17:21:32.539 22672-22672/in.reduxpress d/cartactivity﹕ 2 economy 09-21 17:21:32.539 22672-22672/in.reduxpress d/cartactivity﹕ 2 6 09-21 17:21:32.539 22672-22672/in.reduxpress d/cartactivity﹕ size of bundle 1
how bypass behaviour?
edit2: have tested extensively. able receive data in activity successfully. put in bundle , set bundle argument. not able receive bundle in fragment. giving me null value.
i have tried putting getarguments in oncreateview onactivitycreated, both don't work. please help.
try this,
here's code passing arraylist :
intent intent = getintent(); intent.putextra("key", selectimages); setresult(result_ok, intent); finish();
here onactivityresult().
public void onactivityresult(int requestcode, int resultcode, intent data){ if (requestcode == 999) { @suppresswarnings("unchecked") arraylist<string> ar1 = data.getstringarraylistextra("key"); if(ar1.size()!= 0){ (int = 0; < ar1.size(); i++) { string value = ar1.get(i); toast.maketext(getactivity(), "path of array in home fragment: "+ar1, toast.length_long).show(); } } } }
here's myparcelable class code:
import java.util.arraylist; import android.os.parcel; import android.os.parcelable; public class objecta implements parcelable { public arraylist<string> choices; public objecta (arraylist<string> choices) { this.choices = choices; } @suppresswarnings("unchecked") public objecta (parcel parcel) { parcel.readarraylist(null); } @override public int describecontents() { return 0; } @override public void writetoparcel(parcel dest, int flags) { dest.writestringlist(choices); } // method recreate objecta parcel public static creator<objecta> creator = new creator<objecta>() { @override public objecta createfromparcel(parcel source) { return new objecta(source); } @override public objecta[] newarray(int size) { return new objecta[size]; } }; }
that's it.
Comments
Post a Comment