android - Call fragment method from another Fragment -


there few threads on topic already, unfortunately couldn't solve problem.

i have 2 tabs. when 1 tab finished want update one.

i tried following in first fragmenttab:

@uithread void updategallery() {     fragmenttransaction ft = getfragmentmanager().begintransaction();     ft.replace(r.layout.gallery_layout, new galleryfragment());     ft.commit(); } 

i following exception:

01-04 02:27:51.805: e/androidruntime(28235): java.lang.illegalargumentexception: no view found id 0x7f030002 fragment galleryfragment{41d7dcf0 #4 id=0x7f030002} 

and

@uithread void updategallery() {     // returns null (byid return null)     galleryfragment frg = (galleryfragment) getfragmentmanager().findfragmentbytag("gallery");       frg.init(); } 

and tried update necessary data directly:

galleryfragment gf = new galleryfragment(); gf.setimageurls(finddocumentlistall()); gf.getdocumentadapter().notifydatasetchanged(); // returns nullpointerexception 

from read far problem may never add <fragment> in main.xml(mainactivity.java) add them in mainactivitylike this:

mainactivity.addtab(this, this.mtabhost, this.mtabhost.newtabspec("gallery").setindicator("", getresources().getdrawable(r.drawable.gallery))); mainactivity.addtab(this, this.mtabhost, this.mtabhost.newtabspec("camera").setindicator("", getresources().getdrawable(r.drawable.camera))); mtabhost.setontabchangedlistener(this); madapter = new tabspageradapter(getsupportfragmentmanager()); mviewpager.setadapter(madapter); mviewpager.setonpagechangelistener(mainactivity.this); 

with tab definition in adapter like:

@override public fragment getitem(int index) {      switch (index) {         case 0:             return new galleryfragment_();         case 1:             return new camerafragment_(); // ... } 

i'm not sure if matters, use android.support.v4.app.fragment; , not android.app.fragment;

you must add method in activity this:

void updategallery() {     fragmentmanager fragmentmanager = getsupportfragmentmanager();     fragmentmanager.begintransaction().replace(r.layout.gallery_layout, fragment).commit(); } 

and in fragment, can call method activity way:

((activitymain) getactivity()).updategallery(); 

Comments